Skip to content

Commit 7cce36b

Browse files
refactoring AritmeticOperators, GenericScope is now Collection!
1 parent 39fdb1f commit 7cce36b

File tree

9 files changed

+200
-96
lines changed

9 files changed

+200
-96
lines changed

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

Lines changed: 96 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
/**
25-
* This is some kind of hybrid between {@link List} and {@link Map} which allow you to have both variables and independent values managed by one Object. <br>
25+
* This collection is some sort of hybrid between {@link List} and {@link Map} which allow you to have both variables and independent values managed by one Object. <br>
2626
* Note: Variables are managed and accessed classically via {@link Map} methods such as <code>put(KeyT key, Object)</code> and array of independent values is accessed by via {@link List} methods such as <code>add(Object)</code> and <code>get(int)</code><br>
2727
* Also this is java representation of JUSS GenericScope group such as:
2828
* <pre>
@@ -35,12 +35,12 @@
3535
*
3636
* @author PETO
3737
*
38-
* @since 1.2.0
38+
* @since 1.3.5
3939
*
4040
* @param <KeyT> generic type of variables key.
4141
* @param <ValT> generic type of variables value and independent value.
4242
*/
43-
public class GenericScope<KeyT, ValT> implements Iterable<ValT>, Cloneable, Serializable
43+
public class GenericScope<KeyT, ValT> implements Collection<ValT>, Cloneable, Serializable
4444
{
4545
private static final long serialVersionUID = 5717775602991055386L;
4646

@@ -108,7 +108,7 @@ else if (obj instanceof Collection)
108108
else if (obj instanceof Map)
109109
return valuesCount() <= 0 && variables().equals(obj);
110110
else if (obj != null && obj.getClass().isArray())
111-
return variablesCount() <= 0 && Objects.deepEquals(toValArray(), Utils.fromAmbiguousArray(obj));
111+
return variablesCount() <= 0 && Objects.deepEquals(toArray(), Utils.fromAmbiguousArray(obj));
112112
return super.equals(obj);
113113
}
114114

@@ -365,12 +365,28 @@ public boolean containsVariable(KeyT variableKey)
365365
}
366366

