Package ch.njol.yggdrasil
Class Fields
java.lang.Object
ch.njol.yggdrasil.Fields
- All Implemented Interfaces:
Iterable<Fields.FieldContext>
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classHolds a field's name and value, and throwsStreamCorruptedExceptions if primitives or objects are used incorrectly. -
Constructor Summary
ConstructorsConstructorDescriptionFields()Creates an empty Fields object.Creates a fields object and initialises it with all non-transient and non-static fields of the given class and its superclasses.Creates a fields object and initialises it with all non-transient and non-static fields of the given object.Creates a fields object and initialises it with all non-transient and non-static fields of the given object. -
Method Summary
Modifier and TypeMethodDescriptionboolean<T> TgetAndRemoveObject(String field, Class<T> expectedType) <T> TgetAndRemovePrimitive(String field, Class<T> expectedType) static Collection<Field> Gets all serializable fields of the provided class, including superclasses.@Nullable Object<T> TgetPrimitive(String fieldID) <T> TgetPrimitive(String fieldID, Class<T> expectedType) booleaniterator()<T,R> R Maps the object stored in the given field through the provided function.<T,R> R mapPrimitive(String fieldID, Class<T> type, Function<T, R> function) Maps the primitive stored in the given field through the provided function.voidvoidputPrimitive(String fieldID, Object value) booleanremoveField(String fieldID) Removes a field and its value from this Fields object.voidSets all fields of the given Object to the values stored in this Fields object.voidDeprecated, for removal: This API element is subject to removal in a future version.static FieldssingletonObject(String fieldID, @Nullable Object value) Creates a Fields object with a single object field.static FieldssingletonPrimitive(String fieldID, Object value) Creates a Fields object with a single primitive field.intsize()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Fields
public Fields()Creates an empty Fields object. -
Fields
-
Fields
Creates a fields object and initialises it with all non-transient and non-static fields of the given class and its superclasses.- Parameters:
type- Some class- Throws:
NotSerializableException- If a field occurs more than once (i.e. if a class has a field with the same name as a field in one of its superclasses)
-
Fields
Creates a fields object and initialises it with all non-transient and non-static fields of the given object.- Parameters:
object- Some object- Throws:
NotSerializableException- If a field occurs more than once (i.e. if a class has a field with the same name as a field in one of its superclasses)
-
Fields
public Fields(Object object, @Nullable @Nullable Yggdrasil yggdrasil) throws NotSerializableException Creates a fields object and initialises it with all non-transient and non-static fields of the given object.- Parameters:
object- Some object- Throws:
NotSerializableException- If a field occurs more than once (i.e. if a class has a field with the same name as a field in one of its superclasses)
-
-
Method Details
-
singletonObject
Creates a Fields object with a single object field.- Parameters:
fieldID- The id of the fieldvalue- The value of the field- Returns:
- A Fields object with the given field defined
-
singletonPrimitive
Creates a Fields object with a single primitive field.- Parameters:
fieldID- The id of the fieldvalue- The value of the field- Returns:
- A Fields object with the given field defined
-
mapObject
public <T,R> R mapObject(String fieldID, Class<T> type, Function<T, R> function) throws StreamCorruptedExceptionMaps the object stored in the given field through the provided function.- Type Parameters:
T- The type of the fieldR- The return type of the mapping function- Parameters:
fieldID- The id of the fieldtype- The expected type of the fieldfunction- The mapping function- Returns:
- The result of applying the mapping function to the field's value
- Throws:
StreamCorruptedException- If the field does not exist or is of an unexpected type
-
mapPrimitive
public <T,R> R mapPrimitive(String fieldID, Class<T> type, Function<T, R> function) throws StreamCorruptedExceptionMaps the primitive stored in the given field through the provided function.- Type Parameters:
T- The type of the fieldR- The return type of the mapping function- Parameters:
fieldID- The id of the fieldtype- The expected type of the fieldfunction- The mapping function- Returns:
- The result of applying the mapping function to the field's value
- Throws:
StreamCorruptedException- If the field does not exist or is of an unexpected type
-
getFields
Gets all serializable fields of the provided class, including superclasses.- Parameters:
type- The class to get the fields of- Returns:
- All non-static and non-transient fields of the given class and its superclasses
- Throws:
NotSerializableException- If a field occurs more than once (i.e. if a class has a field with the same name as a field in one of its superclasses)
-
setFields
Sets all fields of the given Object to the values stored in this Fields object.- Parameters:
object- The object whose fields should be set- Throws:
YggdrasilException- If this was called on a Fields object not created by Yggdrasil itselfStreamCorruptedExceptionNotSerializableException
-
setFields
@Deprecated(since="2.3.0", forRemoval=true) public void setFields(Object object, Yggdrasil yggdrasil) throws StreamCorruptedException, NotSerializableException Deprecated, for removal: This API element is subject to removal in a future version. -
size
public int size()- Returns:
- The number of fields defined
-
putObject
-
putPrimitive
-
contains
- Parameters:
fieldID- A field's id- Returns:
- Whether the field is defined
-
hasField
-
getObject
- Throws:
StreamCorruptedException
-
getObject
@Nullable public <T> T getObject(String fieldID, Class<T> expectedType) throws StreamCorruptedException - Throws:
StreamCorruptedException
-
getPrimitive
- Throws:
StreamCorruptedException
-
getPrimitive
- Throws:
StreamCorruptedException
-
getAndRemoveObject
@Nullable public <T> T getAndRemoveObject(String field, Class<T> expectedType) throws StreamCorruptedException - Throws:
StreamCorruptedException
-
getAndRemovePrimitive
public <T> T getAndRemovePrimitive(String field, Class<T> expectedType) throws StreamCorruptedException - Throws:
StreamCorruptedException
-
removeField
Removes a field and its value from this Fields object.- Parameters:
fieldID- The id of the field to remove- Returns:
- Whether a field with the given name was actually defined
-
iterator
- Specified by:
iteratorin interfaceIterable<Fields.FieldContext>
-