Project

General

Profile

Download (3.67 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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

    
10
package eu.etaxonomy.cdm.model.term;
11

    
12
import java.util.HashSet;
13
import java.util.Set;
14
import java.util.UUID;
15

    
16
import javax.persistence.Entity;
17
import javax.persistence.Transient;
18
import javax.validation.constraints.NotNull;
19
import javax.xml.bind.annotation.XmlAccessType;
20
import javax.xml.bind.annotation.XmlAccessorType;
21
import javax.xml.bind.annotation.XmlRootElement;
22
import javax.xml.bind.annotation.XmlType;
23

    
24
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
25
import org.hibernate.envers.Audited;
26

    
27

    
28
/**
29
 * @author a.mueller
30
 * @since 07.03.2019
31
 *
32
 * @param <T>
33
 */
34
@XmlAccessorType(XmlAccessType.FIELD)
35
@XmlType(name = "TermTree", propOrder = {
36

    
37
})
38
@XmlRootElement(name = "TermTree")
39
@Entity
40
@Audited
41
public class TermGraph <T extends DefinedTermBase>
42
            extends TermGraphBase<T, TermRelation<T>> {
43

    
44
	private static final long serialVersionUID = -6713834139003172735L;
45
	private static final Logger logger = LogManager.getLogger(TermGraph.class);
46

    
47
//******************** FACTORY METHODS ******************************************/
48

    
49
    /**
50
     * Creates a new term collection instance for the given term type
51
     * with an empty {@link #getRoot() root node}.
52
     * @param termType the {@link TermType term type}, must not be null
53
     */
54
    public static <T extends DefinedTermBase<T>> TermGraph<T> NewInstance(@NotNull TermType termType){
55
        return new TermGraph<>(termType);
56
    }
57

    
58
	/**
59
	 * Creates a new TermGraph instance with a given uuid.
60
	 * @param termType
61
	 * @param uuid
62
	 * @return
63
	 */
64
	public static <T extends DefinedTermBase<T>> TermGraph<T> NewInstance(@NotNull TermType termType, UUID uuid){
65
		TermGraph<T> result =  new TermGraph<>(termType);
66
		result.setUuid(uuid);
67
		return result;
68
	}
69

    
70
// ******************** CONSTRUCTOR *************************************/
71

    
72
    //TODO needed?
73
    @Deprecated
74
    protected TermGraph(){}
75

    
76
	/**
77
	 * Class constructor: creates a new feature tree instance with an empty
78
	 * {@link #getRoot() root node}.
79
	 */
80
	protected TermGraph(TermType termType) {
81
        super(termType);
82
	}
83

    
84
// ****************** GETTER / SETTER **********************************/
85

    
86

    
87
//******************** METHODS ***********************************************/
88

    
89
	/**
90
	 * Computes a set of distinct terms that are present in this term tree
91
	 *
92
	 * @return
93
	 */
94
	@Override
95
    @Transient
96
	public Set<T> getDistinctTerms(){
97
	    Set<T> result = new HashSet<>();
98
	    for (TermRelation<T> rel : getTermRelations()){
99
	        result.add(rel.getTerm());
100
	        result.add(rel.getToTerm());
101
	    }
102
	    result.remove(null);  //just in case
103
	    return result;
104
	}
105

    
106
//*********************** CLONE ********************************************************/
107

    
108
	/**
109
	 * Clones <i>this</i> {@link TermGraph}. This is a shortcut that enables to create
110
	 * a new instance that differs only slightly from <i>this</i> graph by
111
	 * modifying only some of the attributes.
112
	 * {@link TermRelation Term relations} always belong only to one tree, so all
113
	 * {@link TermRelation Term relations} are cloned to build
114
	 * the new {@link TermGraph}
115
	 *
116
	 * @see java.lang.Object#clone()
117
	 */
118
	@Override
119
	public TermGraph<T> clone() {
120
		try {
121
		    TermGraph<T> result = (TermGraph<T>)super.clone();
122
			return result;
123
		}catch (CloneNotSupportedException e) {
124
            String message = "Clone not possible. Object does not implement cloneable";
125
            logger.warn(message);
126
            throw new RuntimeException(message);
127
		}
128
	}
129
}
(20-20/30)