Skip to content

Commit 6023552

Browse files
Protocol "mode" is now bitwise based on bit-packing
1 parent e8104b7 commit 6023552

File tree

6 files changed

+75
-59
lines changed

6 files changed

+75
-59
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,10 @@ public <C extends E> C get(Class<C> cls, boolean includeChildrens)
334334
for (int i = 0, size = size(); i < size; i++)
335335
{
336336
C elm = (C) get(i);
337-
Class<?> objCls = elm.getClass();
338-
if (objCls == cls)
337+
Class<?> objCls;
338+
if ((objCls = elm.getClass()) == cls)
339339
return elm;
340-
else if (includeChildrens && cls.isAssignableFrom(objCls))
340+
if (includeChildrens && cls.isAssignableFrom(objCls))
341341
obj = elm;
342342
}
343343
return obj;
@@ -371,7 +371,7 @@ public int indexOf(Class<? extends E> cls, boolean includeChildrens)
371371
Class<?> objCls = get(i).getClass();
372372
if (objCls == cls)
373373
return i;
374-
else if (includeChildrens && cls.isAssignableFrom(objCls))
374+
if (includeChildrens && cls.isAssignableFrom(objCls))
375375
index = i;
376376
}
377377
return index;

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public class ProtocolConverter implements DataConverter
7373

7474
protected Set<Class<?>> invokableClasses = new HashSet<>(Arrays.asList(Math.class, Scope.class, Double.class, Float.class, String.class));
7575

