Skip to content

Commit c80d378

Browse files
Refactoring VariableConverter, splitting it to VariableParser and it.
Some formatings
1 parent 402a77d commit c80d378

File tree

7 files changed

+137
-84
lines changed

7 files changed

+137
-84
lines changed

SerialX-core/src/main/java/org/ugp/serialx/GenericScope.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public <V extends ValT> V get(KeyT variableKey)
267267
* @param variableKey | Variables name.
268268
* @param defaultValue | Default value to return.
269269
*
270-
* @return Value of variable with name or defaultValue if there is no such a one!
270+
* @return Value of variable with name or defaultValue if there is no such a one or given key contains null!
271271
*
272272
* @since 1.2.5
273273
*/
@@ -337,7 +337,7 @@ public <V extends ValT> V get(KeyT... pathToValue)
337337
* @param cls | Default value to return.
338338
* @param defaultValue | Class that you want the obtained object to be converted into! Exact conversion algorithm can differ based on its implementations.
339339
*
340-
* @return Value of variable with name given converted to object of cls or defaultValue if there is no such a one!
340+
* @return Value of variable with name given converted to object of cls or defaultValue if there is no such a one or given key contains null!
341341
*
342342
* @throws Exception | If converting to object of cls failed from some reason! This can differ from implementation to implementation! By default it uses {@link GenericScope#toObject(cls)}
343343
*
@@ -792,7 +792,7 @@ public boolean isEmpty()
792792
*/
793793
public <T extends GenericScope<?, ?>> T getParent()
794794
{
795-
return (T) getParent(1);
795+
return getParent(1);
796796
}
797797

798798
/**
@@ -804,6 +804,7 @@ public boolean isEmpty()
804804
*
805805
* @since 1.3.2
806806
*/
807+
@SuppressWarnings("unchecked")
807808
public <T extends GenericScope<?, ?>> T getParent(int depth)
808809
{
809810
if (depth < 1)

SerialX-core/src/main/java/org/ugp/serialx/Scope.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public byte getByte(String variableName)
159159
* @param variableName | Variables name.
160160
* @param defaultValue | Default value to return.
161161
*
162-
* @return Byte value of variable with name or defaultValue if there is no such a one!
162+
* @return Byte value of variable with name or defaultValue if there is no such a one or given key contains null!
163163
* Byte will be also parsed from {@link Number}, {@link Character} or {@link CharSequence} if possible!
164164
*
165165
* @since 1.2.5
@@ -186,7 +186,7 @@ public short getShort(String variableName)
186186
* @param variableName | Variables name.
187187
* @param defaultValue | Default value to return.
188188
*
189-
* @return Byte value of variable with name or defaultValue if there is no such a one!
189+
* @return Byte value of variable with name or defaultValue if there is no such a one or given key contains null!
190190
* Byte will be also parsed from {@link Number}, {@link Character} or {@link CharSequence} if possible!
191191
*
192192
* @since 1.2.5
@@ -213,7 +213,7 @@ public int getInt(String variableName)
213213
* @param variableName | Variables name.
214214
* @param defaultValue | Default value to return.
215215
*
216-
* @return Int value of variable with name or defaultValue if there is no such a one!
216+
* @return Int value of variable with name or defaultValue if there is no such a one or given key contains null!
217217
* Int will be also parsed from {@link Number}, {@link Character} or {@link CharSequence} if possible!
218218
*
219219
* @since 1.2.5
@@ -240,7 +240,7 @@ public int getLong(String variableName)
240240
* @param variableName | Variables name.
241241
* @param defaultValue | Default value to return.
242242
*
243-
* @return Long value of variable with name or defaultValue if there is no such a one!
243+
* @return Long value of variable with name or defaultValue if there is no such a one or given key contains null!
244244
* Long will be also parsed from {@link Number}, {@link Character} or {@link CharSequence} if possible!
245245
*
246246
* @since 1.2.5
@@ -267,7 +267,7 @@ public float getFloat(String variableName)
267267
* @param variableName | Variables name.
268268
* @param defaultValue | Default value to return.
269269
*
270-
* @return Float value of variable with name or defaultValue if there is no such a one!
270+
* @return Float value of variable with name or defaultValue if there is no such a one or given key contains null!
271271
* Float will be also parsed from {@link Number}, {@link Character} or {@link CharSequence} if possible!
272272
*
273273
* @since 1.2.5
@@ -294,7 +294,7 @@ public double getDouble(String variableName)
294294
* @param variableName | Variables name.
295295
* @param defaultValue | Default value to return.
296296
*
297-
* @return Double value of variable with name or defaultValue if there is no such a one!
297+
* @return Double value of variable with name or defaultValue if there is no such a one or given key contains null!
298298
* Double will be also parsed from {@link Number}, {@link Character} or {@link CharSequence} if possible!
299299
*
300300
* @since 1.2.5
@@ -328,7 +328,7 @@ public String getString(String variableName)
328328
* @param variableName | Variables name.
329329
* @param defaultValue | Default value to return.
330330
*
331-
* @return String value of variable with name or defaultValue if there is no such a one!
331+
* @return String value of variable with name or defaultValue if there is no such a one or given key contains null!
332332
* String will be also parsed from any object using {@link Object#toString()}!
333333
*
334334
* @since 1.2.5
@@ -355,7 +355,7 @@ public char getChar(String variableName)
355355
* @param variableName | Variables name.
356356
* @param defaultValue | Default value to return.
357357
*
358-
* @return Char value of variable with name or defaultValue if there is no such a one!
358+
* @return Char value of variable with name or defaultValue if there is no such a one or given key contains null!
359359
* Char will be also parsed from {@link Number}, {@link Character} or {@link CharSequence} if possible!
360360
*
361361
* @since 1.2.5
@@ -389,7 +389,7 @@ public boolean getBool(String variableName)
389389
* @param variableName | Variables name.
390390
* @param defaultValue | Default value to return.
391391
*
392-
* @return Boolean value of variable with name or defaultValue if there is no such a one!
392+
* @return Boolean value of variable with name or defaultValue if there is no such a one or given key contains null!
393393
* Boolean will be also parsed from {@link Number}, or {@link CharSequence} if possible!
394394
*
395395
* @since 1.2.5
@@ -715,7 +715,7 @@ public <T> T toObjectOf(String variableWithscope, Class<T> objClass) throws Exce
715715
* @param objClass | Object of class to create.
716716
* @param defaultValue | Default value to return.
717717
*
718-
* @return Value of variable with name or defaultValue if there is no such a one! If there is no Scope<Object, ?> stored by variableWithScope this function behaves same as {@link Scope#get(String, Object)}!
718+
* @return Value of variable with name or defaultValue if there is no such a one or given key contains null! If there is no Scope<Object, ?> stored by variableWithScope this function behaves same as {@link Scope#get(String, Object)}!
719719
* If variableWithScope contains {@link GenericScope} like it supposed to, then values of scope are parsed to {@link SerializationProtocol} of objClass.
720720
*
721721
* @see Serializer#PROTOCOL_REGISTRY
@@ -736,7 +736,7 @@ public <T> T toObjectOf(String variableWithscope, Class<T> objClass, T defaultVa
736736
* @param defaultValue | Default value to return.
737737
* @param protocolsToUse | Registry of protocols to use.
738738
*
739-
* @return Value of variable with name or defaultValue if there is no such a one! If there is no Scope<Object, ?> stored by variableWithScope this function behaves same as {@link Scope#get(String, Object)}!
739+
* @return Value of variable with name or defaultValue if there is no such a one or given key contains null! If there is no Scope<Object, ?> stored by variableWithScope this function behaves same as {@link Scope#get(String, Object)}!
740740
* If variableWithScope contains {@link GenericScope} like it supposed to, then values of scope are parsed to {@link SerializationProtocol} of objClass.
741741
*
742742
* @see Serializer#PROTOCOL_REGISTRY

SerialX-core/src/main/java/org/ugp/serialx/Utils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,8 @@ public static void post(Serializer serializer, HttpURLConnection conn) throws IO
854854
* @author PETO
855855
*
856856
* @since 1.2.2
857+
*
858+
* @deprecated (in 1.3.7) NO LONGER NEEDED, DO NOT USE THIS! You were never supposed to...
857859
*/
858860
public static final class NULL //TODO: REMOVE!!!!
859861
{

SerialX-core/src/main/java/org/ugp/serialx/converters/DataParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public interface DataParser
4141
*
4242
* @since 1.3.0
4343
*/
44-
public static final ParserRegistry REGISTRY = new ParserRegistry(/* new OperationGroups(), new VariableConverter(),*/ new StringConverter(), /*new ObjectConverter(),*/ /*TODO: remove new ArrayConverter(),*/ new NumberConverter(), new BooleanConverter(), new CharacterConverter(), new NullConverter(), new SerializableBase64Converter());
44+
public static final ParserRegistry REGISTRY = new ParserRegistry(new VariableParser(), new StringConverter(), new ProtocolConverter(), new NumberConverter(), new BooleanConverter(), new CharacterConverter(), new NullConverter(), new SerializableBase64Converter());
4545

4646
/**
4747
* @param myHomeRegistry | Registry where this parser is registered provided by {@link DataParser#parseObj(Registry, String, boolean, Class[], Object...)} otherwise it demands on implementation (it should not be null)!
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.ugp.serialx.converters;
2+
3+
import static org.ugp.serialx.Utils.Clone;
4+
import static org.ugp.serialx.Utils.contains;
5+
import static org.ugp.serialx.Utils.splitValues;
6+
7+
import org.ugp.serialx.GenericScope;
8+
import org.ugp.serialx.Registry;
9+
import org.ugp.serialx.Scope;
10+
import org.ugp.serialx.Serializer;
11+
12+
/**
13+
* This parser is capable of reading variables from {@link GenericScope} by using "$"!
14+
* {@link VariableConverter#parse(String, Object...)} required one additional Scope argument in args... at index 0!
15+
* Its case sensitive!<br>
16+
* Exact outputs of this converter are based on inserted scope!
17+
*
18+
* @author PETO
19+
*
20+
* @since 1.3.7
21+
*/
22+
public class VariableParser implements DataParser
23+
{
24+
@SuppressWarnings("unchecked")
25+
@Override
26+
public Object parse(ParserRegistry myHomeRegistry, String str, Object... args)
27+
{
28+
if (args.length > 0 && str.length() > 0 && args[0] instanceof GenericScope)
29+
{
30+
return parse(myHomeRegistry, str, (GenericScope<Object, Object>) args[0], args);
31+
}
32+
return CONTINUE;
33+
}
34+
35+
/**
36+
* @param myHomeRegistry | Registry where this parser is registered provided by {@link DataParser#parseObj(Registry, String, boolean, Class[], Object...)} otherwise it demands on implementation (it should not be null)!
37+
* @param str | Source string (preferably with some variables to read)!
38+
* @param scope | Source scope to read from, can't be null!
39+
* @param args | Some additional args. This can be anything and it demands on implementation of DataParser.
40+
*
41+
* @return Value of variable read from scope is str was suitable. Special return types are {@link DataParser#VOID} and {@link DataParser#CONTINUE}. Continue will ignore this parser and jump to another one in registry.
42+
*
43+
* @since 1.3.7
44+
*/
45+
@SuppressWarnings("unchecked")
46+
protected Object parse(ParserRegistry myHomeRegistry, String str, GenericScope<?, Object> scope, Object... args)
47+
{
48+
if (str.charAt(0) == '$' && !contains(str, ' ', '+', '-', '*', '/', '%', '>', '<', '=', '&', '|', '^', '?', '='))
49+
{
50+
51+
boolean clsModif = str.endsWith("::class"), newModif = false; // Handle modifiers...
52+
if (clsModif)
53+
str = str.substring(0, str.length()-7);
54+
else if (newModif = str.endsWith("::new"))
55+
str = str.substring(0, str.length()-5);
56+
57+
Object obj = null;
58+
if ((str = str.substring(1)).indexOf('.') > -1)
59+
{
60+
Object[] path = splitValues(str, '.');
61+
int iLast = path.length-1;
62+
63+
backlook: do
64+
{
65+
Object sc = scope.variables().getOrDefault(path[0], VOID);
66+
if (sc instanceof GenericScope) // The first one has to be scope!
67+
{
68+
for (int i = 1; i < iLast; i++) // Subscope/forward lookup...
69+
if (!((sc = ((GenericScope<Object, ?>) sc).get(path[i])) instanceof GenericScope))
70+
{
71+
// LogProvider.instance.logErr("Value of path \"" + arg + "\" cannot be dereferenced because \"" + path[i] + "\" is not a scope but " + sc + "!", null);
72+
break backlook;
73+
}
74+
75+
obj = ((GenericScope<?, Object>) sc).variables().get(path[iLast]);
76+
break;
77+
}
78+
79+
if (sc != VOID) // = variable was defined in parent but it is not a scope, it means we want to break cos we can't deref that = treat the path as invalid (undefined)...
80+
{
81+
// LogProvider.instance.logErr("Value of path \"" + arg + "\" cannot be dereferenced because \"" + path[0] + "\" is not a scope but " + sc + "!", null);
82+
break;
83+
}
84+
}
85+
while ((scope = scope.getParent()) != null);
86+
}
87+
else
88+
{
89+
do
90+
{
91+
if ((obj = scope.variables().getOrDefault(str, VOID)) != VOID)
92+
break;
93+
}
94+
while ((scope = scope.getParent()) != null);
95+
}
96+
97+
if (obj == null || obj == VOID) // When was not found...
98+
return null;
99+
return clsModif ? obj.getClass() : newModif ? Clone(obj, scope instanceof Serializer ? ((Serializer) scope).getParsers() : DataParser.REGISTRY, new Object[] {}, new Scope()) : obj;
100+
}
101+
return CONTINUE;
102+
}
103+
}

SerialX-juss/src/main/java/org/ugp/serialx/juss/JussSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,13 +701,13 @@ public <T> T cloneOf(String variableName)
701701
* @param variable | Variable to clone!
702702
* @param defaultValue | Default value to return.
703703
*
704-
* @return Clone of value stored by variable with inserted name or defaultValue if there is no such a one!
704+
* @return Clone of value stored by variable with inserted name or defaultValue if there is no such a one or given key contains null!
705705
* <br><br>
706706
* Note: Cloning is done by {@link Serializer#Clone(Object, Registry, Object[], Object...))}!
707707
*
708708
* @since 1.3.2
709709
*/
710-
public <T> T cloneOf(String variableName , T defaultValue)
710+
public <T> T cloneOf(String variableName, T defaultValue)
711711
{
712712
T obj = get(variableName , defaultValue);
713713
if (obj == defaultValue)

0 commit comments

Comments
 (0)