Project

General

Profile

Download (6.14 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.common;
11

    
12
import javax.persistence.Entity;
13
import javax.xml.bind.annotation.XmlAccessType;
14
import javax.xml.bind.annotation.XmlAccessorType;
15
import javax.xml.bind.annotation.XmlElement;
16
import javax.xml.bind.annotation.XmlSeeAlso;
17
import javax.xml.bind.annotation.XmlType;
18

    
19
import org.apache.log4j.Logger;
20
import org.hibernate.envers.Audited;
21
import org.hibernate.search.annotations.Indexed;
22

    
23
import eu.etaxonomy.cdm.model.description.Modifier;
24
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
25
import eu.etaxonomy.cdm.model.description.State;
26
import eu.etaxonomy.cdm.model.location.NamedArea;
27
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
28
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
29
import eu.etaxonomy.cdm.model.name.Rank;
30

    
31
/**
32
 * @author m.doering
33
 * @version 1.0
34
 * @created 08-Nov-2007 13:06:23
35
 */
36
@XmlAccessorType(XmlAccessType.FIELD)
37
@XmlType(name = "OrderedTermBase", propOrder = {
38
    "orderIndex"
39
})
40
@XmlSeeAlso({
41
	RelationshipTermBase.class,
42
	Modifier.class,
43
	PresenceAbsenceTermBase.class,
44
	State.class,
45
	NamedArea.class,
46
	NamedAreaLevel.class,
47
	NomenclaturalStatusType.class,
48
	Rank.class
49
})
50
@Entity
51
@Indexed(index = "eu.etaxonomy.cdm.model.common.DefinedTermBase")
52
@Audited
53
public abstract class OrderedTermBase<T extends OrderedTermBase> extends DefinedTermBase<T> implements Comparable<T> {
54
	private static final long serialVersionUID = 8000797926720467399L;
55
	@SuppressWarnings("unused")
56
	private static final Logger logger = Logger.getLogger(OrderedTermBase.class);
57
	
58
	//Order index, value < 1 means that this Term is not in order yet
59
	@XmlElement(name = "OrderIndex")
60
	protected int orderIndex;
61
	
62
	/**
63
	 * Higher ordered terms have a lower order index, 
64
	 * lower ordered terms have a higher order index:
65
	 * <p>
66
	 * <b>a.oderIndex &lt; b.oderIndex : a &gt; b</b>
67
	 * @return the order index of a term
68
	 */
69
	public int getOrderIndex() {
70
		return orderIndex;
71
	}
72
		
73
	public OrderedTermBase() {
74
		super();
75
	}
76
	public OrderedTermBase(String term, String label, String labelAbbrev) {
77
		super(term, label, labelAbbrev);
78
	}	
79
	
80
	/**
81
	 * Compares this OrderedTermBase with the specified OrderedTermBase for
82
	 * order. Returns a negative integer, zero, or a positive integer as the
83
	 * orderId of this object is greater than, equal to, or less than the
84
	 * specified object. This Integer compare logic of this method is the
85
	 * <b>inverse logic</b> of the the one implemented in
86
	 * {@link java.lang.Comparable#compareTo(java.lang.Object)}
87
	 */
88
	public int compareTo(T orderedTerm) {
89
		return performCompareTo(orderedTerm, false);
90
	}
91
	
92
	/**
93
	 * Compares this OrderedTermBase with the specified OrderedTermBase for
94
	 * order. Returns a negative integer, zero, or a positive integer as the
95
	 * orderId of this object is greater than, equal to, or less than the
96
	 * specified object. This Integer compare logic of this method is the
97
	 * <b>inverse logic</b> of the the one implemented in
98
	 * {@link java.lang.Comparable#compareTo(java.lang.Object)}
99
	 */
100
	protected int performCompareTo(T orderedTerm, boolean skipVocabularyCheck) {
101
		
102
		if(!skipVocabularyCheck){
103
			if (this.vocabulary == null || ! this.vocabulary.getUuid().equals(orderedTerm.vocabulary.getUuid())){
104
				throw new IllegalStateException("2 terms do not belong to the same vocabulary and therefore can not be compared");
105
			}
106
		}
107
		
108
		int orderThat;
109
		int orderThis;
110
		try {
111
			orderThat = orderedTerm.orderIndex;//OLD: this.getVocabulary().getTerms().indexOf(orderedTerm);
112
			orderThis = orderIndex; //OLD: this.getVocabulary().getTerms().indexOf(this);
113
		} catch (RuntimeException e) {
114
			throw e;
115
		}
116
		if (orderThis > orderThat){
117
			return -1;
118
		}else if (orderThis < orderThat){
119
			return 1;
120
		}else {
121
			return 0;
122
		}
123
	}
124
	
125
//	public int compareTo(IdentifiableEntity o) {
126
//		if (o instanceof OrderedTermBase){
127
//			return compareTo((OrderedTermBase)o);
128
//		}else{
129
//			return super.compareTo(o);
130
//		}
131
//	}
132
	
133
	/**
134
	 * If this term is lower than the parameter term, true is returned, else false.
135
	 * If the parameter term is null, an Exception is thrown.
136
	 * @param orderedTerm
137
	 * @return boolean result of the comparison
138
	 */
139
	public boolean isLower(T orderedTerm){
140
		return (this.compareTo(orderedTerm) < 0 );
141
	}
142

    
143
	
144
	/**
145
	 * If this term is higher than the parameter term, true is returned, else false.
146
	 * If the parameter term is null, an Exception is thrown.
147
	 * @param orderedTerm
148
	 * @return boolean result of the comparison
149
	 */
150
	public boolean isHigher(T orderedTerm){
151
		return (this.compareTo(orderedTerm) > 0 );
152
	}
153
	
154
	
155
	/** 
156
	 * @deprecated To be used only by OrderedTermVocabulary
157
	 **/
158
	@Deprecated
159
	protected boolean decreaseIndex(OrderedTermVocabulary<T> vocabulary){
160
		if (vocabulary.indexChangeAllowed(this) == true){
161
			orderIndex--;
162
			return true;
163
		}else{
164
			return false;
165
		}
166
	}
167
	
168
	/** 
169
	 * @deprecated To be used only by OrderedTermVocabulary
170
	 **/
171
	@Deprecated
172
	protected boolean incrementIndex(OrderedTermVocabulary<T> vocabulary){
173
		if (vocabulary.indexChangeAllowed(this) == true){
174
			orderIndex++;
175
			return true;
176
		}else{
177
			return false;
178
		}
179
	}
180
	
181
	@Override
182
	public boolean equals(Object object){
183
		if(this == object)
184
			return true;
185
		if((object == null) || (!OrderedTermBase.class.isAssignableFrom(object.getClass()))) {
186
			return false;
187
		}else{
188
			OrderedTermBase orderedTermBase = (OrderedTermBase)object;
189
			if (orderedTermBase.getUuid().equals(this.getUuid())){
190
				return true;
191
			}else{
192
				return false;
193
			}
194
		}
195
	}
196
	
197
//*********************** CLONE ********************************************************/
198
	
199
	/** 
200
	 * Clones <i>this</i> OrderedTermBase. This is a shortcut that enables to create
201
	 * a new instance that differs only slightly from <i>this</i> OrderedTermBase.
202
	 * 
203
	 * @see eu.etaxonomy.cdm.model.common.DefinedTermBase#clone()
204
	 * @see java.lang.Object#clone()
205
	 */
206
	@Override
207
	public Object clone() {
208
		OrderedTermBase result = (OrderedTermBase) super.clone();
209
		//no changes to orderIndex
210
		return result;
211
	}
212
}
(44-44/63)