Project

General

Profile

« Previous | Next » 

Revision d4e134f2

Added by Patrick Plitzner about 9 years ago

  • fixed recursive method

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/EnumeratedTermVoc.java
23 23
public class EnumeratedTermVoc<T extends IEnumTerm<T>> {
24 24

  
25 25
	private static Map<Class<? extends IEnumTerm<?>>,EnumeratedTermVoc<?>> vocsMap= new HashMap<Class<? extends IEnumTerm<?>>, EnumeratedTermVoc<?>>();
26
	
26

  
27 27
	private final Map<T,SingleEnumTerm<T>> lookup = new HashMap<T, SingleEnumTerm<T>>();
28
	
28

  
29 29
//	public interface EnumTerm<R extends EnumTerm<?>> extends ISimpleTerm<R>{
30 30
//		public String getKey();
31 31
//	}
32
	
32

  
33 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;
34
		private final S term;
35
		private final String label;
36
		private final UUID uuid;
37
		private final String key;
38
		private final Set<S> children = new HashSet<S>();
39
		private final S parent;
40

  
40 41

  
41 42

  
42
		
43 43
		private	SingleEnumTerm(S term, UUID uuid, String defaultString, String key, S parent){
44 44
			this.term = term;
45 45
			this.label = defaultString;
46 46
			this.key = key;
47 47
			this.uuid = uuid;
48 48
			this.parent = parent;
49
			SingleEnumTerm<T> parentSingleEnum = lookup.get(parent); 
49
			SingleEnumTerm<T> parentSingleEnum = lookup.get(parent);
50 50
			if (parentSingleEnum != null){
51 51
				parentSingleEnum.children.add(term);
52 52
			}
53 53
		}
54
		
54

  
55 55
		@Override
56 56
		public UUID getUuid() {	return uuid;}
57 57
		@Override
......
68 68
			//TODO make multi-lingual
69 69
			return label;
70 70
		}
71
		
71

  
72 72
		@Override
73 73
		public Set<T> getGeneralizationOf() {
74 74
//			return Collections.unmodifiableSet( children );   //TODO creates stack overflow
......
92 92
			result.addAll(this.children);
93 93
			if (recursive){
94 94
				for (T child : this.children){
95
					result.addAll(child.getGeneralizationOf());
95
					result.addAll(child.getGeneralizationOf(recursive));
96 96
				}
97 97
			}
98 98
			return result;
......
105 105
	public String getKey(T term) {return lookup.get(term).getKey();}
106 106

  
107 107
	public UUID getUuid(T term) {return lookup.get(term).getUuid();}
108
	
108

  
109 109
	public T getKindOf(T term) {return lookup.get(term).getKindOf();}
110 110

  
111 111
	public Set<T> getGeneralizationOf(T term) {return lookup.get(term).getGeneralizationOf();}
112 112

  
113 113
//******************* DELEGATE CLASS NETHODS ************************
114 114

  
115
	
115

  
116 116
	public static <S extends IEnumTerm<?>> IEnumTerm addTerm(Class<? extends IEnumTerm<?>> clazz, S term, UUID uuid, String defaultString, String key, S parent){
117 117
		if (vocsMap.get(clazz) == null){
118 118
			vocsMap.put(clazz, new EnumeratedTermVoc());
......
120 120
		IEnumTerm myTerm = vocsMap.get(clazz).add(term, uuid, defaultString, key, parent);
121 121
		return myTerm;
122 122
	}
123
	
123

  
124 124
	private  SingleEnumTerm<T> add(ISimpleTerm<?> term, UUID uuid, String defaultString, String key, ISimpleTerm<?> parent) {
125 125
		SingleEnumTerm<T> singleTerm = new SingleEnumTerm<T>((T)term, uuid, defaultString, key, (T)parent);
126 126
		if (containsKey(lookup, key)){
......
161 161
		return voc == null? null:voc.getByUuid(uuid);
162 162
	}
163 163

  
164
	
164

  
165 165
	public T getByKey(String key) {
166 166
		for (SingleEnumTerm<T> term : lookup.values()){
167 167
			if (term.getKey().equals(key)){
......
170 170
		}
171 171
		return null;
172 172
	}
173
	
173

  
174 174
	public T getByUuid(UUID uuid) {
175 175
		for (SingleEnumTerm<T> term : lookup.values()){
176 176
			if (term.getUuid().equals(uuid)){
......
186 186

  
187 187

  
188 188

  
189
	
189

  
190 190

  
191 191
}

Also available in: Unified diff