2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.ref
;
11 import java
.util
.UUID
;
13 import org
.apache
.commons
.lang3
.builder
.HashCodeBuilder
;
15 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
16 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
17 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
20 * @author a.kohlbecker
23 public class TypedEntityReference
<T
extends CdmBase
> extends EntityReference
{
25 private static final long serialVersionUID
= -4619590272174606288L;
27 private Class
<T
> type
;
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)
33 public TypedEntityReference(Class
<T
> type
, UUID uuid
, String label
) {
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)
42 public TypedEntityReference(T entity
) {
44 this.type
= (Class
<T
>) entity
.getClass();
45 this.uuid
= entity
.getUuid();
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)
52 public TypedEntityReference(Class
<T
> type
, UUID uuid
) {
57 public static <T
extends CdmBase
> TypedEntityReference
<T
> fromEntity(T entity
, boolean withLabel
) {
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());
65 return new TypedEntityReference
<>((Class
<T
>)entity
.getClass(), entity
.getUuid());
70 * Casts the <code>TypedEntityReference</code> to the <code>subType</code> if possible.
72 * @throws ClassCastException
73 * If the {@link #type} is not a super type of <code>subType</code>.
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());
79 return new TypedEntityReference
<>(subType
, getUuid());
82 public static <T
extends CdmBase
> TypedEntityReference
<T
> fromEntity(T entity
) {
83 return TypedEntityReference
.fromEntity(entity
, true);
86 public Class
<T
> getType() {
89 public void setType(Class
<T
> type
) {
94 public int hashCode() {
95 return new HashCodeBuilder(17, 31)
97 .appendSuper(type
.hashCode())
101 @SuppressWarnings("rawtypes")
103 public boolean equals(Object obj
) {
105 TypedEntityReference other
= (TypedEntityReference
) obj
;
106 return uuid
.equals(other
.uuid
) && type
.equals(other
.type
);
108 } catch (Exception e
) {
114 public String
toString(){
115 return type
.getSimpleName() + "#" + uuid
;