Class ObjectAccessor<T>
- Type Parameters:
T
- The specified object's class.
-
Method Summary
Modifier and TypeMethodDescriptionabstract ObjectAccessor<T>
clear
(Predicate<Field> canBeDefault, PrefabValues prefabValues, TypeTag enclosingType) Clears all fields of the wrapped object to their default values, but only ifcanBeDefault
for the given field returns true.abstract T
copy()
Creates a copy of the wrapped object.abstract T
Creates a copy of the wrapped object, where the copy type is an anonymous subclass of the wrapped object's class.abstract <S extends T>
ScopyIntoSubclass
(Class<S> subclass) Creates a copy of the wrapped object, where the copy's type is a specified subclass of the wrapped object's class.get()
Returns the wrapped object.Returns the value of the given field.static <T> ObjectAccessor<T>
of
(T object) Factory method.static <T> ObjectAccessor<T>
Factory method.abstract ObjectAccessor<T>
scramble
(PrefabValues prefabValues, TypeTag enclosingType) Modifies all fields of the wrapped object that are declared in T and in its superclasses.abstract ObjectAccessor<T>
shallowScramble
(PrefabValues prefabValues, TypeTag enclosingType) Modifies all fields of the wrapped object that are declared in T, but not those inherited from superclasses.type()
Returns the type of the object.abstract ObjectAccessor<T>
withChangedField
(Field field, PrefabValues prefabValues, TypeTag enclosingType) Changes the given field of the wrapped object to some unspecified, but different value.abstract ObjectAccessor<T>
withDefaultedField
(Field field) Clears the given field of the wrapped object to its default value.abstract ObjectAccessor<T>
withFieldSetTo
(Field field, Object newValue) Changes the given field of the wrapped object to the given value.
-
Method Details
-
of
Factory method.- Type Parameters:
T
-object
's type.- Parameters:
object
- The object to wrap.- Returns:
- An
ObjectAccessor
forobject
.
-
of
Factory method.- Type Parameters:
T
-object
's type, or a supertype.- Parameters:
object
- The object to wrap.type
- Superclass ofobject
's type, as which it will be treated byObjectAccessor
.- Returns:
- An
ObjectAccessor
forobject
.
-
get
Returns the wrapped object.- Returns:
- The wrapped object.
-
type
Returns the type of the object.- Returns:
- The type of the object.
-
getField
Returns the value of the given field.- Parameters:
field
- The field whose value we want to get.- Returns:
- The value of the given field.
-
copy
Creates a copy of the wrapped object.Note: it does a "shallow" copy. Reference fields are not copied recursively.
- Returns:
- A shallow copy.
-
copyIntoSubclass
Creates a copy of the wrapped object, where the copy's type is a specified subclass of the wrapped object's class.Note: it does a "shallow" copy. Reference fields are not copied recursively.
- Type Parameters:
S
- The subclass.- Parameters:
subclass
- A subclass of the wrapped object's class.- Returns:
- A shallow copy.
-
copyIntoAnonymousSubclass
Creates a copy of the wrapped object, where the copy type is an anonymous subclass of the wrapped object's class.Note: it does a "shallow" copy. Reference fields are not copied recursively.
- Returns:
- A shallow copy.
-
scramble
Modifies all fields of the wrapped object that are declared in T and in its superclasses. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.This method is consistent: given two equal objects; after scrambling both objects, they remain equal to each other.
It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may be left unmodified.
- Parameters:
prefabValues
- Prefabricated values to take values from.enclosingType
- Describes the type that contains this object as a field, to determine any generic parameters it may contain.- Returns:
- An accessor to the scrambled object.
-
shallowScramble
Modifies all fields of the wrapped object that are declared in T, but not those inherited from superclasses. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.This method is consistent: given two equal objects; after scrambling both objects, they remain equal to each other.
It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may be left unmodified.
- Parameters:
prefabValues
- Prefabricated values to take values from.enclosingType
- Describes the type that contains this object as a field, to determine any generic parameters it may contain.- Returns:
- An accessor to the scrambled object.
-
clear
public abstract ObjectAccessor<T> clear(Predicate<Field> canBeDefault, PrefabValues prefabValues, TypeTag enclosingType) Clears all fields of the wrapped object to their default values, but only ifcanBeDefault
for the given field returns true. Otherwise, leaves the value intact. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may be left unmodified.
- Parameters:
canBeDefault
- A predicate that determines for the wrapped object's fields whether or not they are allowed to be 'default', i.e. 0 or null. If a field is marked with @NonNull, for example, it may not be default.prefabValues
- Prefabricated values to take values from.enclosingType
- Describes the type that contains this object as a field, to determine any generic parameters it may contain.- Returns:
- An accessor to the cleared object.
-
withDefaultedField
Clears the given field of the wrapped object to its default value. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may be left unmodified.
- Parameters:
field
- The field to set to its default value.- Returns:
- An accessor to the object with the defaulted field.
-
withChangedField
public abstract ObjectAccessor<T> withChangedField(Field field, PrefabValues prefabValues, TypeTag enclosingType) Changes the given field of the wrapped object to some unspecified, but different value. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may be left unmodified.
- Parameters:
field
- The field to set to a different value.prefabValues
- Prefabricated values to take values from.enclosingType
- Describes the type that contains this object as a field, to determine any generic parameters it may contain.- Returns:
- An accessor to the object with the changed field.
-
withFieldSetTo
Changes the given field of the wrapped object to the given value. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may be left unmodified.
- Parameters:
field
- The field to set to the given value.newValue
- The value to set the field to.- Returns:
- An accessor to the object with the defaulted field.
-