1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.model.common;
|
12
|
|
13
|
import java.util.HashMap;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
/**
|
20
|
* @author a.mueller
|
21
|
*
|
22
|
*/
|
23
|
public class EnumeratedTermVoc<T extends IEnumTerm<T>> {
|
24
|
|
25
|
private static Map<Class<? extends IEnumTerm<?>>,EnumeratedTermVoc<?>> vocsMap= new HashMap<Class<? extends IEnumTerm<?>>, EnumeratedTermVoc<?>>();
|
26
|
|
27
|
private final Map<T,SingleEnumTerm<T>> lookup = new HashMap<T, SingleEnumTerm<T>>();
|
28
|
|
29
|
// public interface EnumTerm<R extends EnumTerm<?>> extends ISimpleTerm<R>{
|
30
|
// public String getKey();
|
31
|
// }
|
32
|
|
33
|
private class SingleEnumTerm<S extends T> implements IEnumTerm<T>{
|
34
|
private S term;
|
35
|
private String label;
|
36
|
private UUID uuid;
|
37
|
private String key;
|
38
|
private Set<S> children = new HashSet<S>();
|
39
|
private S parent;
|
40
|
|
41
|
|
42
|
|
43
|
private SingleEnumTerm(S term, UUID uuid, String defaultString, String key, S parent){
|
44
|
this.term = term;
|
45
|
this.label = defaultString;
|
46
|
this.key = key;
|
47
|
this.uuid = uuid;
|
48
|
this.parent = parent;
|
49
|
SingleEnumTerm<T> parentSingleEnum = lookup.get(parent);
|
50
|
if (parentSingleEnum != null){
|
51
|
parentSingleEnum.children.add(term);
|
52
|
}
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
public UUID getUuid() { return uuid;}
|
57
|
@Override
|
58
|
public T getKindOf() {return parent;}
|
59
|
public T getTerm() {return term;}
|
60
|
@Override
|
61
|
public String getKey() {return key; }
|
62
|
|
63
|
@Override
|
64
|
public String getMessage() {return getMessage(Language.DEFAULT());}
|
65
|
|
66
|
@Override
|
67
|
public String getMessage(Language language) {
|
68
|
//TODO make multi-lingual
|
69
|
return label;
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
public Set<T> getGeneralizationOf() {
|
74
|
// return Collections.unmodifiableSet( children ); //TODO creates stack overflow
|
75
|
return new HashSet<T>(children);
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
public boolean isKindOf(T ancestor) {
|
80
|
if (parent == null || ancestor == null){
|
81
|
return false;
|
82
|
}else if (parent.equals(ancestor)){
|
83
|
return true;
|
84
|
}else{
|
85
|
return parent.isKindOf(ancestor);
|
86
|
}
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public Set<T> getGeneralizationOf(boolean recursive) {
|
91
|
Set<T> result = new HashSet<T>();
|
92
|
result.addAll(this.children);
|
93
|
if (recursive){
|
94
|
for (T child : this.children){
|
95
|
result.addAll(child.getGeneralizationOf());
|
96
|
}
|
97
|
}
|
98
|
return result;
|
99
|
}
|
100
|
|
101
|
} //end of inner class
|
102
|
|
103
|
//******************* DELEGATE NETHODS ************************
|
104
|
|
105
|
public String getKey(T term) {return lookup.get(term).getKey();}
|
106
|
|
107
|
public UUID getUuid(T term) {return lookup.get(term).getUuid();}
|
108
|
|
109
|
public T getKindOf(T term) {return lookup.get(term).getKindOf();}
|
110
|
|
111
|
public Set<T> getGeneralizationOf(T term) {return lookup.get(term).getGeneralizationOf();}
|
112
|
|
113
|
//******************* DELEGATE CLASS NETHODS ************************
|
114
|
|
115
|
|
116
|
public static <S extends IEnumTerm<?>> IEnumTerm addTerm(Class<? extends IEnumTerm<?>> clazz, S term, UUID uuid, String defaultString, String key, S parent){
|
117
|
if (vocsMap.get(clazz) == null){
|
118
|
vocsMap.put(clazz, new EnumeratedTermVoc());
|
119
|
}
|
120
|
IEnumTerm myTerm = vocsMap.get(clazz).add(term, uuid, defaultString, key, parent);
|
121
|
return myTerm;
|
122
|
}
|
123
|
|
124
|
private SingleEnumTerm<T> add(ISimpleTerm<?> term, UUID uuid, String defaultString, String key, ISimpleTerm<?> parent) {
|
125
|
SingleEnumTerm<T> singleTerm = new SingleEnumTerm<T>((T)term, uuid, defaultString, key, (T)parent);
|
126
|
if (containsKey(lookup, key)){
|
127
|
throw new RuntimeException(String.format("Key must be unique in enumeration but was not for '%s'", key));
|
128
|
}
|
129
|
if (containsUuid(lookup, uuid)){
|
130
|
throw new RuntimeException(String.format("UUID must be unique in enumeration but was not for '%s'", uuid));
|
131
|
}
|
132
|
lookup.put((T)term, singleTerm);
|
133
|
return singleTerm;
|
134
|
}
|
135
|
|
136
|
public boolean containsKey(Map<T, SingleEnumTerm<T>> lookup, String key) {
|
137
|
for (SingleEnumTerm<T> term : lookup.values()){
|
138
|
if (term.getKey().equals(key)){
|
139
|
return true;
|
140
|
}
|
141
|
}
|
142
|
return false;
|
143
|
}
|
144
|
|
145
|
public boolean containsUuid(Map<T, SingleEnumTerm<T>> lookup, UUID uuid) {
|
146
|
for (SingleEnumTerm<T> term : lookup.values()){
|
147
|
if (term.getUuid().equals(uuid)){
|
148
|
return true;
|
149
|
}
|
150
|
}
|
151
|
return false;
|
152
|
}
|
153
|
|
154
|
public static <R extends IEnumTerm<R>> R byKey(Class<R> clazz, String key) {
|
155
|
EnumeratedTermVoc<R> voc = getVoc(clazz);
|
156
|
return voc == null? null:voc.getByKey(key);
|
157
|
}
|
158
|
|
159
|
public static <R extends IEnumTerm<R>> R byUuid(Class<R> clazz, UUID uuid) {
|
160
|
EnumeratedTermVoc<R> voc = getVoc(clazz);
|
161
|
return voc == null? null:voc.getByUuid(uuid);
|
162
|
}
|
163
|
|
164
|
|
165
|
public T getByKey(String key) {
|
166
|
for (SingleEnumTerm<T> term : lookup.values()){
|
167
|
if (term.getKey().equals(key)){
|
168
|
return term.getTerm();
|
169
|
}
|
170
|
}
|
171
|
return null;
|
172
|
}
|
173
|
|
174
|
public T getByUuid(UUID uuid) {
|
175
|
for (SingleEnumTerm<T> term : lookup.values()){
|
176
|
if (term.getUuid().equals(uuid)){
|
177
|
return term.getTerm();
|
178
|
}
|
179
|
}
|
180
|
return null;
|
181
|
}
|
182
|
|
183
|
public static <R extends IEnumTerm<R>> EnumeratedTermVoc<R> getVoc(Class<R> clazz) {
|
184
|
return (EnumeratedTermVoc<R>)vocsMap.get(clazz);
|
185
|
}
|
186
|
|
187
|
|
188
|
|
189
|
|
190
|
|
191
|
}
|