Skip to content

Commit

Permalink
disable config module for now
Browse files Browse the repository at this point in the history
It's too hard to design it
  • Loading branch information
RawDiamondMC committed Dec 21, 2024
1 parent 2791a75 commit 79d74ac
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

import band.kessoku.lib.api.base.reflect.ReflectUtil;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

public interface ConfigValue<F, T> extends Supplier<F> {
void setFrom(F value);
Expand Down Expand Up @@ -50,28 +53,60 @@ enum Type {
STRING(String.class),
INTEGER(Character.class, Byte.class, Short.class, Integer.class, Long.class),
DECIMAL(Float.class, Double.class),
NULL(Void.class);
CFG_VALUE(ConfigValue.class);//,
//NULL(Void.class);

public final Class<?>[] classes;

Type(Class<?>... classes) {
Type(final Class<?>... classes) {
this.classes = classes;
}

public static Type asType(Object o) {
if (o.getClass().isArray()) return ARRAY;
for (Type type : Type.values()) {
if (type.is(o)) return type;
public static Optional<Type> asType(Object o) {
if (o == null) return Optional.empty();
if (o.getClass().isArray()) return Optional.of(ARRAY);
for (final Type type : Type.values()) {
if (type.is(o)) return Optional.of(type);
}
return NULL;
return Optional.empty();
}

public boolean canCastFrom(Type type) {
// todo
return this == type;
// Just like how java behave
public boolean canCastFrom(final Type type) {
return false;
}

public boolean is(Object o) {
// Just like how java behave
public Object cast(final Object o) {
if (o == null) return null;
return switch (this) {
case ARRAY -> {
if (o.getClass().isArray()) {
yield o;
} else {
if (o.getClass().isAssignableFrom(Iterable.class)) {
yield Lists.newArrayList(o);
}
throw new ClassCastException();
}
};
case MAP -> (Map<?, ?>) o;
case BOOLEAN -> (boolean) o;
case STRING -> (String) o;
case INTEGER -> (long) o;
case DECIMAL -> (double) o;
case CFG_VALUE -> o;

//default -> null;
};
}

public boolean compatible(final Object o) {
final Optional<Type> optional = asType(o);
return optional.filter(this::canCastFrom).isPresent();
}

public boolean is(final Object o) {
return ReflectUtil.isAssignableFrom(o, this.classes);
}

Expand All @@ -84,7 +119,7 @@ public Object getDefaultValue() {
case INTEGER -> 0L;
case DECIMAL -> 0.0d;

default -> null;
//default -> null;
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public final class BooleanValue extends DefaultConfigValue<Boolean> {
public final class BooleanValue extends PrimitiveConfigValue<Boolean> {
private BooleanValue(final Supplier<Boolean> defaultValue) {
super(defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public final class DecimalValue extends DefaultConfigValue<Double> {
public final class DecimalValue extends PrimitiveConfigValue<Double> {
private DecimalValue(final Supplier<Double> defaultValue) {
super(defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public final class IntegerValue extends DefaultConfigValue<Long> {
public final class IntegerValue extends PrimitiveConfigValue<Long> {
private IntegerValue(final Supplier<Long> defaultValue) {
super(defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,200 +16,116 @@
package band.kessoku.lib.api.config.values;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.function.Supplier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

import band.kessoku.lib.api.config.ConfigValue;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
import org.jetbrains.annotations.UnmodifiableView;

@SuppressWarnings({"rawtypes"})
public final class ListValue<T extends ConfigValue<?, ?>> extends DefaultConfigValue<List<T>> implements List<T> {
private ListValue(final Supplier<List<T>> defaultValue) {
super(defaultValue);
}

@Override
public Type getType() {
return Type.ARRAY;
}

@Override
@SuppressWarnings({"unchecked"})
public void reset() {
this.value = new ArrayList(this.defaultValue.get());
}

@Override
@NotNull
@Unmodifiable
public List<T> getDefaultFrom() {
return Collections.unmodifiableList(super.getDefaultFrom());
}

@Override
@NotNull
@Unmodifiable
public List<T> getDefaultTo() {
return Collections.unmodifiableList(super.getDefaultTo());
}

public List<?> normalize() {
List list = new ArrayList<>();
this.value.forEach(value -> {
if (List.of(Type.ARRAY,Type.MAP).contains(value.getType())) {
try {
list.add(MethodUtils.getAccessibleMethod(value.getClass(),"normalize").invoke(value));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
} else {
list.add(value.getTo());
}
});
return list;
public final class ListValue<T extends ConfigValue<?, ?>> extends ArrayList<T> implements ConfigValue<List<T>, T[]> {
private final Consumer<ArrayList<T>> initializeConsumer;
private ListValue(Consumer<ArrayList<T>> initializeConsumer) {
super();
this.initializeConsumer = initializeConsumer;
initializeConsumer.accept(this);
}

// constructor
@Contract("_ -> new")
@SafeVarargs
@NotNull
public static <E extends ConfigValue<?, ?>> ListValue<E> of(final E... elements) {
return new ListValue<>(() -> new ArrayList<>(List.of(elements)));
return new ListValue<>(list -> list.addAll(List.of(elements)));
}

@Contract("_ -> new")
@NotNull
public static <E extends ConfigValue<?, ?>> ListValue<E> of(final List<E> list) {
return new ListValue<>(() -> new ArrayList<>(list));
public static <E extends ConfigValue<?, ?>> ListValue<E> of(final List<E> fromList) {
final List<E> clone = new ArrayList<>(fromList);
return new ListValue<>(list -> list.addAll(clone));
}

@Contract("_ -> new")
@NotNull
public static <E extends ConfigValue<?, ?>> ListValue<E> of(final Supplier<List<E>> listSupplier) {
return new ListValue<>(listSupplier);
}

// List
@Override
public int size() {
return this.value.size();
}

@Override
public boolean isEmpty() {
return this.value.isEmpty();
}

@Override
public boolean contains(final Object o) {
return this.value.contains(o);
}

@Override
public @NotNull Iterator<T> iterator() {
return this.value.iterator();
}

@Contract(pure = true)
@Override
public Object @NotNull [] toArray() {
return this.value.toArray();
public static <E extends ConfigValue<?, ?>> ListValue<E> of(final Consumer<ArrayList<E>> initializeConsumer) {
return new ListValue<>(initializeConsumer);
}

@Override
public <T1> T1 @NotNull [] toArray(final T1 @NotNull [] a) {
return this.value.toArray(a);
public void setFrom(List<T> value) {
this.addAll(value);
}

@Override
public boolean add(final T t) {
return this.value.add(t);
public void setTo(T[] value) {
Collections.addAll(this, value);
}

@Override
public boolean remove(final Object o) {
return this.value.remove(o);
@UnmodifiableView
@SuppressWarnings("RedundantUnmodifiable")
public List<T> getFrom() {
return Collections.unmodifiableList(this);
}

@Override
@SuppressWarnings("SlowListContainsAll")
public boolean containsAll(@NotNull final Collection<?> c) {
return this.value.containsAll(c);
@SuppressWarnings({"unchecked", "DataFlowIssue"})
public T[] getTo() {
return (T[]) this.toArray();
}

@Override
public boolean addAll(@NotNull final Collection<? extends T> c) {
return this.value.addAll(c);
}

@Override
public boolean addAll(final int index, @NotNull final Collection<? extends T> c) {
return this.value.addAll(index, c);
}

@Override
public boolean removeAll(@NotNull final Collection<?> c) {
return this.value.removeAll(c);
}

@Override
public boolean retainAll(@NotNull final Collection<?> c) {
return this.value.retainAll(c);
}

@Override
public void clear() {
this.value.clear();
}

@Override
public T get(final int index) {
return this.value.get(index);
}

@Override
public T set(final int index, final T element) {
return this.value.set(index, element);
}

@Override
public void add(final int index, final T element) {
this.value.add(index, element);
}

@Override
public T remove(final int index) {
return this.value.remove(index);
public void reset() {
this.clear();
this.initializeConsumer.accept(this);
}

@Override
public int indexOf(final Object o) {
return this.value.indexOf(o);
public Type getType() {
return Type.ARRAY;
}

@Override
public int lastIndexOf(final Object o) {
return this.value.lastIndexOf(o);
public List<T> getDefaultFrom() {
final ArrayList<T> list = new ArrayList<>();
initializeConsumer.accept(list);
return list;
}

@Override
@NotNull
public ListIterator<T> listIterator() {
return this.value.listIterator();
@SuppressWarnings({"unchecked", "DataFlowIssue"})
public T[] getDefaultTo() {
return (T[]) getDefaultFrom().toArray();
}

@Override
@NotNull
public ListIterator<T> listIterator(final int index) {
return this.value.listIterator(index);
public List<Object> normalize() {
final List<Object> list = new ArrayList<>();
this.forEach(value -> {
final Type type = value.getType();
if (type == Type.ARRAY || type == Type.MAP) {
try {
list.add(MethodUtils.getAccessibleMethod(value.getClass(),"normalize").invoke(value));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
} else {
list.add(value.getTo());
}
});
return list;
}

@Override
@NotNull
public List<T> subList(final int fromIndex, final int toIndex) {
return this.value.subList(fromIndex, toIndex);
public static <E extends ConfigValue<V,?>,V> List<E> transform(List<V> list, Function<V,E> boxFunc) {
final List<E> result = new ArrayList<>();
list.forEach(v -> {
result.add(boxFunc.apply(v));
});
return result;
}
}
Loading

0 comments on commit 79d74ac

Please sign in to comment.