Skip to content

Commit 402a77d

Browse files
begining refactor of VarConver...
1 parent 8ceb5de commit 402a77d

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ public <V extends ValT> V get(KeyT variableKey, V defaultValue)
286286
* @return Value of variable at given path. If no path is given (length is 0) then this {@link GenericScope} will be returned.<br>
287287
* If 1 path argument is given then this behaves similarly to {@link GenericScope#get(Object)}.<br>
288288
* if 2+ path arguments are given then it will search the sub-scopes and return first match value. For example:<br>
289+
* In either way, if there is no suitable result then null will be returned!<br>
289290
* Consider this scope tree:<br>
290291
* <pre>
291292
* <code>
@@ -361,10 +362,7 @@ public <V extends ValT> V get(KeyT variableKey, Class<V> cls, V defaultValue) th
361362
*/
362363
public boolean containsVariable(KeyT variableKey)
363364
{
364-
for (Map.Entry<KeyT, ValT> ent : varEntrySet())
365-
if (ent.getKey().equals(variableKey))
366-
return true;
367-
return false;
365+
return variables().containsKey(variableKey);
368366
}
369367

370368
/**
@@ -792,9 +790,9 @@ public boolean isEmpty()
792790
*
793791
* @since 1.2.0
794792
*/
795-
public GenericScope<?, ?> getParent()
793+
public <T extends GenericScope<?, ?>> T getParent()
796794
{
797-
return getParent(1);
795+
return (T) getParent(1);
798796
}
799797

800798
/**
@@ -806,15 +804,15 @@ public boolean isEmpty()
806804
*
807805
* @since 1.3.2
808806
*/
809-
public GenericScope<?, ?> getParent(int depth)
807+
public <T extends GenericScope<?, ?>> T getParent(int depth)
810808
{
811809
if (depth < 1)
812-
return this;
810+
return (T) this;
813811

814812
GenericScope<?, ?> parentsParent;
815813
if (depth == 1 || parent == null || (parentsParent = parent.getParent(--depth)) == null)
816-
return parent;
817-
return parentsParent;
814+
return (T) parent;
815+
return (T) parentsParent;
818816
}
819817

820818
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ public static void post(Serializer serializer, HttpURLConnection conn) throws IO
855855
*
856856
* @since 1.2.2
857857
*/
858-
public static final class NULL
858+
public static final class NULL //TODO: REMOVE!!!!
859859
{
860860
public static Object toOopNull(Object obj)
861861
{
@@ -871,7 +871,7 @@ public boolean equals(Object obj)
871871
@Override
872872
public String toString()
873873
{
874-
return "null";
874+
return "NULL";
875875
}
876876
}
877877
}

SerialX-juss/src/main/java/org/ugp/serialx/juss/converters/VariableConverter.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import org.ugp.serialx.GenericScope;
1515
import org.ugp.serialx.LogProvider;
1616
import org.ugp.serialx.Scope;
17+
import org.ugp.serialx.Utils;
1718
import org.ugp.serialx.Utils.NULL;
1819
import org.ugp.serialx.converters.DataConverter;
20+
import org.ugp.serialx.converters.DataParser;
1921

2022
/**
2123
* This converter is capable of converting {@link Map.Entry} and reading variables from {@link Scope} via "$"!
@@ -70,7 +72,7 @@ public Object parse(ParserRegistry myHomeRegistry, String arg, Object... args)
7072

7173
if (enrty.length > 1 && !objString.isEmpty())
7274
{
73-
obj = NULL.toOopNull(myHomeRegistry.parse(objString, args));
75+
obj = myHomeRegistry.parse(objString, args);
7476
}
7577

7678
for (int i = 0; i < enrty.length-1; i++)
@@ -105,12 +107,38 @@ else if (obj == VOID)
105107
}
106108
else if (arg.charAt(0) == '$' && !contains(arg, ' ', '+', '-', '*', '/', '%', '>', '<', '=', '&', '|', '^', '?', '='))
107109
{
110+
// Object obj;
111+
// if ((arg = fastReplace(arg, "$", "")).indexOf('.') > -1)
112+
// {
113+
// Object[] path = splitValues(fastReplace(fastReplace(arg, "::new", ""), "::class", ""), '.');
114+
// GenericScope<Object, Object> sc = (GenericScope<Object, Object>) scope.getGenericScope(Arrays.copyOfRange(path, 0, path.length-1)); //TODO: Prevent neccesity of scope parent inheritance.
115+
// obj = sc == null ? null : sc.variables().get(path[path.length-1]);
116+
// /*if (sc == null || !sc.containsVariable(tree[tree.length-1]))
117+
// LogProvider.instance.logErr("Variable \"" + tree[tree.length-1] + "\" was not declared in \"" + arg.substring(0, arg.length() - tree[tree.length-1].length() - 1) + "\"! Defaulting to null!");*/
118+
// }
119+
// else
120+
// {
121+
// String str = fastReplace(fastReplace(arg, "::new", ""), "::class", "");
122+
// /*if (!scope.containsVariable(str))
123+
// LogProvider.instance.logErr("Variable \"" + str + "\" was not declared! Defaulting to null!");*/
124+
// obj = scope.variables().get(str);
125+
// }
126+
108127
Object obj;
109128
if ((arg = fastReplace(arg, "$", "")).indexOf('.') > -1)
110129
{
111-
Object[] tree = splitValues(fastReplace(fastReplace(arg, "::new", ""), "::class", ""), '.');
112-
GenericScope<Object, Object> sc = (GenericScope<Object, Object>) scope.getGenericScope(Arrays.copyOfRange(tree, 0, tree.length-1)); //TODO: Prevent neccesity of scope parent inheritance.
113-
obj = sc == null ? null : sc.variables().get(tree[tree.length-1]);
130+
Object[] path = splitValues(fastReplace(fastReplace(arg, "::new", ""), "::class", ""), '.');
131+
if ((obj = scope.get(path)) == null && !scope.variables().containsKey(path[0]))
132+
{
133+
for (GenericScope<Object, Object> parent = scope.getParent(); parent != null; parent = parent.getParent())
134+
if (parent.variables().containsKey(path[0]))
135+
{
136+
obj = parent.get(path);
137+
break;
138+
}
139+
}
140+
141+
// GenericScope<Object, Object> sc = (GenericScope<Object, Object>) scope.getGenericScope(Arrays.copyOfRange(path, 0, path.length-1)); //TODO: Prevent neccesity of scope parent inheritance.
114142
/*if (sc == null || !sc.containsVariable(tree[tree.length-1]))
115143
LogProvider.instance.logErr("Variable \"" + tree[tree.length-1] + "\" was not declared in \"" + arg.substring(0, arg.length() - tree[tree.length-1].length() - 1) + "\"! Defaulting to null!");*/
116144
}
@@ -119,9 +147,16 @@ else if (arg.charAt(0) == '$' && !contains(arg, ' ', '+', '-', '*', '/', '%', '>
119147
String str = fastReplace(fastReplace(arg, "::new", ""), "::class", "");
120148
/*if (!scope.containsVariable(str))
121149
LogProvider.instance.logErr("Variable \"" + str + "\" was not declared! Defaulting to null!");*/
122-
obj = scope.variables().get(str);
150+
if ((obj = scope.variables().getOrDefault(str, VOID)) == VOID)
151+
{
152+
for (GenericScope<Object, Object> parent = scope.getParent(); parent != null; parent = parent.getParent())
153+
if ((obj = parent.variables().getOrDefault(str, VOID)) != VOID)
154+
break;
155+
}
123156
}
124157

158+
if (obj == null || obj == VOID) // Was not found...
159+
return null;
125160
return arg.endsWith("::class") ? obj.getClass() : arg.endsWith("::new") ? Clone(obj) : obj;
126161
}
127162
}

0 commit comments

Comments
 (0)