DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamAbstractVariableTest.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamVariable;
25
26import com.dreamfirestudios.dreamcore.DreamPersistentData.PersistentDataTypes;
27
28import java.lang.reflect.Array;
29import java.util.ArrayList;
30import java.util.List;
31
48public abstract class DreamAbstractVariableTest<T> {
49
50 private final PersistentDataTypes pdt;
51 private final Class<T> valueType;
52 private final List<Class<?>> supported;
53
61 protected DreamAbstractVariableTest(PersistentDataTypes pdt, Class<T> valueType, boolean includePrimitive, boolean includeArray) {
62 this.pdt = pdt;
63 this.valueType = valueType;
64 this.supported = new ArrayList<>();
65 this.supported.add(valueType);
66
67 Class<?> primitive = boxedToPrimitive(valueType);
68 if (includePrimitive && primitive != null){
69 supported.add(primitive);
70 }
71
72 if (includeArray) {
73 supported.add(Array.newInstance(valueType, 0).getClass());
74 if (primitive != null) supported.add(Array.newInstance(primitive, 0).getClass());
75 }
76 }
77
81 protected DreamAbstractVariableTest(PersistentDataTypes pdt, Class<T> valueType) {
82 this(pdt, valueType, true, true);
83 }
84
86 public final PersistentDataTypes persistentType() { return pdt; }
88 public final Class<T> valueType() { return valueType; }
90 public final List<Class<?>> supportedTypes() { return List.copyOf(supported); }
91
93 protected abstract T parseImpl(String raw);
95 public abstract T defaultValue();
96
102 public T parse(String raw) {
103 if (raw == null) throw new IllegalArgumentException("raw cannot be null");
104 return parseImpl(raw.trim());
105 }
106
112 public Object serialize(Object value) {
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));
118 return out;
119 }
120 return value.toString();
121 }
122
128 public Object deserialize(Object storageValue) {
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))));
135 }
136 return arr;
137 }
138 return parse(String.valueOf(storageValue));
139 }
140
144 public boolean isType(Object variable) {
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; }
148 }
149
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;
159 return null;
160 }
161}
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 .
DreamAbstractVariableTest(PersistentDataTypes pdt, Class< T > valueType)
Creates a variable test with primitive+array support enabled.
final PersistentDataTypes persistentType()
Persistent data type backing this test.