Skip to content

Commit

Permalink
added proper generic-typing on ScalarType
Browse files Browse the repository at this point in the history
  • Loading branch information
BlvckBytes committed Sep 18, 2024
1 parent 4c7323f commit 5b9cc1d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
16 changes: 8 additions & 8 deletions src/main/java/me/blvckbytes/bbconfigmapper/ConfigValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ public ConfigValue(@Nullable Object value, @Nullable IExpressionEvaluator evalua

@Override
@SuppressWarnings("unchecked")
public <T> T asScalar(ScalarType type, IEvaluationEnvironment env) {
public <T> T asScalar(ScalarType<T> type, IEvaluationEnvironment env) {
return (T) interpret(value, type.getType(), null, env);
}

@Override
@SuppressWarnings("unchecked")
public <T> List<T> asList(ScalarType type, IEvaluationEnvironment env) {
public <T> List<T> asList(ScalarType<T> type, IEvaluationEnvironment env) {
return (List<T>) interpret(value, List.class, new ScalarType[] { type }, env);
}

@Override
@SuppressWarnings("unchecked")
public <T> Set<T> asSet(ScalarType type, IEvaluationEnvironment env) {
public <T> Set<T> asSet(ScalarType<T> type, IEvaluationEnvironment env) {
return (Set<T>) interpret(value, Set.class, new ScalarType[] { type }, env);
}

Expand All @@ -68,7 +68,7 @@ public Object asRawObject(IEvaluationEnvironment env) {

@Override
@SuppressWarnings("unchecked")
public <T, U> Map<T, U> asMap(ScalarType key, ScalarType value, IEvaluationEnvironment env) {
public <T, U> Map<T, U> asMap(ScalarType<T> key, ScalarType<T> value, IEvaluationEnvironment env) {
return (Map<T, U>) interpret(value, Map.class, new ScalarType[] { key, value }, env);
}

Expand All @@ -82,7 +82,7 @@ public <T, U> Map<T, U> asMap(ScalarType key, ScalarType value, IEvaluationEnvir
* @return Guaranteed non-Null value of the requested type
*/
@SuppressWarnings("unchecked")
protected <T> T interpretScalar(@Nullable Object input, ScalarType type, IEvaluationEnvironment env) {
protected <T> T interpretScalar(@Nullable Object input, ScalarType<T> type, IEvaluationEnvironment env) {
Class<?> typeClass = type.getType();

if (typeClass.isInstance(input))
Expand All @@ -108,7 +108,7 @@ protected <T> T interpretScalar(@Nullable Object input, ScalarType type, IEvalua
* @return Guaranteed non-null value of the requested type
*/
@SuppressWarnings("unchecked")
private<T> T interpret(@Nullable Object input, Class<T> type, @Nullable ScalarType[] genericTypes, IEvaluationEnvironment env) {
private<T> T interpret(@Nullable Object input, Class<T> type, @Nullable ScalarType<T>[] genericTypes, IEvaluationEnvironment env) {

if (input instanceof AExpression && this.evaluator != null)
input = this.evaluator.evaluateExpression((AExpression) input, env);
Expand Down Expand Up @@ -182,12 +182,12 @@ private<T> T interpret(@Nullable Object input, Class<T> type, @Nullable ScalarTy
return (T) results;
}

ScalarType scalarType = ScalarType.fromClass(type);
ScalarType<?> scalarType = ScalarType.fromClass(type);

if (scalarType == null)
throw new IllegalStateException("Unknown scalar type provided: " + type);

return interpretScalar(input, scalarType, env);
return (T) interpretScalar(input, scalarType, env);
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/me/blvckbytes/bbconfigmapper/IEvaluable.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@

public interface IEvaluable {

<T> T asScalar(ScalarType type, IEvaluationEnvironment env);
<T> T asScalar(ScalarType<T> type, IEvaluationEnvironment env);

<T, U> Map<T, U> asMap(ScalarType key, ScalarType value, IEvaluationEnvironment env);
<T, U> Map<T, U> asMap(ScalarType<T> key, ScalarType<T> value, IEvaluationEnvironment env);

<T> List<T> asList(ScalarType type, IEvaluationEnvironment env);
<T> List<T> asList(ScalarType<T> type, IEvaluationEnvironment env);

<T> Set<T> asSet(ScalarType type, IEvaluationEnvironment env);
<T> Set<T> asSet(ScalarType<T> type, IEvaluationEnvironment env);

Object asRawObject(IEvaluationEnvironment env);

Expand Down
20 changes: 10 additions & 10 deletions src/main/java/me/blvckbytes/bbconfigmapper/ScalarType.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@
import java.util.Map;
import java.util.function.BiFunction;

public enum ScalarType {
INT(int.class, (i, e) -> (int) e.getValueInterpreter().asLong(i)),
LONG(long.class, (i, e) -> e.getValueInterpreter().asLong(i)),
DOUBLE(double.class, (i, e) -> e.getValueInterpreter().asDouble(i)),
BOOLEAN(boolean.class, (i, e) -> e.getValueInterpreter().asBoolean(i)),
STRING(String.class, (i, e) -> e.getValueInterpreter().asString(i))
;
public class ScalarType<T> {

public static final ScalarType<Integer> INT = new ScalarType<>(int.class, (i, e) -> (int) e.getValueInterpreter().asLong(i));
public static final ScalarType<Long> LONG = new ScalarType<>(long.class, (i, e) -> e.getValueInterpreter().asLong(i));
public static final ScalarType<Double> DOUBLE = new ScalarType<>(double.class, (i, e) -> e.getValueInterpreter().asDouble(i));
public static final ScalarType<Boolean> BOOLEAN = new ScalarType<>(boolean.class, (i, e) -> e.getValueInterpreter().asBoolean(i));
public static final ScalarType<String> STRING = new ScalarType<>(String.class, (i, e) -> e.getValueInterpreter().asString(i));

private final Class<?> type;
private final BiFunction<@Nullable Object, IEvaluationEnvironment, Object> interpreter;

private static final Map<Class<?>, ScalarType> lookupTable;
private static final Map<Class<?>, ScalarType<?>> lookupTable;

static {
lookupTable = new HashMap<>();
Expand All @@ -57,7 +57,7 @@ public enum ScalarType {
lookupTable.put(String.class, STRING);
}

ScalarType(Class<?> type, BiFunction<@Nullable Object, IEvaluationEnvironment, Object> interpreter) {
private ScalarType(Class<?> type, BiFunction<@Nullable Object, IEvaluationEnvironment, Object> interpreter) {
this.type = type;
this.interpreter = interpreter;
}
Expand All @@ -70,7 +70,7 @@ public BiFunction<Object, IEvaluationEnvironment, Object> getInterpreter() {
return interpreter;
}

public static @Nullable ScalarType fromClass(Class<?> c) {
public static @Nullable ScalarType<?> fromClass(Class<?> c) {
return lookupTable.get(c);
}
}

0 comments on commit 5b9cc1d

Please sign in to comment.