From 5b9cc1d7706870c1a66bc9da1b50c732cb9a9fc5 Mon Sep 17 00:00:00 2001 From: Philip Hell Date: Wed, 18 Sep 2024 18:35:04 +0200 Subject: [PATCH] added proper generic-typing on ScalarType --- .../bbconfigmapper/ConfigValue.java | 16 +++++++-------- .../blvckbytes/bbconfigmapper/IEvaluable.java | 8 ++++---- .../blvckbytes/bbconfigmapper/ScalarType.java | 20 +++++++++---------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/java/me/blvckbytes/bbconfigmapper/ConfigValue.java b/src/main/java/me/blvckbytes/bbconfigmapper/ConfigValue.java index e367ac8..9192a26 100644 --- a/src/main/java/me/blvckbytes/bbconfigmapper/ConfigValue.java +++ b/src/main/java/me/blvckbytes/bbconfigmapper/ConfigValue.java @@ -43,19 +43,19 @@ public ConfigValue(@Nullable Object value, @Nullable IExpressionEvaluator evalua @Override @SuppressWarnings("unchecked") - public T asScalar(ScalarType type, IEvaluationEnvironment env) { + public T asScalar(ScalarType type, IEvaluationEnvironment env) { return (T) interpret(value, type.getType(), null, env); } @Override @SuppressWarnings("unchecked") - public List asList(ScalarType type, IEvaluationEnvironment env) { + public List asList(ScalarType type, IEvaluationEnvironment env) { return (List) interpret(value, List.class, new ScalarType[] { type }, env); } @Override @SuppressWarnings("unchecked") - public Set asSet(ScalarType type, IEvaluationEnvironment env) { + public Set asSet(ScalarType type, IEvaluationEnvironment env) { return (Set) interpret(value, Set.class, new ScalarType[] { type }, env); } @@ -68,7 +68,7 @@ public Object asRawObject(IEvaluationEnvironment env) { @Override @SuppressWarnings("unchecked") - public Map asMap(ScalarType key, ScalarType value, IEvaluationEnvironment env) { + public Map asMap(ScalarType key, ScalarType value, IEvaluationEnvironment env) { return (Map) interpret(value, Map.class, new ScalarType[] { key, value }, env); } @@ -82,7 +82,7 @@ public Map asMap(ScalarType key, ScalarType value, IEvaluationEnvir * @return Guaranteed non-Null value of the requested type */ @SuppressWarnings("unchecked") - protected T interpretScalar(@Nullable Object input, ScalarType type, IEvaluationEnvironment env) { + protected T interpretScalar(@Nullable Object input, ScalarType type, IEvaluationEnvironment env) { Class typeClass = type.getType(); if (typeClass.isInstance(input)) @@ -108,7 +108,7 @@ protected T interpretScalar(@Nullable Object input, ScalarType type, IEvalua * @return Guaranteed non-null value of the requested type */ @SuppressWarnings("unchecked") - private T interpret(@Nullable Object input, Class type, @Nullable ScalarType[] genericTypes, IEvaluationEnvironment env) { + private T interpret(@Nullable Object input, Class type, @Nullable ScalarType[] genericTypes, IEvaluationEnvironment env) { if (input instanceof AExpression && this.evaluator != null) input = this.evaluator.evaluateExpression((AExpression) input, env); @@ -182,12 +182,12 @@ private T interpret(@Nullable Object input, Class 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 diff --git a/src/main/java/me/blvckbytes/bbconfigmapper/IEvaluable.java b/src/main/java/me/blvckbytes/bbconfigmapper/IEvaluable.java index 30d68a9..227f558 100644 --- a/src/main/java/me/blvckbytes/bbconfigmapper/IEvaluable.java +++ b/src/main/java/me/blvckbytes/bbconfigmapper/IEvaluable.java @@ -32,13 +32,13 @@ public interface IEvaluable { - T asScalar(ScalarType type, IEvaluationEnvironment env); + T asScalar(ScalarType type, IEvaluationEnvironment env); - Map asMap(ScalarType key, ScalarType value, IEvaluationEnvironment env); + Map asMap(ScalarType key, ScalarType value, IEvaluationEnvironment env); - List asList(ScalarType type, IEvaluationEnvironment env); + List asList(ScalarType type, IEvaluationEnvironment env); - Set asSet(ScalarType type, IEvaluationEnvironment env); + Set asSet(ScalarType type, IEvaluationEnvironment env); Object asRawObject(IEvaluationEnvironment env); diff --git a/src/main/java/me/blvckbytes/bbconfigmapper/ScalarType.java b/src/main/java/me/blvckbytes/bbconfigmapper/ScalarType.java index 29307d6..158d171 100644 --- a/src/main/java/me/blvckbytes/bbconfigmapper/ScalarType.java +++ b/src/main/java/me/blvckbytes/bbconfigmapper/ScalarType.java @@ -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 { + + public static final ScalarType INT = new ScalarType<>(int.class, (i, e) -> (int) e.getValueInterpreter().asLong(i)); + public static final ScalarType LONG = new ScalarType<>(long.class, (i, e) -> e.getValueInterpreter().asLong(i)); + public static final ScalarType DOUBLE = new ScalarType<>(double.class, (i, e) -> e.getValueInterpreter().asDouble(i)); + public static final ScalarType BOOLEAN = new ScalarType<>(boolean.class, (i, e) -> e.getValueInterpreter().asBoolean(i)); + public static final ScalarType 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, ScalarType> lookupTable; + private static final Map, ScalarType> lookupTable; static { lookupTable = new HashMap<>(); @@ -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; } @@ -70,7 +70,7 @@ public BiFunction getInterpreter() { return interpreter; } - public static @Nullable ScalarType fromClass(Class c) { + public static @Nullable ScalarType fromClass(Class c) { return lookupTable.get(c); } }