4 package eu
.etaxonomy
.cdm
.model
.common
;
6 import java
.util
.Iterator
;
7 import java
.util
.NoSuchElementException
;
9 import java
.util
.SortedSet
;
10 import java
.util
.TreeSet
;
12 import javax
.persistence
.Entity
;
13 import javax
.persistence
.Transient
;
14 import javax
.xml
.bind
.annotation
.XmlAccessType
;
15 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
16 import javax
.xml
.bind
.annotation
.XmlRootElement
;
17 import javax
.xml
.bind
.annotation
.XmlType
;
19 import org
.apache
.log4j
.Logger
;
25 @XmlAccessorType(XmlAccessType
.FIELD
)
26 @XmlType(name
= "OrderedTermVocabulary")
27 @XmlRootElement(name
= "OrderedTermVocabulary")
29 public class OrderedTermVocabulary
<T
extends OrderedTermBase
> extends TermVocabulary
<T
> {
30 private static final long serialVersionUID
= 7871741306306371242L;
31 @SuppressWarnings("unused")
32 private static final Logger logger
= Logger
.getLogger(OrderedTermVocabulary
.class);
37 public OrderedTermVocabulary() {
44 * @param termSourceUri
46 public OrderedTermVocabulary(String term
, String label
, String labelAbbrev
, String termSourceUri
) {
47 super(term
, label
, labelAbbrev
, termSourceUri
);
52 protected Set
<T
> getNewTermSet(){
53 return new TreeSet
<T
>();
57 public SortedSet
<T
> getOrderedTerms(T otb
) {
58 SortedSet
<T
> result
= new TreeSet
<T
>();
64 public SortedSet
<T
> getHigherAndEqualTerms(T otb
) {
65 SortedSet
<T
> result
= new TreeSet
<T
>();
66 SortedSet
<T
> sortedSet
= new TreeSet
<T
>();
67 sortedSet
.addAll(terms
);
68 result
.addAll( sortedSet
.tailSet(otb
));
72 public SortedSet
<T
> getHigherTerms(T otb
) {
73 SortedSet
<T
> result
= getHigherAndEqualTerms(otb
);
74 for (T setObject
: terms
){
75 if (setObject
.compareTo(otb
) == 0){
76 result
.remove(setObject
);
83 public SortedSet
<T
> getLowerAndEqualTerms(T otb
) {
84 SortedSet
<T
> result
= new TreeSet
<T
>();
85 SortedSet
<T
> sortedSet
= new TreeSet
<T
>();
86 sortedSet
.addAll(terms
);
87 result
.addAll( sortedSet
.headSet(otb
));
92 public SortedSet
<T
> getLowerTerms(T otb
) {
93 SortedSet
<T
> result
= getLowerAndEqualTerms(otb
);
94 for (T setObject
: terms
){
95 if (setObject
.compareTo(otb
) == 0){
96 result
.remove(setObject
);
103 public SortedSet
<T
> getEqualTerms(T otb
) {
104 SortedSet
<T
> result
= new TreeSet
<T
>();
105 for (T setObject
: terms
){
106 if (setObject
.compareTo(otb
) == 0){
107 result
.add(setObject
);
114 public T
getNextHigherTerm(T otb
) {
116 return getHigherTerms(otb
).first();
117 } catch (NoSuchElementException e
) {
123 public T
getNextLowerTerm(T otb
) {
125 return getLowerTerms(otb
).last();
126 } catch (NoSuchElementException e
) {
132 public T
getLowestTerm() {
134 SortedSet
<T
> sortedSet
= new TreeSet
<T
>();
135 sortedSet
.addAll(terms
);
136 return sortedSet
.first();
137 //return ((SortedSet<T>)terms).first();
138 } catch (NoSuchElementException e
) {
144 public T
getHighestTerm() {
146 SortedSet
<T
> sortedSet
= new TreeSet
<T
>();
147 sortedSet
.addAll(terms
);
148 return sortedSet
.last();
149 } catch (NoSuchElementException e
) {
155 public void addTerm(T term
) throws WrongTermTypeException
{
156 SortedSet sortedTerms
= ((SortedSet
<T
>)terms
);
157 int lowestOrderIndex
;
158 if (sortedTerms
.size() == 0){
159 lowestOrderIndex
= 0;
161 Object first
= sortedTerms
.first();
162 lowestOrderIndex
= ((T
)first
).orderIndex
;
164 term
.orderIndex
= lowestOrderIndex
+ 1;
168 public void addTermAbove(T termToBeAdded
, T lowerTerm
) throws WrongTermTypeException
{
169 int orderInd
= lowerTerm
.orderIndex
;
170 termToBeAdded
.orderIndex
= orderInd
;
171 //increment all orderIndexes of terms below
172 Iterator
<T
> iterator
= terms
.iterator();
173 while(iterator
.hasNext()){
174 T term
= iterator
.next();
175 if (term
.orderIndex
>= orderInd
){ //should always be true
179 super.addTerm(termToBeAdded
);
182 public void addTermBelow(T termToBeAdded
, T higherTerm
) throws WrongTermTypeException
{
183 int orderInd
= higherTerm
.orderIndex
;
184 termToBeAdded
.orderIndex
= orderInd
+ 1;
185 //increment all orderIndexes of terms below
186 Iterator
<T
> iterator
= getLowerTerms(higherTerm
).iterator();
187 while(iterator
.hasNext()){
188 T term
= iterator
.next();
189 if (term
.orderIndex
> orderInd
){
193 super.addTerm(termToBeAdded
);
196 // public void addTermEqualLevel(T termToBeAdded, T equalLevelTerm) throws WrongTermTypeException {
197 // int orderInd = equalLevelTerm.orderIndex;
198 // termToBeAdded.orderIndex = orderInd;
199 // super.addTerm(termToBeAdded);
203 public void removeTerm(T term
) {
207 if (this.getEqualTerms(term
).size() == 0){
208 Iterator
<T
> iterator
= getLowerTerms(term
).iterator();
209 while (iterator
.hasNext()){
210 T otb
= iterator
.next();
211 toBeChangedByObject
= otb
;
212 otb
.decreaseIndex(this);
213 toBeChangedByObject
= null;
216 term
.setVocabulary(null);
219 private T toBeChangedByObject
;
221 public boolean indexChangeAllowed(T otb
){
222 return otb
== toBeChangedByObject
;