Module io.github.mmm.entity
Package io.github.mmm.entity.id
Class AbstractId<E,I,V extends Comparable<?>>
- java.lang.Object
-
- io.github.mmm.entity.id.AbstractId<E,I,V>
-
- All Implemented Interfaces:
Id<E>
- Direct Known Subclasses:
AbstractInstantId,AbstractVersionId
public abstract class AbstractId<E,I,V extends Comparable<?>> extends Object implements Id<E>
This is the abstract base implementation ofId.- Since:
- 1.0.0
-
-
Field Summary
-
Fields inherited from interface io.github.mmm.entity.id.Id
PROPERTY_ID, PROPERTY_VERSION, VERSION_LATEST, VERSION_SEPARATOR
-
-
Constructor Summary
Constructors Constructor Description AbstractId(Class<E> type)The constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)abstract IdFactory<I,V,?>getFactory()abstract IgetId()static <I> IgetIdAs(Id<?> id, Class<I> type)Class<E>getType()abstract VgetVersion()static <V> VgetVersionAs(Id<?> id, Class<V> type)static longgetVersionAsLong(Id<?> id)static <E> Id<E>getWithType(Id<?> id, Class<E> type)inthashCode()protected static UUIDparseUuid(String idString)StringtoString()protected voidtoString(StringBuilder buffer)Id<E>withLatestVersion()Id<E>withType(Class<?> newType)Id<E>withVersion(V newVersion)-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.github.mmm.entity.id.Id
getIdAsString, getVersionAsString
-
-
-
-
Method Detail
-
getId
public abstract I getId()
- Specified by:
getIdin interfaceId<E>- Returns:
- the primary key of the identified
EntityasObjectvalue. It is only unique for a particulartypeof an entity. - See Also:
LongVersionId,UuidVersionId,StringVersionId
-
getVersion
public abstract V getVersion()
- Specified by:
getVersionin interfaceId<E>- Returns:
- the
versionof this entity. Whenever theEntitygets updated (a modification is saved and the transaction is committed), this version is increased. Typically the version is aNumberstarting with0for a newEntitythat is increased whenever a modification is committed. However, it may also be anInstant. The version acts as a modification sequence for optimistic locking. On each update it will be verified that the version has not been increased already by another transaction. When linking anEntity(Idused as foreign key) the version can act as revision for auditing. If it isId.VERSION_LATEST(null) it points to the latest revision of theEntity. Otherwise it points to a specific historic revision of theEntity. Depending on the database technology (e.g. when using hibernate envers) the version and the revision can be semantically different. In such case aprimary keycan not be converted 1:1 as revisioned foreign keyId.
-
withLatestVersion
public Id<E> withLatestVersion()
- Specified by:
withLatestVersionin interfaceId<E>- Returns:
- a copy of this
Idwith aversionofId.VERSION_LATESTe.g. to use theIdas regular foreign key (pointing to the latest revision and not a historic revision) or thisIditself if already satisfying.
-
withVersion
public Id<E> withVersion(V newVersion)
- Parameters:
newVersion- the new value ofgetVersion().- Returns:
- a copy of this
Idwith the givenversionor thisIditself if already satisfying.
-
withType
public final Id<E> withType(Class<?> newType)
- Specified by:
withTypein interfaceId<E>- Parameters:
newType- the new value ofId.getType(). Exact type should actually beClass<E> but this prevents simple usage. As thetypecan not actually be changed with this method, this should be fine.- Returns:
- a copy of this
Idwith the giventypeor thisIditself if already satisfying.
-
toString
public String toString()
-
toString
protected void toString(StringBuilder buffer)
- Parameters:
buffer- theStringBuilderwhere toappendthe string representation to.- See Also:
toString()
-
getIdAs
public static <I> I getIdAs(Id<?> id, Class<I> type) throws IllegalArgumentException
- Type Parameters:
I- the generic type expected forId.getId().- Parameters:
id- the actualIdinstance.type- theClassreflecting the expected type ofId.getId().- Returns:
- the
primary keyof the givenId. May benullif the givenIdisnull. - Throws:
IllegalArgumentException- if the giventypedoes not match.
-
getVersionAs
public static <V> V getVersionAs(Id<?> id, Class<V> type) throws IllegalArgumentException
- Type Parameters:
V- the generic type expected forId.getVersion().- Parameters:
id- the actualIdinstance.type- theClassreflecting the expected type ofId.getVersion().- Returns:
- the
versionof the givenId. May benullif the givenIditself or itsversionisnull. - Throws:
IllegalArgumentException- if the giventypedoes not match.
-
getVersionAsLong
public static long getVersionAsLong(Id<?> id) throws IllegalArgumentException
-
-