Class PrimaryKey

java.lang.Object
ch.tocco.nice2.model.entity.api.PrimaryKey
All Implemented Interfaces:
Serializable

public class PrimaryKey extends Object implements Serializable
A simple helper class that's used for identifying entities of a known type.
See Also:
  • Constructor Details

    • PrimaryKey

      public PrimaryKey(Serializable value)
      Constructs a Primary Key from an object.

      WARNING: the key must match the data type of the entity model!

      Parameters:
      value - The key in the correct data type.
      See Also:
  • Method Details

    • createPrimary

      public static PrimaryKey createPrimary(EntityModel model, Serializable key) throws IllegalArgumentException
      Constructs a Primary Key from Object array.
      Parameters:
      model - The entity model, required for the pk field models.
      key - The key in any data type, either the target type or a compatible type to it, or String is fine too. Just something where the string representation is convertable to the target type.
      Returns:
      typesafe primary
      Throws:
      IllegalArgumentException - If the conversion fails.
    • createPrimary

      public static PrimaryKey createPrimary(EntityModel model, String key) throws IllegalArgumentException
      Converts a stringified PK to a PK object.
      Throws:
      IllegalArgumentException
    • createPrimary

      public static PrimaryKey createPrimary(EntityModel model, Number key) throws IllegalArgumentException
      Converts a Long PK to a PK object.
      Throws:
      IllegalArgumentException
    • createPrimary

      public static PrimaryKey createPrimary(EntityModel model, Object key) throws IllegalArgumentException
      Throws:
      IllegalArgumentException
    • toString

      public String toString()
      For human-readability only!
      Overrides:
      toString in class Object
      See Also:
    • stringify

      public String stringify()
      Returns a consistent string representation.

      This returns a consistent machine-usable string version of the PK whereas the toString() method is meant for humans and might change at any time.

      maybe asString() would have been a better method name. Feel free to refactor.

    • getSerializable

      public Serializable getSerializable()
      Returns a serializable type representation of the key.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • verify

      public void verify(EntityModel model) throws ValidationException
      Verify that the key matches the entity type's model.
      Parameters:
      model - The model to verify the key against.
      Throws:
      ValidationException - If the key doesn't match the model.
    • PK

      public static PrimaryKey PK(Serializable value)
      Helper method to prettify writing of primary keys. Intended for use in static imports:

      new PrimaryKey(a, b, c)
      becomes
      
      import static ch.tocco.nice2.model.PrimaryKey.PK;
      // ...
      PK(1)

      Note: all entries of the key must match the data type of the entity model!

      Parameters:
      value - The value of the primary key.
      Returns:
      The new key.