Class ConditionalEntityWrapper

java.lang.Object
ch.tocco.nice2.persist.core.api.entity.entitywrappers.AbstractEntityWrapper
ch.tocco.nice2.persist.core.api.entity.entitywrappers.ConditionalEntityWrapper
All Implemented Interfaces:
EntityReference, Entity
Direct Known Subclasses:
NullCounterEntityWrapper, NullKeyEntityWrapper

public abstract class ConditionalEntityWrapper extends AbstractEntityWrapper
A wrapper around an entity that may return a different value for fields based on a condition.

Implementations must provide the 3 methods isCondition(ch.tocco.nice2.model.entity.api.FieldModel), getSubstitute(ch.tocco.nice2.model.entity.api.FieldModel, ch.tocco.nice2.types.api.Type<T>) and getKey().

  • Constructor Details

    • ConditionalEntityWrapper

      public ConditionalEntityWrapper(Entity entity)
  • Method Details

    • isCondition

      protected abstract boolean isCondition(FieldModel fieldModel)
      Checks the condition on a field.
      Parameters:
      fieldModel - The field for which the condition is checked.
      Returns:
      true if your condition is true, and thus the value is overridden by calling getSubstitute(ch.tocco.nice2.model.entity.api.FieldModel, ch.tocco.nice2.types.api.Type<T>).
      See Also:
    • getSubstitute

      @Nullable protected abstract <T> T getSubstitute(FieldModel fieldModel, Type<T> type)
      Requests the substitute value on a field.
      Type Parameters:
      T - The data type of the field.
      Parameters:
      fieldModel - The field for which the substitute is asked.
      type - The data type of the field. Can also be accessed by fieldModel.getType(), but is required as param for the generics (checking correct method return type).
      Returns:
      The substitute value, or pass through to the wrapped entity using entity.getValue(fieldModel, type);
      See Also:
    • getKey

      @Nullable public abstract @Nullable PrimaryKey getKey()
      Implementing this may be tricky. Decide for yourself if you
      • just pass through to the wrapped entity using entity.getKey();
      • or if you blank it all out by returning null
      • or if you try to just modify parts of it.
      I suggest you write a sentence about the reasons for your decision.
      Specified by:
      getKey in interface Entity
      Specified by:
      getKey in interface EntityReference
      Overrides:
      getKey in class AbstractEntityWrapper
      Returns:
      The primary key of this entity, or null if in CONCEPTION state.
      See Also:
    • getValue

      public final TypedValue<?> getValue(String field)
      Description copied from interface: Entity
      Get the value and (nominal) type of a field. If the value of the specified field is null, typed value with the field type and an associated value of null is returned.
      Specified by:
      getValue in interface Entity
      Overrides:
      getValue in class AbstractEntityWrapper
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
    • getValue

      public final <T> T getValue(String field, Class<T> targetType)
      Description copied from interface: Entity
      Get the value of a field, converting it if necessary.
      Specified by:
      getValue in interface Entity
      Overrides:
      getValue in class AbstractEntityWrapper
      Type Parameters:
      T - The target Java type.
      Parameters:
      field - The name of a field.
      targetType - The target Java type.
      Returns:
      The value of the field.
    • getValue

      public final <T> T getValue(String field, Type<T> type)
      Description copied from interface: Entity
      Get the value of a field as the specified type, converting the value if neccessary.
      Specified by:
      getValue in interface Entity
      Overrides:
      getValue in class AbstractEntityWrapper
      Parameters:
      field - The name of the Field
      type - The target type.
      Returns:
      The value.
    • getValue

      public final TypedValue<?> getValue(FieldModel field)
      Description copied from interface: Entity
      Get the value and (nominal) type of a field. If the underlying value is null, it returns the type associated with the value of null.
      Specified by:
      getValue in interface Entity
      Overrides:
      getValue in class AbstractEntityWrapper
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
    • getValue

      @Nullable public final <T> T getValue(FieldModel field, Class<T> targetType)
      Description copied from interface: Entity
      Get the value of a field, converting it if necessary.
      Specified by:
      getValue in interface Entity
      Overrides:
      getValue in class AbstractEntityWrapper
      Type Parameters:
      T - The target Java type.
      Parameters:
      field - The name of a field.
      targetType - The target Java type.
      Returns:
      The value of the field.
    • getValue

      @Nullable public final <T> T getValue(FieldModel fieldModel, Type<T> type)
      Description copied from interface: Entity
      Get the value of a field as the specified type, converting the value if neccessary.
      Specified by:
      getValue in interface Entity
      Overrides:
      getValue in class AbstractEntityWrapper
      Parameters:
      fieldModel - The field model.
      type - The target type.
      Returns:
      The value.
    • getValueMap

      public final Map<String,Object> getValueMap()
      Description copied from interface: Entity
      Get all values of the entity as a map.

      Changes to that map will not be reflected in the entity.

      The data includes pk fields as well as nice-specific fields (version etc).

      Specified by:
      getValueMap in interface Entity
      Overrides:
      getValueMap in class AbstractEntityWrapper
      Returns:
      A map containing all values of the entity (shallow copy, modifiable).