Project

General

Profile

Download (6.33 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 || orderedTerm.vocabulary == null){
104
				throw new IllegalStateException("An ordered term of class " + this.getClass() + " does not belong to a vocabulary and therefore can not be compared");
105
			}
106
			if (! this.vocabulary.getUuid().equals(orderedTerm.vocabulary.getUuid())){
107
				throw new IllegalStateException("2 terms do not belong to the same vocabulary and therefore can not be compared");
108
			}
109
		}
110
		
111
		int orderThat;
112
		int orderThis;
113
		try {
114
			orderThat = orderedTerm.orderIndex;//OLD: this.getVocabulary().getTerms().indexOf(orderedTerm);
115
			orderThis = orderIndex; //OLD: this.getVocabulary().getTerms().indexOf(this);
116
		} catch (RuntimeException e) {
117
			throw e;
118
		}
119
		if (orderThis > orderThat){
120
			return -1;
121
		}else if (orderThis < orderThat){
122
			return 1;
123
		}else {
124
			return 0;
125
		}
126
	}
127
	
128
//	public int compareTo(IdentifiableEntity o) {
129
//		if (o instanceof OrderedTermBase){
130
//			return compareTo((OrderedTermBase)o);
131
//		}else{
132
//			return super.compareTo(o);
133
//		}
134
//	}
135
	
136
	/**
137
	 * If this term is lower than the parameter term, true is returned, else false.
138
	 * If the parameter term is null, an Exception is thrown.
139
	 * @param orderedTerm
140
	 * @return boolean result of the comparison
141
	 */
142
	public boolean isLower(T orderedTerm){
143
		return (this.compareTo(orderedTerm) < 0 );
144
	}
145

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