24package com.dreamfirestudios.dreamcore.DreamVariable;
26import com.dreamfirestudios.dreamcore.DreamPersistentData.PersistentDataTypes;
28import java.lang.reflect.Array;
29import java.util.ArrayList;
50 private final PersistentDataTypes pdt;
51 private final Class<T> valueType;
52 private final List<Class<?>> supported;
63 this.valueType = valueType;
64 this.supported =
new ArrayList<>();
65 this.supported.add(valueType);
67 Class<?> primitive = boxedToPrimitive(valueType);
68 if (includePrimitive && primitive !=
null){
69 supported.add(primitive);
73 supported.add(Array.newInstance(valueType, 0).getClass());
74 if (primitive !=
null) supported.add(Array.newInstance(primitive, 0).getClass());
82 this(pdt, valueType,
true,
true);
90 public final List<Class<?>>
supportedTypes() {
return List.copyOf(supported); }
103 if (raw ==
null)
throw new IllegalArgumentException(
"raw cannot be null");
113 if (value ==
null)
return null;
114 if (value.getClass().isArray()) {
115 int len = Array.getLength(value);
116 Object[] out =
new Object[len];
117 for (
int i = 0; i < len; i++) out[i] =
serialize(Array.get(value, i));
120 return value.toString();
129 if (storageValue ==
null)
return null;
130 if (storageValue.getClass().isArray()) {
131 int len = Array.getLength(storageValue);
132 Object arr = Array.newInstance(valueType, len);
133 for (
int i = 0; i < len; i++) {
134 Array.set(arr, i,
parse(String.valueOf(Array.get(storageValue, i))));
138 return parse(String.valueOf(storageValue));
145 if (variable ==
null)
return false;
146 for (Class<?> c : supported)
if (c.isInstance(variable))
return true;
147 try {
parse(String.valueOf(variable));
return true; }
catch (RuntimeException ex) {
return false; }
150 private static Class<?> boxedToPrimitive(Class<?> boxed) {
151 if (boxed == Boolean.class)
return boolean.class;
152 if (boxed == Integer.class)
return int.class;
153 if (boxed == Long.class)
return long.class;
154 if (boxed == Double.class)
return double.class;
155 if (boxed == Float.class)
return float.class;
156 if (boxed == Short.class)
return short.class;
157 if (boxed == Byte.class)
return byte.class;
158 if (boxed == Character.class)
return char.class;
Base class for variable “tests” that parse/validate values and serialize/deserialise to persistent st...
final List< Class<?> > supportedTypes()
All supported runtime types (boxed/primitive/arrays as configured).
DreamAbstractVariableTest(PersistentDataTypes pdt, Class< T > valueType, boolean includePrimitive, boolean includeArray)
Creates a variable test with explicit flags for primitive and array support.
Object deserialize(Object storageValue)
Deserializes storage into value (or array of values).
Object serialize(Object value)
Serializes an object (or array) into a storage-friendly representation.
boolean isType(Object variable)
Checks if an object is compatible (direct type or parsable).
abstract T parseImpl(String raw)
Parses a trimmed string into T .
abstract T defaultValue()
Default value for this type.
DreamAbstractVariableTest(PersistentDataTypes pdt, Class< T > valueType)
Creates a variable test with primitive+array support enabled.
T parse(String raw)
Parses an input string (must be non-null).
final Class< T > valueType()
Primary boxed value type.
final PersistentDataTypes persistentType()
Persistent data type backing this test.