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 boolean
equals(Object obj)
abstract IdFactory<I,V,?>
getFactory()
abstract I
getId()
static <I> I
getIdAs(Id<?> id, Class<I> type)
Class<E>
getType()
abstract V
getVersion()
static <V> V
getVersionAs(Id<?> id, Class<V> type)
static long
getVersionAsLong(Id<?> id)
static <E> Id<E>
getWithType(Id<?> id, Class<E> type)
int
hashCode()
protected static UUID
parseUuid(String idString)
String
toString()
protected void
toString(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:
getId
in interfaceId<E>
- Returns:
- the primary key of the identified
Entity
asObject
value. It is only unique for a particulartype
of an entity. - See Also:
LongVersionId
,UuidVersionId
,StringVersionId
-
getVersion
public abstract V getVersion()
- Specified by:
getVersion
in interfaceId<E>
- Returns:
- the
version
of this entity. Whenever theEntity
gets updated (a modification is saved and the transaction is committed), this version is increased. Typically the version is aNumber
starting with0
for a newEntity
that 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
(Id
used 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 key
can not be converted 1:1 as revisioned foreign keyId
.
-
withLatestVersion
public Id<E> withLatestVersion()
- Specified by:
withLatestVersion
in interfaceId<E>
- Returns:
- a copy of this
Id
with aversion
ofId.VERSION_LATEST
e.g. to use theId
as regular foreign key (pointing to the latest revision and not a historic revision) or thisId
itself if already satisfying.
-
withVersion
public Id<E> withVersion(V newVersion)
- Parameters:
newVersion
- the new value ofgetVersion()
.- Returns:
- a copy of this
Id
with the givenversion
or thisId
itself if already satisfying.
-
withType
public final Id<E> withType(Class<?> newType)
- Specified by:
withType
in interfaceId<E>
- Parameters:
newType
- the new value ofId.getType()
. Exact type should actually beClass
<E> but this prevents simple usage. As thetype
can not actually be changed with this method, this should be fine.- Returns:
- a copy of this
Id
with the giventype
or thisId
itself if already satisfying.
-
toString
public String toString()
-
toString
protected void toString(StringBuilder buffer)
- Parameters:
buffer
- theStringBuilder
where toappend
the 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 actualId
instance.type
- theClass
reflecting the expected type ofId.getId()
.- Returns:
- the
primary key
of the givenId
. May benull
if the givenId
isnull
. - Throws:
IllegalArgumentException
- if the giventype
does 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 actualId
instance.type
- theClass
reflecting the expected type ofId.getVersion()
.- Returns:
- the
version
of the givenId
. May benull
if the givenId
itself or itsversion
isnull
. - Throws:
IllegalArgumentException
- if the giventype
does not match.
-
getVersionAsLong
public static long getVersionAsLong(Id<?> id) throws IllegalArgumentException
-
-