Package ch.tocco.nice2.model.entity.api
Class PrimaryKey
java.lang.Object
ch.tocco.nice2.model.entity.api.PrimaryKey
- All Implemented Interfaces:
Serializable
A simple helper class that's used for identifying entities of a known type.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic PrimaryKey
createPrimary
(EntityModel model, Serializable key) Constructs a Primary Key from Object array.static PrimaryKey
createPrimary
(EntityModel model, Number key) Converts a Long PK to a PK object.static PrimaryKey
createPrimary
(EntityModel model, Object key) static PrimaryKey
createPrimary
(EntityModel model, String key) Converts astringified
PK to a PK object.boolean
Returns a serializable type representation of the key.int
hashCode()
static PrimaryKey
PK
(Serializable value) Helper method to prettify writing of primary keys.Returns a consistent string representation.toString()
For human-readability only!void
verify
(EntityModel model) Verify that the key matches the entity type's model.
-
Constructor Details
-
PrimaryKey
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 astringified
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
For human-readability only! -
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
Returns a serializable type representation of the key. -
equals
-
hashCode
public int hashCode() -
verify
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
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.
-