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
;
15 import org
.apache
.log4j
.Logger
;
22 public class OrderedTermVocabulary
<T
extends OrderedTermBase
> extends TermVocabulary
<T
> {
23 private static final Logger logger
= Logger
.getLogger(OrderedTermVocabulary
.class);
28 public OrderedTermVocabulary() {
35 * @param termSourceUri
37 public OrderedTermVocabulary(String term
, String label
, String termSourceUri
) {
38 super(term
, label
, termSourceUri
);
43 protected Set
<T
> getNewTermSet(){
44 return new TreeSet
<T
>();
48 public SortedSet
<T
> getOrderedTerms(T otb
) {
49 SortedSet
<T
> result
= new TreeSet
<T
>();
55 public SortedSet
<T
> getHigherAndEqualTerms(T otb
) {
56 SortedSet
<T
> result
= new TreeSet
<T
>();
57 SortedSet
<T
> sortedSet
= new TreeSet
<T
>();
58 sortedSet
.addAll(terms
);
59 result
.addAll( sortedSet
.tailSet(otb
));
63 public SortedSet
<T
> getHigherTerms(T otb
) {
64 SortedSet
<T
> result
= getHigherAndEqualTerms(otb
);
65 for (T setObject
: terms
){
66 if (setObject
.compareTo(otb
) == 0){
67 result
.remove(setObject
);
74 public SortedSet
<T
> getLowerAndEqualTerms(T otb
) {
75 SortedSet
<T
> result
= new TreeSet
<T
>();
76 SortedSet
<T
> sortedSet
= new TreeSet
<T
>();
77 sortedSet
.addAll(terms
);
78 result
.addAll( sortedSet
.headSet(otb
));
83 public SortedSet
<T
> getLowerTerms(T otb
) {
84 SortedSet
<T
> result
= getLowerAndEqualTerms(otb
);
85 for (T setObject
: terms
){
86 if (setObject
.compareTo(otb
) == 0){
87 result
.remove(setObject
);
94 public SortedSet
<T
> getEqualTerms(T otb
) {
95 SortedSet
<T
> result
= new TreeSet
<T
>();
96 for (T setObject
: terms
){
97 if (setObject
.compareTo(otb
) == 0){
98 result
.add(setObject
);
105 public T
getNextHigherTerm(T otb
) {
107 return getHigherTerms(otb
).first();
108 } catch (NoSuchElementException e
) {
114 public T
getNextLowerTerm(T otb
) {
116 return getLowerTerms(otb
).last();
117 } catch (NoSuchElementException e
) {
123 public T
getLowestTerm() {
125 SortedSet
<T
> sortedSet
= new TreeSet
<T
>();
126 sortedSet
.addAll(terms
);
127 return sortedSet
.first();
128 //return ((SortedSet<T>)terms).first();
129 } catch (NoSuchElementException e
) {
135 public T
getHighestTerm() {
137 SortedSet
<T
> sortedSet
= new TreeSet
<T
>();
138 sortedSet
.addAll(terms
);
139 return sortedSet
.last();
140 } catch (NoSuchElementException e
) {
146 public void addTerm(T term
) throws WrongTermTypeException
{
147 SortedSet sortedTerms
= ((SortedSet
<T
>)terms
);
148 int lowestOrderIndex
;
149 if (sortedTerms
.size() == 0){
150 lowestOrderIndex
= 0;
152 Object first
= (T
)sortedTerms
.first();
153 lowestOrderIndex
= ((T
)first
).orderIndex
;
155 term
.orderIndex
= lowestOrderIndex
+ 1;
159 public void addTermAbove(T termToBeAdded
, T lowerTerm
) throws WrongTermTypeException
{
160 int orderInd
= lowerTerm
.orderIndex
;
161 termToBeAdded
.orderIndex
= orderInd
;
162 //increment all orderIndexes of terms below
163 Iterator
<T
> iterator
= terms
.iterator();
164 while(iterator
.hasNext()){
165 T term
= iterator
.next();
166 if (term
.orderIndex
>= orderInd
){ //should always be true
170 super.addTerm(termToBeAdded
);
173 public void addTermBelow(T termToBeAdded
, T higherTerm
) throws WrongTermTypeException
{
174 int orderInd
= higherTerm
.orderIndex
;
175 termToBeAdded
.orderIndex
= orderInd
+ 1;
176 //increment all orderIndexes of terms below
177 Iterator
<T
> iterator
= getLowerTerms(higherTerm
).iterator();
178 while(iterator
.hasNext()){
179 T term
= iterator
.next();
180 if (term
.orderIndex
> orderInd
){
184 super.addTerm(termToBeAdded
);
187 // public void addTermEqualLevel(T termToBeAdded, T equalLevelTerm) throws WrongTermTypeException {
188 // int orderInd = equalLevelTerm.orderIndex;
189 // termToBeAdded.orderIndex = orderInd;
190 // super.addTerm(termToBeAdded);
194 public void removeTerm(T term
) {
198 if (this.getEqualTerms(term
).size() == 0){
199 Iterator
<T
> iterator
= getLowerTerms(term
).iterator();
200 while (iterator
.hasNext()){
201 T otb
= iterator
.next();
202 toBeChangedByObject
= otb
;
203 otb
.decreaseIndex(this);
204 toBeChangedByObject
= null;
207 term
.setVocabulary(null);
210 private T toBeChangedByObject
;
212 public boolean indexChangeAllowed(T otb
){
213 return otb
== toBeChangedByObject
;