367367
/**
368-
* @param value | Objecthe value.
368+
* @param value | Object the independent value.
369+
*
370+
* @return True if independent value was found in this scope.
371+
*
372+
* @since 1.3.7
373+
*/
374+
@Override
375+
public boolean contains(Object value)
376+
{
377+
return values().contains(value);
378+
}
379+
380+
/**
381+
* @deprecated USE {@link GenericScope#contains(Object)} instead!
382+
*
383+
* @param value | Object the value.
369384
*
370385
* @return True if independent value was found in this scope.
371386
*
372387
* @since 1.2.0
373388
*/
389+
@Deprecated
374390
public boolean containsIndependentValue(ValT value)
375391
{
376392
return values().contains(value);
@@ -417,6 +433,7 @@ public <V extends ValT> V get(int valueIndex, Class<V> cls) throws Exception
417433
*
418434
* @since 1.2.0
419435
*/
436+
@Override
420437
public boolean add(ValT value)
421438
{
422439
boolean result = values().add(value);
@@ -432,11 +449,12 @@ public boolean add(ValT value)
432449
*
433450
* @since 1.2.0
434451
*/
435-
public boolean addAll(Collection<ValT> values)
452+
@Override
453+
public boolean addAll(Collection<? extends ValT> values)
436454
{
437455
if (values.isEmpty())
438456
return false;
439-
return values().addAll(values);
457+
return values().addAll((Collection<? extends ValT>) values);
440458
}
441459

442460
/**
@@ -451,6 +469,45 @@ public boolean addAll(@SuppressWarnings("unchecked") ValT... values)
451469
return addAll(Arrays.asList(values));
452470
}
453471

472+
/**
473+
* @param values | Independent values to check.
474+
*
475+
* @return True all provided values are contained in this scope as independent values!
476+
*
477+
* @since 1.3.7
478+
*/
479+
@Override
480+
public boolean containsAll(Collection<?> values)
481+
{
482+
return values().containsAll(values);
483+
}
484+
485+
/**
486+
* @param values | Independent values to check.
487+
*
488+
* @return {@link Collection#removeAll(Collection)} for the independent values,,,
489+
*
490+
* @since 1.3.7
491+
*/
492+
@Override
493+
public boolean removeAll(Collection<?> values)
494+
{
495+
return values().removeAll(values);
496+
}
497+
498+
/**
499+
* @param values | Independent values to check.
500+
*
501+
* @return {@link Collection#retainAll(Collection)} for the independent values,,,
502+
*
503+
* @since 1.3.7
504+
*/
505+
@Override
506+
public boolean retainAll(Collection<?> values)
507+
{
508+
return values().retainAll(values);
509+
}
510+
454511
/**
455512
* @param scopeValueIndex | Index of sub-scopes value.
456513
*
@@ -521,7 +578,7 @@ public <T> T toObject(Class<T> objClass, ProtocolRegistry protocolsToUse) throws
521578
SerializationProtocol<T> pro = protocolsToUse == null ? null : protocolsToUse.GetProtocolFor(objClass, SerializationProtocol.MODE_DESERIALIZE);
522579
if (pro != null)
523580
{
524-
T obj = pro.unserialize(objClass, this.variablesCount() > 0 ? new Object[] {this} : this.toValArray());
581+
T obj = pro.unserialize(objClass, this.variablesCount() > 0 ? new Object[] {this} : this.toArray());
525582
if (obj != null)
526583
return obj;
527584
}
@@ -680,14 +737,27 @@ public ValT remove(int valueIndex)
680737
return values().remove(valueIndex < 0 ? valuesCount() + valueIndex : valueIndex);
681738
}
682739

740+
/**
741+
* @param independentValue | Independent value to remove!
742+
*
743+
* @return Value of variable that was removed!
744+
*
745+
* @since 1.3.7
746+
*/
747+
@Override
748+
public boolean remove(Object independentValue)
749+
{
750+
return values().remove(independentValue);
751+
}
752+
683753
/**
684754
* @param variableKey | Name of variable to remove!
685755
*
686756
* @return Value of variable that was removed!
687757
*
688-
* @since 1.3.2
758+
* @since 1.3.7
689759
*/
690-
public ValT remove(KeyT variableKey)
760+
public ValT removeVariable(KeyT variableKey)
691761
{
692762
return variables().remove(variableKey);
693763
}
@@ -699,6 +769,7 @@ public ValT remove(KeyT variableKey)
699769
*
700770
* @since 1.3.5
701771
*/
772+
@Override
702773
public void clear()
703774
{
704775
variables().clear();
@@ -712,7 +783,7 @@ public void clear()
712783
*/
713784
@SuppressWarnings("unchecked")
714785
public GenericScope<KeyT, ValT> inheritParent()
715-
{
786+
{
716787
GenericScope<?, ?> parent = getParent();
717788
if (parent != null)
718789
variables().putAll((Map<? extends KeyT, ? extends ValT>) parent.variables());
@@ -765,8 +836,11 @@ public int variablesCount()
765836

766837
/**
767838
* @return Total number of variables and independent values of this scope! (<code>vvaluesCount() + variablesCount()</code>)
839+
*
840+
* @since 1.3.7 (before 1.3.7 known as totalSize)
768841
*/
769-
public int totalSize()
842+
@Override
843+
public int size()
770844
{
771845
return valuesCount() + variablesCount();
772846
}
@@ -776,9 +850,10 @@ public int totalSize()
776850
*
777851
* @since 1.2.0
778852
*/
853+
@Override
779854
public boolean isEmpty()
780855
{
781-
return totalSize() <= 0;
856+
return size() <= 0;
782857
}
783858

784859
/**
@@ -838,22 +913,24 @@ public List<? extends ValT> toValList()
838913
* @return Primitive array with independent values of this {@link GenericScope}. These values have nothing to do with values of variables, they are independent!
839914
* Modifying this list will not affect this {@link GenericScope} object!
840915
*
841-
* @since 1.2.0
916+
* @since 1.3.7 (before 1.3.7 known as toValArray)
842917
*/
843-
public Object[] toValArray()
918+
@Override
919+
public Object[] toArray()
844920
{
845921
return values().toArray();
846922
}
847-
923+
848924
/**
849925
* @param vals | Array to store independent values into!
850926
*
851927
* @return Primitive array with independent values of this {@link GenericScope}. These values have nothing to do with values of variables, they are independent!
852928
* Modifying this list will not affect this {@link GenericScope} object!
853929
*
854-
* @since 1.3.5
930+
* @since 1.3.7 (before 1.3.7 known as toValArray)
855931
*/
856-
public <V extends ValT> V[] toValArray(V[] vals)
932+
@Override
933+
public <T> T[] toArray(T[] vals)
857934
{
858935
return values().toArray(vals);
859936
}
@@ -874,7 +951,7 @@ public List<Object> toUnifiedList()
874951
}
875952

876953
/**
877-
* @return Values of this scope. These are not the values of keys these are values that have no key. You can access them via {@link GenericScope#get(int)}!
954+
* @return Independent values of this scope. These are not the values of keys these are values that have no key. You can access them via {@link GenericScope#get(int)}!
878955
* Note: Editing this List will affect this scope!
879956
*
880957
* @since 1.2.0

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
/**
25-
* This is some kind of hybrid between {@link List} and {@link Map} which allow you to have both variables and independent values managed by one Object. <br>
25+
* This collection is some sort of hybrid between {@link List} and {@link Map} which allow you to have both variables and independent values managed by one Object. <br>
2626
* Note: Variables are managed and accessed classically via {@link Map} methods such as <code>put(String key, Object)</code> and array of independent values is accessed by via {@link List} methods such as <code>add(Object)</code> and <code>get(int)</code><br>
2727
* Also this is java representation of JUSS Scope group such as:
2828
* <pre>
@@ -1082,7 +1082,7 @@ public static <T> T intoNew(Class<T> objCls, GenericScope<? super String, ?> fro
10821082
if (objCls.isArray())
10831083
{
10841084
if (objCls.getComponentType() == Object.class)
1085-
return (T) fromScope.toValArray();
1085+
return (T) fromScope.toArray();
10861086

10871087
return (T) into(Array.newInstance(objCls.getComponentType(), fromScope.valuesCount()), fromScope, fieldNamesToUse);
10881088
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.ugp.serialx.protocols.SerializationProtocol.ProtocolRegistry;
3636

3737
/**
38-
* {@link org.ugp.serialx.Serializer} is powerful utility class that allows you to serialize any object in Java using custom data format compiled by recursive descent parser consisting of {@link DataParser}s.
38+
* {@link org.ugp.serialx.Serializer} is powerful abstract class that allows you to serialize any object in Java using custom data format compiled by recursive descent parser consisting of {@link DataParser}s.
3939
* This class itself is responsible for formating and managing input-output (IO) of content obtained from parsers and protocols as well as their management of their usage!
4040
* It is instance of {@link Scope} so we can say that this is scope that can serialize itself using system of already mentioned {@link DataParser} and {@link SerializationProtocol}!
4141
*

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,19 @@ public Object parse(String str, Object... args)
213213
*
214214
* @since 1.3.5
215215
*/
216-
public Object parse(String str, boolean returnAsStringIfNotFound, Class<?>[] ignore, Object... args)
216+
public Object parse(String str, boolean returnAsStringIfNotFound, Class<? extends DataParser> ignore, Object... args)
217217
{
218218
Object obj = null;
219219
if (parsingCache != null)
220220
for (DataParser parser : parsingCache)
221-
if (parser != null && (obj = parser.parse(this, str, args)) != CONTINUE)
221+
if (parser != null && (ignore == null || ignore != parser.getClass()) && (obj = parser.parse(this, str, args)) != CONTINUE)
222222
return obj;
223223

224-
registryLoop: for (int i = 0, size = size(); i < size; i++)
224+
for (int i = 0, size = size(); i < size; i++)
225225
{
226226
DataParser parser = get(i);
227-
if (ignore != null)
228-
for (Class<?> cls : ignore)
229-
if (cls == parser.getClass())
230-
continue registryLoop;
227+
if (ignore != null && ignore == parser.getClass())
228+
continue;
231229

232230
if ((obj = parser.parse(this, str, args)) != CONTINUE)
233231
{

SerialX-core/src/main/java/org/ugp/serialx/protocols/ScopeProtocol.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Object[] serialize(GenericScope<?, ?> object) throws Exception
3333
{
3434
if (objectClass == args[0].getClass())
3535
return (GenericScope<?, ?>) args[0];
36-
return objectClass.getConstructor(Map.class, Object[].class).newInstance(((GenericScope<?, ?>) args[0]).toVarMap(), ((GenericScope<?, ?>) args[0]).toValArray());
36+
return objectClass.getConstructor(Map.class, Object[].class).newInstance(((GenericScope<?, ?>) args[0]).toVarMap(), ((GenericScope<?, ?>) args[0]).toArray());
3737
}
3838
return objectClass.getConstructor(Map.class, Object[].class).newInstance(new HashMap<>(), args);
3939
}

SerialX-core/src/main/java/org/ugp/serialx/protocols/SerializationProtocol.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ public ProtocolRegistry clone()
293293
@Override
294294
public void add(int index, SerializationProtocol<?> element)
295295
{
296-
if (GetProtocolFor(element.applicableFor()) != null && element.applicableFor() != Object.class)
296+
SerializationProtocol<?> existing;
297+
if ((existing = GetProtocolFor(element.applicableFor(), element.getMode())) != null && element.applicableFor() == existing.applicableFor())
297298
LogProvider.instance.logErr("Protocol applicable for \"" + element.applicableFor().getName() + "\" is already registred!", null);
298299
addDuplicatively(index, element);
299300
}

SerialX-devtools/src/main/java/org/ugp/serialx/devtools/DebugParserRegistry.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public CharSequence toString(Object obj, Object... args) {
9898
}
9999

100100
@Override
101-
public Object parse(String str, boolean returnAsStringIfNotFound, Class<?>[] ignore, Object... args)
101+
public Object parse(String str, boolean returnAsStringIfNotFound, Class<? extends DataParser> ignore, Object... args)
102102
{
103103
int iterationIndex = 0;
104104
if (args.length > 99 && args[99] instanceof Integer)
@@ -112,7 +112,7 @@ public Object parse(String str, boolean returnAsStringIfNotFound, Class<?>[] ign
112112
for (int i = 0; i < parsingCache.length; i++)
113113
{
114114
DataParser parser = parsingCache[i];
115-
if (parser != null)
115+
if (parser != null && (ignore == null || ignore != parser.getClass()))
116116
{
117117
try
118118
{
@@ -133,13 +133,11 @@ public Object parse(String str, boolean returnAsStringIfNotFound, Class<?>[] ign
133133
}
134134
}
135135

136-
registryLoop: for (int i = 0, size = size(); i < size; i++)
136+
for (int i = 0, size = size(); i < size; i++)
137137
{
138138
DataParser parser = get(i);
139-
if (ignore != null)
140-
for (Class<?> cls : ignore)
141-
if (cls == parser.getClass())
142-
continue registryLoop;
139+
if (ignore != null && ignore == parser.getClass())
140+
continue;
143141

144142
try
145143
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public Object setMemberOperator(ParserRegistry myHomeRegistry, Object source, St
171171
if (source instanceof GenericScope)
172172
{
173173
if (val == VOID)
174-
return ((GenericScope<Object,?>) source).remove(member);
174+
return ((GenericScope<Object,?>) source).removeVariable(member);
175175
return ((GenericScope<Object, Object>) source).put(genericVar ? myHomeRegistry.parse(member, true, null, args) : member, val);
176176
}
177177
return VOID;

0 commit comments

Comments
 (0)