-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
82 changed files
with
2,714 additions
and
227 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
mmm-util-core/src/main/java/net/sf/mmm/util/lang/api/Id.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package net.sf.mmm.util.lang.api; | ||
|
||
/** | ||
* This is the interface for an ID that uniquely identifies an <em>entity</em> of a particular type.<br> | ||
* An {@link Id} is build out of the following parts: | ||
* <ul> | ||
* <li>{@link #getId() object-id} - the ID that identifies the entity and is unique for a specific {@link #getType() | ||
* type}.</li> | ||
* <li>{@link #getType() type} - is the ID of the type of the identified entity.</li> | ||
* <li>{@link #getRevision() revision} - the optional {@link #getRevision() revision} of the entity.</li> | ||
* </ul> | ||
* Just like the {@link #getId() primary key} the {@link #getRevision() revision} and {@link #getType() type} of an | ||
* object do not change. This allows to create an instance of the identified object without additional costs (e.g. | ||
* database lookup) by a dynamic proxy using lazy loading.<br> | ||
* An {@link Id} has a compact {@link #toString() string representation} that can be converted back to an {@link Id}. | ||
* Therefore, the implementation shall provide a {@link String}-arg constructor and a static {@code valueOf(String)} | ||
* method. | ||
* | ||
* @param <E> the generic type of the identified entity. | ||
* | ||
* @see net.sf.mmm.util.lang.base.SimpleId | ||
* @author hohwille | ||
* @since 7.1.0 | ||
*/ | ||
public interface Id<E> extends Datatype { | ||
|
||
/** | ||
* @return the the <em>primary key</em> of the <em>entity</em>. It is only unique for a particular {@link #getType() | ||
* type} of an <em>entity</em>. | ||
*/ | ||
long getId(); | ||
|
||
/** | ||
* @return the {@link Class} reflecting the <em>type</em> of the referenced <em>entity</em>. | ||
*/ | ||
Class<E> getType(); | ||
|
||
/** | ||
* @return the optional revision of the <em>entity</em> referenced by this {@link Id} or {@code null} for the latest | ||
* revision. In case entities of this {@link #getType() type} will not be revision controlled, this method | ||
* will always return {@code null}. | ||
*/ | ||
Number getRevision(); | ||
|
||
/** | ||
* @return the {@link String} representation of this {@link Id}. Will consist of {@link #getId() object-id}, | ||
* {@link #getType() type} and {@link #getRevision() revision} separated with a specific separator. Segments | ||
* that are {@code null} will typically be omitted in the {@link String} representation. | ||
*/ | ||
@Override | ||
String toString(); | ||
|
||
} |
128 changes: 128 additions & 0 deletions
128
mmm-util-core/src/main/java/net/sf/mmm/util/lang/base/AbstractId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package net.sf.mmm.util.lang.base; | ||
|
||
import java.util.Objects; | ||
|
||
import net.sf.mmm.util.lang.api.Id; | ||
|
||
/** | ||
* This is the abstract base implementation of {@link Id}. | ||
* | ||
* @param <E> the generic type of the identified entity. | ||
* | ||
* @author hohwille | ||
* @since 7.1.0 | ||
*/ | ||
public abstract class AbstractId<E> implements Id<E> { | ||
|
||
private long objectId; | ||
|
||
private Number revision; | ||
|
||
private Class<E> type; | ||
|
||
/** | ||
* The constructor. | ||
*/ | ||
protected AbstractId() { | ||
super(); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param type - see {@link #getType()}. | ||
* @param objectId - see {@link #getId()}. | ||
*/ | ||
public AbstractId(Class<E> type, long objectId) { | ||
this(type, objectId, null); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param type - see {@link #getType()}. | ||
* @param objectId - see {@link #getId()}. | ||
* @param revision - see {@link #getRevision()}. | ||
*/ | ||
public AbstractId(Class<E> type, long objectId, Number revision) { | ||
super(); | ||
this.type = type; | ||
this.objectId = objectId; | ||
this.revision = revision; | ||
} | ||
|
||
@Override | ||
public long getId() { | ||
|
||
return this.objectId; | ||
} | ||
|
||
@Override | ||
public Class<E> getType() { | ||
|
||
return this.type; | ||
} | ||
|
||
@Override | ||
public Number getRevision() { | ||
|
||
return this.revision; | ||
} | ||
|
||
@Override | ||
public final int hashCode() { | ||
|
||
return Objects.hash(Long.valueOf(this.objectId), this.type, this.revision); | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object obj) { | ||
|
||
if (obj == this) { | ||
return true; | ||
} | ||
if ((obj == null) || !(obj instanceof AbstractId)) { | ||
return false; | ||
} | ||
AbstractId<?> other = (AbstractId<?>) obj; | ||
if (this.objectId != other.objectId) { | ||
return false; | ||
} | ||
if (!Objects.equals(this.type, other.type)) { | ||
return false; | ||
} | ||
if (!Objects.equals(this.revision, other.revision)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
|
||
StringBuilder buffer = new StringBuilder(32); | ||
toString(buffer); | ||
return buffer.toString(); | ||
} | ||
|
||
/** | ||
* @see #toString() | ||
* @param buffer the {@link StringBuilder} where to {@link StringBuilder#append(CharSequence) append} the string | ||
* representation to. | ||
*/ | ||
protected void toString(StringBuilder buffer) { | ||
|
||
if (this.type != null) { | ||
buffer.append(this.type.getSimpleName()); | ||
buffer.append(':'); | ||
} | ||
buffer.append(this.objectId); | ||
if (this.revision != null) { | ||
buffer.append(':'); | ||
buffer.append(this.revision); | ||
} | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
mmm-util-core/src/main/java/net/sf/mmm/util/lang/base/SimpleId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package net.sf.mmm.util.lang.base; | ||
|
||
import javax.persistence.Id; | ||
|
||
/** | ||
* This is a simple implementation of {@link Id}. | ||
* | ||
* @param <E> the generic type of the identified entity. | ||
* | ||
* @author hohwille | ||
* @since 7.1.0 | ||
*/ | ||
public class SimpleId<E> extends AbstractId<E> { | ||
|
||
/** | ||
* The constructor. | ||
*/ | ||
protected SimpleId() { | ||
super(); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param type - see {@link #getType()}. | ||
* @param objectId - see {@link #getId()}. | ||
*/ | ||
public SimpleId(Class<E> type, long objectId) { | ||
this(type, objectId, null); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param type - see {@link #getType()}. | ||
* @param objectId - see {@link #getId()}. | ||
* @param revision - see {@link #getRevision()}. | ||
*/ | ||
public SimpleId(Class<E> type, long objectId, Number revision) { | ||
super(type, objectId, revision); | ||
} | ||
|
||
/** | ||
* @param <E> the generic type of the identified entity. | ||
* @param type - see {@link #getType()}. | ||
* @param id - see {@link #getId()}. | ||
* @return a new instance of {@link SimpleId}. | ||
*/ | ||
public static <E> SimpleId<E> valueOf(Class<E> type, Long id) { | ||
|
||
return valueOf(type, id, null); | ||
} | ||
|
||
/** | ||
* @param <E> the generic type of the identified entity. | ||
* @param type - see {@link #getType()}. | ||
* @param id - see {@link #getId()}. | ||
* @param revision - see {@link #getRevision()}. | ||
* @return a new instance of {@link SimpleId}. | ||
*/ | ||
public static <E> SimpleId<E> valueOf(Class<E> type, Long id, Number revision) { | ||
|
||
if (id == null) { | ||
return null; | ||
} else { | ||
return valueOf(type, id.longValue(), revision); | ||
} | ||
} | ||
|
||
/** | ||
* @param <E> the generic type of the identified entity. | ||
* @param type - see {@link #getType()}. | ||
* @param id - see {@link #getId()}. | ||
* @return a new instance of {@link SimpleId}. | ||
*/ | ||
public static <E> SimpleId<E> valueOf(Class<E> type, long id) { | ||
|
||
return valueOf(type, id, null); | ||
} | ||
|
||
/** | ||
* @param <E> the generic type of the identified entity. | ||
* @param type - see {@link #getType()}. | ||
* @param id - see {@link #getId()}. | ||
* @param revision - see {@link #getRevision()}. | ||
* @return a new instance of {@link SimpleId}. | ||
*/ | ||
public static <E> SimpleId<E> valueOf(Class<E> type, long id, Number revision) { | ||
|
||
return new SimpleId<>(type, id, revision); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
.../main/java/net/sf/mmm/util/lang/base/datatype/adapter/jpa/SimpleIdAttributeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package net.sf.mmm.util.lang.base.datatype.adapter.jpa; | ||
|
||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Converter; | ||
|
||
import net.sf.mmm.util.lang.base.SimpleId; | ||
|
||
/** | ||
* This is the implementation of {@link AttributeConverter} for {@link SimpleId}. | ||
* | ||
* @author Joerg Hohwiller (hohwille at users.sourceforge.net) | ||
* @since 6.0.0 | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
@Converter(autoApply = true) | ||
// https://hibernate.atlassian.net/browse/HHH-8854 | ||
public class SimpleIdAttributeConverter implements AttributeConverter<SimpleId, Long> { | ||
|
||
/** | ||
* The constructor. | ||
*/ | ||
public SimpleIdAttributeConverter() { | ||
|
||
super(); | ||
} | ||
|
||
@Override | ||
public SimpleId convertToEntityAttribute(Long value) { | ||
|
||
return SimpleId.valueOf(null, value); | ||
} | ||
|
||
@Override | ||
public Long convertToDatabaseColumn(SimpleId datatype) { | ||
|
||
if (datatype == null) { | ||
return null; | ||
} | ||
return Long.valueOf(datatype.getId()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.