cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / ref / TypedEntityReference.java
1 /**
2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.ref;
10
11 import java.util.UUID;
12
13 import org.apache.commons.lang3.builder.HashCodeBuilder;
14
15 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
16 import eu.etaxonomy.cdm.model.common.CdmBase;
17 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
18
19 /**
20 * @author a.kohlbecker
21 * @since Jun 12, 2017
22 */
23 public class TypedEntityReference<T extends CdmBase> extends EntityReference {
24
25 private static final long serialVersionUID = -4619590272174606288L;
26
27 private Class<T> type;
28
29 /**
30 * @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)
31 */
32 @Deprecated
33 public TypedEntityReference(Class<T> type, UUID uuid, String label) {
34 super(uuid, label);
35 this.type = type;
36 }
37
38 /**
39 * @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)
40 */
41 @Deprecated
42 public TypedEntityReference(T entity) {
43 super();
44 this.type = (Class<T>) entity.getClass();
45 this.uuid = entity.getUuid();
46 }
47
48 /**
49 * @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)
50 */
51 @Deprecated
52 public TypedEntityReference(Class<T> type, UUID uuid) {
53 super(uuid, null);
54 this.type = type;
55 }
56
57 public static <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity, boolean withLabel) {
58 if(entity == null) {
59 return null;
60 }
61 entity = HibernateProxyHelper.deproxy(entity);
62 if(withLabel && IdentifiableEntity.class.isAssignableFrom(entity.getClass())) {
63 return new TypedEntityReference<>((Class<T>)entity.getClass(), entity.getUuid(), ((IdentifiableEntity)entity).getTitleCache());
64 } else {
65 return new TypedEntityReference<>((Class<T>)entity.getClass(), entity.getUuid());
66 }
67 }
68
69 /**
70 * Casts the <code>TypedEntityReference</code> to the <code>subType</code> if possible.
71 *
72 * @throws ClassCastException
73 * If the {@link #type} is not a super type of <code>subType</code>.
74 */
75 public <S extends CdmBase> TypedEntityReference<S> castTo(Class<S> subType){
76 if(!type.isAssignableFrom(subType)) {
77 throw new ClassCastException("Cannot cast " + type.getName() + " to " + subType.getName());
78 }
79 return new TypedEntityReference<>(subType, getUuid());
80 }
81
82 public static <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity) {
83 return TypedEntityReference.fromEntity(entity, true);
84 }
85
86 public Class<T> getType() {
87 return type;
88 }
89 public void setType(Class<T> type) {
90 this.type = type;
91 }
92
93 @Override
94 public int hashCode() {
95 return new HashCodeBuilder(17, 31)
96 .append(uuid)
97 .appendSuper(type.hashCode())
98 .hashCode();
99 }
100
101 @SuppressWarnings("rawtypes")
102 @Override
103 public boolean equals(Object obj) {
104 try {
105 TypedEntityReference other = (TypedEntityReference) obj;
106 return uuid.equals(other.uuid) && type.equals(other.type);
107
108 } catch (Exception e) {
109 return false;
110 }
111 }
112
113 @Override
114 public String toString(){
115 return type.getSimpleName() + "#" + uuid;
116 }
117 }