Project

General

Profile

Download (2.27 KB) Statistics
| Branch: | Tag: | Revision:
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
    public TypedEntityReference(Class<T> type, UUID uuid, String label) {
30
        super(uuid, label);
31
        this.type = type;
32
    }
33

    
34
    public TypedEntityReference(Class<T> type, UUID uuid) {
35
        super(uuid, null);
36
        this.type = type;
37
    }
38

    
39
    public static  <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity) {
40
        if(entity == null) {
41
            return null;
42
        }
43
        entity = HibernateProxyHelper.deproxy(entity);
44
        if(IdentifiableEntity.class.isAssignableFrom(entity.getClass())) {
45
            return new TypedEntityReference<T>((Class<T>)entity.getClass(), entity.getUuid(), ((IdentifiableEntity)entity).getTitleCache());
46
        } else {
47
            return new TypedEntityReference<T>((Class<T>)entity.getClass(), entity.getUuid());
48
        }
49
    }
50

    
51
    public Class<T> getType() {
52
        return type;
53
    }
54
    public void setType(Class<T> type) {
55
        this.type = type;
56
    }
57

    
58
    @Override
59
    public int hashCode() {
60
        return new HashCodeBuilder(17, 31)
61
                .append(uuid)
62
                .appendSuper(type.hashCode())
63
                .hashCode();
64
    }
65

    
66
    @SuppressWarnings("rawtypes")
67
    @Override
68
    public boolean equals(Object obj) {
69
        try {
70
            TypedEntityReference other = (TypedEntityReference) obj;
71
            return uuid.equals(other.uuid) && type.equals(other.type);
72

    
73
        } catch (Exception e) {
74
            return false;
75
        }
76
    }
77

    
78
    @Override
79
    public String toString(){
80
        return type.getSimpleName() + "#" + uuid;
81
    }
82
}
(3-3/3)