Project

General

Profile

Download (2.72 KB) Statistics
| Branch: | Tag: | Revision:
1 6e858cb7 Andreas Kohlbecker
/**
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 09f4db2f Andreas Kohlbecker
package eu.etaxonomy.cdm.ref;
10 6e858cb7 Andreas Kohlbecker
11
import java.util.UUID;
12
13 f33355c3 Andreas Müller
import org.apache.commons.lang3.builder.HashCodeBuilder;
14 6e858cb7 Andreas Kohlbecker
15 7e0e6396 Andreas Kohlbecker
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
16 1d9ed98e Andreas Kohlbecker
import eu.etaxonomy.cdm.model.common.CdmBase;
17
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
18
19 6e858cb7 Andreas Kohlbecker
/**
20
 * @author a.kohlbecker
21
 * @since Jun 12, 2017
22
 */
23 ea5bc5a6 Andreas Kohlbecker
public class TypedEntityReference<T extends CdmBase> extends EntityReference {
24 6e858cb7 Andreas Kohlbecker
25 a731ca32 Patrick Plitzner
    private static final long serialVersionUID = -4619590272174606288L;
26 696be488 Andreas Müller
27
    private Class<T> type;
28
29 2a28e5d2 Andreas Kohlbecker
    /**
30
     * @deprecated use factory method instead (TODO: to be made protected once no longer used publicly)
31
     */
32
    @Deprecated
33 6e858cb7 Andreas Kohlbecker
    public TypedEntityReference(Class<T> type, UUID uuid, String label) {
34
        super(uuid, label);
35
        this.type = type;
36
    }
37
38 2a28e5d2 Andreas Kohlbecker
    /**
39
     * @deprecated use factory method instead (TODO; to be made protected once no longer used publicly)
40
     */
41
    @Deprecated
42 6e858cb7 Andreas Kohlbecker
    public TypedEntityReference(Class<T> type, UUID uuid) {
43
        super(uuid, null);
44
        this.type = type;
45
    }
46
47 2a28e5d2 Andreas Kohlbecker
    public static  <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity, boolean withLabel) {
48 1d9ed98e Andreas Kohlbecker
        if(entity == null) {
49
            return null;
50
        }
51 7e0e6396 Andreas Kohlbecker
        entity = HibernateProxyHelper.deproxy(entity);
52 2a28e5d2 Andreas Kohlbecker
        if(withLabel && IdentifiableEntity.class.isAssignableFrom(entity.getClass())) {
53 ea5bc5a6 Andreas Kohlbecker
            return new TypedEntityReference<T>((Class<T>)entity.getClass(), entity.getUuid(), ((IdentifiableEntity)entity).getTitleCache());
54
        } else {
55
            return new TypedEntityReference<T>((Class<T>)entity.getClass(), entity.getUuid());
56 1d9ed98e Andreas Kohlbecker
        }
57
    }
58
59 2a28e5d2 Andreas Kohlbecker
    public static  <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity) {
60
        return TypedEntityReference.fromEntity(entity, true);
61
    }
62
63 6e858cb7 Andreas Kohlbecker
    public Class<T> getType() {
64
        return type;
65
    }
66
    public void setType(Class<T> type) {
67
        this.type = type;
68
    }
69
70
    @Override
71
    public int hashCode() {
72
        return new HashCodeBuilder(17, 31)
73
                .append(uuid)
74
                .appendSuper(type.hashCode())
75
                .hashCode();
76
    }
77
78
    @SuppressWarnings("rawtypes")
79
    @Override
80
    public boolean equals(Object obj) {
81
        try {
82
            TypedEntityReference other = (TypedEntityReference) obj;
83
            return uuid.equals(other.uuid) && type.equals(other.type);
84
85
        } catch (Exception e) {
86
            return false;
87
        }
88
    }
89
90
    @Override
91
    public String toString(){
92
        return type.getSimpleName() + "#" + uuid;
93
    }
94 a06014f5 Andreas Müller
}