import org.apache.commons.lang3.builder.HashCodeBuilder;
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
+
/**
* @author a.kohlbecker
* @since Jun 12, 2017
- *
*/
-public class TypedEntityReference<T> extends EntityReference {
+public class TypedEntityReference<T extends CdmBase> extends EntityReference {
private static final long serialVersionUID = -4619590272174606288L;
private Class<T> type;
/**
- * @param uuid
- * @param label
+ * @deprecated use factory method instead, should only be used by in DTO sub-class constructors (TODO: to be made protected once no longer used publicly)
*/
+ @Deprecated
public TypedEntityReference(Class<T> type, UUID uuid, String label) {
super(uuid, label);
this.type = type;
}
+ /**
+ * @deprecated use factory method instead, should only be used by in DTO sub-class constructors (TODO: to be made protected once no longer used publicly)
+ */
+ @Deprecated
+ public TypedEntityReference(T entity) {
+ super();
+ this.type = (Class<T>) entity.getClass();
+ this.uuid = entity.getUuid();
+ }
+
+ /**
+ * @deprecated use factory method instead, should only be used by in DTO sub-class constructors (TODO; to be made protected once no longer used publicly)
+ */
+ @Deprecated
public TypedEntityReference(Class<T> type, UUID uuid) {
super(uuid, null);
this.type = type;
}
+ public static <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity, boolean withLabel) {
+ if(entity == null) {
+ return null;
+ }
+ entity = HibernateProxyHelper.deproxy(entity);
+ if(withLabel && IdentifiableEntity.class.isAssignableFrom(entity.getClass())) {
+ return new TypedEntityReference<>((Class<T>)entity.getClass(), entity.getUuid(), ((IdentifiableEntity)entity).getTitleCache());
+ } else {
+ return new TypedEntityReference<>((Class<T>)entity.getClass(), entity.getUuid());
+ }
+ }
+
+ /**
+ * Casts the <code>TypedEntityReference</code> to the <code>subType</code> if possible.
+ *
+ * @throws ClassCastException
+ * If the {@link #type} is not a super type of <code>subType</code>.
+ */
+ public <S extends CdmBase> TypedEntityReference<S> castTo(Class<S> subType){
+ if(!type.isAssignableFrom(subType)) {
+ throw new ClassCastException("Cannot cast " + type.getName() + " to " + subType.getName());
+ }
+ return new TypedEntityReference<>(subType, getUuid());
+ }
+
+ public static <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity) {
+ return TypedEntityReference.fromEntity(entity, true);
+ }
+
public Class<T> getType() {
return type;
}
this.type = type;
}
- /**
- * {@inheritDoc}
- */
@Override
public int hashCode() {
return new HashCodeBuilder(17, 31)
.hashCode();
}
- /**
- * {@inheritDoc}
- */
@SuppressWarnings("rawtypes")
@Override
public boolean equals(Object obj) {
@Override
public String toString(){
return type.getSimpleName() + "#" + uuid;
-
}
-
-}
+}
\ No newline at end of file