76+
protected long protocolType = 0;
77+
7678
@Override
7779
public Object parse(ParserRegistry myHomeRegistry, String str, Object... compilerArgs)
7880
{
@@ -153,7 +155,7 @@ protected Object parse(ParserRegistry myHomeRegistry, Class<?> objClass, String
153155
if (objArgs.length == 1 && objArgs[0] instanceof Scope && Scope.class.isAssignableFrom(objClass))
154156
return objArgs[0];
155157
compilerArgs[4] = oldObjectClass;
156-
return SerializationProtocol.unserializeObj(compilerArgs.length > 3 && compilerArgs[3] instanceof ProtocolRegistry ? (ProtocolRegistry) compilerArgs[3] : SerializationProtocol.REGISTRY, objClass, objArgs);
158+
return SerializationProtocol.unserializeObj(compilerArgs.length > 3 && compilerArgs[3] instanceof ProtocolRegistry ? (ProtocolRegistry) compilerArgs[3] : SerializationProtocol.REGISTRY, getProtocolType(), objClass, objArgs);
157159
}
158160
catch (Exception e)
159161
{
@@ -189,7 +191,7 @@ public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Serializ
189191
if (useBase64IfCan && obj instanceof Serializable)
190192
return CONTINUE;
191193

192-
if (preferedProtocol != null || (preferedProtocol = (SerializationProtocol<Object>) getProtocolFor(obj, SerializationProtocol.MODE_SERIALIZE, args)) != null)
194+
if (preferedProtocol != null || (preferedProtocol = (SerializationProtocol<Object>) getProtocolFor(obj, SerializationProtocol.MODE_SERIALIZE | getProtocolType(), args)) != null)
193195
{
194196
Class<?> oldObjectClass = null;
195197
try
@@ -240,11 +242,9 @@ public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Serializ
240242
@Override
241243
public CharSequence getDescription(ParserRegistry myHomeRegistry, Object obj, Object... argsUsedConvert)
242244
{
243-
if (obj instanceof Scope && ((Scope) obj).isEmpty())
244-
return "Empty scope!";
245-
else if (obj instanceof CharSequence && indexOfNotInObj((CharSequence) obj, '\n', '\r') != -1)
245+
if (obj instanceof CharSequence && indexOfNotInObj((CharSequence) obj, '\n', '\r') != -1)
246246
return "Multiline char sequence!";
247-
return new StringBuilder("Object of ").append(obj.getClass().getName()).append(": \"").append(obj.toString()).append("\" serialized using ").append(getProtocolFor(obj, SerializationProtocol.MODE_ALL, argsUsedConvert).toString()).append("!");
247+
return new StringBuilder("Object of ").append(obj.getClass().getName()).append(": \"").append(obj.toString()).append("\" serialized using ").append(getProtocolFor(obj, SerializationProtocol.MODE_SERIALIZE_DESERIALIZE | getProtocolType(), argsUsedConvert)).append("!");
248248
}
249249

250250
/**
@@ -276,6 +276,26 @@ public void setUseBase64IfCan(boolean useBase64IfCan)
276276
{
277277
this.useBase64IfCan = useBase64IfCan;
278278
}
279+
280+
/**
281+
* @return The additional protocol type that will chained with {@link SerializationProtocol#MODE_SERIALIZE} and {@link SerializationProtocol#MODE_DESERIALIZE} (none (0) by default).
282+
*
283+
* @since 1.3.8
284+
*/
285+
public long getProtocolType()
286+
{
287+
return protocolType;
288+
}
289+
290+
/**
291+
* @param protocolType | Value of additional protocol type that will chained with {@link SerializationProtocol#MODE_SERIALIZE} and {@link SerializationProtocol#MODE_DESERIALIZE} (none (0) by default).
292+
*
293+
* @since 1.3.8
294+
*/
295+
public void setProtocolType(long protocolType)
296+
{
297+
this.protocolType = protocolType;
298+
}
279299

280300
/**
281301
* @return Classes that are eligible for public static member invocation (:: operator)!<br>
@@ -298,7 +318,7 @@ public Set<Class<?>> getInvokableClasses()
298318
*
299319
* @since 1.3.5
300320
*/
301-
public static SerializationProtocol<?> getProtocolFor(Object obj, byte mode, Object[] args)
321+
public static SerializationProtocol<?> getProtocolFor(Object obj, long mode, Object[] args)
302322
{
303323
if (args.length > 3)
304324
{

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

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public abstract class SerializationProtocol<T>
2626
/**
2727
* This is serialization protocol registry. This is place where your {@link SerializationProtocol} implementations should be registered in order to work properly! Do not add there two protocols applicable for exactly same classes!
2828
* Also I recommend to register protocols in generic order of object that are they applicable for! In other words If some object Foo has child classes, protocol of these classes should be registered after each other.
29-
* Defaultly there are registered protocols from ugp.org.SerialX.protocols.
29+
* Defaultly there are registered protocols from org.ugp.serialx.protocols.
3030
*
3131
* @since 1.3.0
3232
*/
@@ -37,30 +37,30 @@ public abstract class SerializationProtocol<T>
3737
*
3838
* @since 1.3.5
3939
*/
40-
public static final byte MODE_SERIALIZE = 0;
40+
public static final long MODE_SERIALIZE = 0b01;
4141

4242
/**
4343
* This mode is for protocols that are used for deserialization only!
4444
*
4545
* @since 1.3.5
4646
*/
47-
public static final byte MODE_DESERIALIZE = 1;
47+
public static final long MODE_DESERIALIZE = 0b10;
4848

4949
/**
50-
* This mode is for protocols that chat can both serialize and deserialize!
50+
* This mode is for protocols that chat can both serialize and deserialize (<code>MODE_SERIALIZE | MODE_DESERIALIZE</code>)!
5151
*
52-
* @since 1.3.5
52+
* @since 1.3.8 (originally known as MODE_ALL)
5353
*/
54-
public static final byte MODE_ALL = 2;
54+
public static final long MODE_SERIALIZE_DESERIALIZE = MODE_SERIALIZE | MODE_DESERIALIZE;
5555

5656
protected boolean isActive = true;
5757

5858
@Override
59-
public boolean equals(Object obj)
59+
public boolean equals(Object obj)
6060
{
6161
if (obj instanceof Class)
6262
return applicableFor().equals(obj);
63-
else if (obj instanceof SerializationProtocol)
63+
if (obj instanceof SerializationProtocol)
6464
return applicableFor().equals(((SerializationProtocol<?>) obj).applicableFor());
6565
return super.equals(obj);
6666
}
@@ -128,17 +128,16 @@ public T unserialize(Class<? extends T> objectClass, Scope scope) throws Excepti
128128
public abstract Class<? extends T> applicableFor();
129129

130130
/**
131-
* @return Mode of this protocol. Default is {@link SerializationProtocol#MODE_ALL}!
131+
* @return Mode of this protocol, describing its type or operation mode. Can be one or more chained with binary or. Default is {@link SerializationProtocol#MODE_SERIALIZE_DESERIALIZE}!
132132
*
133-
* @see SerializationProtocol#MODE_ALL
134133
* @see SerializationProtocol#MODE_DESERIALIZE
135134
* @see SerializationProtocol#MODE_SERIALIZE
136135
*
137136
* @since 1.3.5
138137
*/
139-
public byte getMode()
138+
public long getMode()
140139
{
141-
return MODE_ALL;
140+
return MODE_SERIALIZE_DESERIALIZE;
142141
}
143142

144143
/**
@@ -174,21 +173,22 @@ public void setActive(boolean isActive)
174173
*/
175174
public static <O> Object[] serializeObj(O object) throws Exception
176175
{
177-
return serializeObj(REGISTRY, object);
176+
return serializeObj(REGISTRY, 0, object);
178177
}
179178

180179
/**
181180
* @param registry | Registry to use!
181+
* @param aditionalType | Additional protocol mode that will be chained together with {@link SerializationProtocol#MODE_DESERIALIZE}.
182182
* @param object | The object.
183183
*
184184
* @return Array of objects to serialize created from given object. Object will be serialized via protocol picked from registry.
185185
* {@link SerializationProtocol#serialize(Class, Object...)} method of picked protocol will be called! Null will be returned if no protocol was found and you will be prompted with error message!
186186
*
187187
* @since 1.3.0
188188
*/
189-
public static <O> Object[] serializeObj(ProtocolRegistry registry, O object) throws Exception
189+
public static <O> Object[] serializeObj(ProtocolRegistry registry, long aditionalType, O object) throws Exception
190190
{
191-
SerializationProtocol<O> prot = registry.GetProtocolFor(object, MODE_SERIALIZE);
191+
SerializationProtocol<O> prot = registry.GetProtocolFor(object, MODE_SERIALIZE | aditionalType);
192192
if (prot == null)
193193
{
194194
LogProvider.instance.logErr("Unable to serialize \"" + object + "\" because there is no registered and active protocol for serializing " + object.getClass() + "!", null);
@@ -210,11 +210,12 @@ public static <O> Object[] serializeObj(ProtocolRegistry registry, O object) thr
210210
*/
211211
public static <O> O unserializeObj(Class<? extends O> objectClass, Object... args) throws Exception
212212
{
213-
return unserializeObj(REGISTRY, objectClass, args);
213+
return unserializeObj(REGISTRY, 0, objectClass, args);
214214
}
215215

216216
/**
217217
* @param registry | Registry to use!
218+
* @param aditionalType | Additional protocol mode that will be chained together with {@link SerializationProtocol#MODE_DESERIALIZE}.
218219
* @param objectClass | The class of object that should be created. This can be useful when object {@link O} has children classes with same constructors. You can use reflection to make protocol working also for these child classes! This class is also used to pick suitable protocol!
219220
* @param args | Args to create obj {@link T} from.
220221
*
@@ -226,9 +227,9 @@ public static <O> O unserializeObj(Class<? extends O> objectClass, Object... arg
226227
* @since 1.3.0
227228
*/
228229
@SuppressWarnings("unchecked")
229-
public static <O> O unserializeObj(ProtocolRegistry registry, Class<? extends O> objectClass, Object... args) throws Exception
230+
public static <O> O unserializeObj(ProtocolRegistry registry, long aditionalType, Class<? extends O> objectClass, Object... args) throws Exception
230231
{
231-
SerializationProtocol<O> prot = (SerializationProtocol<O>) registry.GetProtocolFor(objectClass, MODE_DESERIALIZE);
232+
SerializationProtocol<O> prot = (SerializationProtocol<O>) registry.GetProtocolFor(objectClass, MODE_DESERIALIZE | aditionalType);
232233
if (prot == null)
233234
{
234235
LogProvider.instance.logErr("Unable to unserialize " + Arrays.toString(args) + " because there is no registered and active protocol for unserializing \"" + objectClass + "\"!", null);
@@ -302,68 +303,65 @@ public void add(int index, SerializationProtocol<?> element)
302303
/**
303304
* @param protocolsClass | Protocols class.
304305
*
305-
* @return Protocol by its class.
306+
* @return Protocol by its class. Pretty much equivalent with {@link Registry#get(Class)}.
306307
*
307308
* @since 1.0.0
308309
*/
309310
public SerializationProtocol<?> getProtocolByClass(Class<? extends SerializationProtocol<?>> protocolsClass)
310311
{
311-
for (SerializationProtocol<?> p : this)
312-
if (p.getClass().equals(protocolsClass))
313-
return p;
314-
return null;
312+
return get(protocolsClass);
315313
}
316314

317315
/**
318-
* @return Sublist of protocols that are active and can be used.
316+
* @return Sublist of any protocols that are active and can be used.
319317
*
320318
* @since 1.0.0
321319
*/
322320
public List<SerializationProtocol<?>> GetActiveProtocols()
323321
{
324-
return GetActiveProtocols(MODE_ALL);
322+
return GetActiveProtocols(-1);
325323
}
326324

327325
/**
328-
* @return Sublist of protocols that are not active and can't be used.
326+
* @return Sublist of any protocols that are not active and can't be used.
329327
*
330328
* @since 1.0.0
331329
*/
332330
public List<SerializationProtocol<?>> GetDeactivatedProtocols()
333331
{
334-
return GetDeactivatedProtocols(MODE_ALL);
332+
return GetDeactivatedProtocols(-1);
335333
}
336334

337335
/**
338-
* @param mode | Mode of protocol to find.
336+
* @param mode | Mode of protocols to find, or -1 if mode does not matter.
339337
*
340338
* @return Sublist of protocols that are active and can be used according to mode.
341339
*
342340
* @since 1.3.5
343341
*/
344-
public List<SerializationProtocol<?>> GetActiveProtocols(byte mode)
342+
public List<SerializationProtocol<?>> GetActiveProtocols(long mode)
345343
{
346344
List<SerializationProtocol<?>> resault = new ArrayList<>();
347345

348346
for (SerializationProtocol<?> p : this)
349-
if (p.isActive() && (p.getMode() == 2 || p.getMode() == mode))
347+
if (p.isActive() && (p.getMode() & mode) != 0)
350348
resault.add(p);
351349
return resault;
352350
}
353351

354352
/**
355-
* @param mode | Mode of protocol to find.
353+
* @param mode | Mode of protocols to find, or -1 if mode does not matter.
356354
*
357355
* @return Sublist of protocols that are not active and can't be used according to mode.
358356
*
359357
* @since 1.3.5
360358
*/
361-
public List<SerializationProtocol<?>> GetDeactivatedProtocols(byte mode)
359+
public List<SerializationProtocol<?>> GetDeactivatedProtocols(long mode)
362360
{
363361
List<SerializationProtocol<?>> resault = new ArrayList<>();
364362

365363
for (SerializationProtocol<?> p : this)
366-
if (!p.isActive() && (p.getMode() == 2 || p.getMode() == mode))
364+
if (!p.isActive() && (p.getMode() & mode) != 0)
367365
resault.add(p);
368366
return resault;
369367
}
@@ -378,7 +376,7 @@ public List<SerializationProtocol<?>> GetDeactivatedProtocols(byte mode)
378376
*/
379377
public <O> SerializationProtocol<O> GetProtocolFor(O obj)
380378
{
381-
return GetProtocolFor(obj, MODE_ALL);
379+
return GetProtocolFor(obj, MODE_SERIALIZE_DESERIALIZE);
382380
}
383381

384382
/**
@@ -391,46 +389,46 @@ public <O> SerializationProtocol<O> GetProtocolFor(O obj)
391389
*/
392390
public <O> SerializationProtocol<O> GetProtocolFor(Class<? extends O> applicableFor)
393391
{
394-
return GetProtocolFor(applicableFor, MODE_ALL);
392+
return GetProtocolFor(applicableFor, MODE_SERIALIZE_DESERIALIZE);
395393
}
396394

397395
/**
398396
* @param <O>
399397
* @param obj | Object that is required protocol applicable for.
400-
* @param mode | Mode of protocol to find.
398+
* @param mode | Mode of protocols to find, or -1 if mode does not matter.
401399
*
402400
* @return Protocol applicable for required objects class according to mode or null if there is no such an active protocol!
403401
*
404402
* @since 1.3.5
405403
*/
406404
@SuppressWarnings("unchecked")
407-
public <O> SerializationProtocol<O> GetProtocolFor(O obj, byte mode)
405+
public <O> SerializationProtocol<O> GetProtocolFor(O obj, long mode)
408406
{
409407
return (SerializationProtocol<O>) GetProtocolFor(obj.getClass(), mode);
410408
}
411409

412410
/**
413411
* @param <O>
414412
* @param applicableFor | Class of object that is protocol applicable for.
415-
* @param mode | Mode of protocol to find.
413+
* @param mode | Mode of protocols to find, or -1 if mode does not matter.
416414
*
417415
* @return Protocol applicable for required class according to mode or null if there is no such an active protocol!
418416
*
419417
* @since 1.3.5
420418
*/
421419
@SuppressWarnings("unchecked")
422-
public <O> SerializationProtocol<O> GetProtocolFor(Class<? extends O> applicableFor, byte mode)
420+
public <O> SerializationProtocol<O> GetProtocolFor(Class<? extends O> applicableFor, long mode)
423421
{
424422
SerializationProtocol<O> protocol = null;
425-
for (int i = 0, len = size(), myMode = 0; i < len; i++)
423+
for (int i = 0, len = size(); i < len; i++)
426424
{
427425
SerializationProtocol<?> p = get(i);
428-
if (p.isActive() && ((myMode = p.getMode()) == 2 || myMode == mode))
426+
if (p.isActive() && (p.getMode() & mode) != 0)
429427
{
430428
Class<?> applicable = p.applicableFor();
431429
if (applicable == applicableFor)
432430
return (SerializationProtocol<O>) p;
433-
else if (applicable.isAssignableFrom(applicableFor))
431+
if (applicable.isAssignableFrom(applicableFor))
434432
protocol = (SerializationProtocol<O>) p;
435433
}
436434
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ public CharSequence getDescription(ParserRegistry myHomeRegistry, Object obj, Ob
190190
{
191191
if (obj instanceof Scope && ((Scope) obj).isEmpty())
192192
return "Empty scope!";
193-
else if (obj instanceof CharSequence && indexOfNotInObj((CharSequence) obj, '\n', '\r') != -1)
194-
return "Multiline char sequence!";
195-
return new StringBuilder("Object of ").append(obj.getClass().getName()).append(": \"").append(obj.toString()).append("\" serialized using ").append(getProtocolFor(obj, SerializationProtocol.MODE_ALL, argsUsedConvert).toString()).append("!");
193+
return super.getDescription(myHomeRegistry, obj, argsUsedConvert);
196194
}
197195

198196
/**

SerialX-juss/src/main/java/org/ugp/serialx/juss/protocols/SelfSerializableProtocol.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public Object[] serialize(T object)
2929
}
3030

3131
@Override
32-
public byte getMode()
32+
public long getMode()
3333
{
34-
return MODE_ALL;
34+
return MODE_SERIALIZE_DESERIALIZE;
3535
}
3636
}
3737

SerialX-juss/src/main/java/org/ugp/serialx/juss/protocols/UniversalObjectInstantiationProtocol.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Class<? extends T> applicableFor()
9292
}
9393

9494
@Override
95-
public byte getMode()
95+
public long getMode()
9696
{
9797
return MODE_DESERIALIZE;
9898
}

0 commit comments

Comments
 (0)