1
2
3
4
5
6
7
8
9
10 package eu.etaxonomy.cdm.io.common;
11
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Collection;
16 import java.util.Set;
17 import java.util.UUID;
18
19
20 import org.apache.log4j.Logger;
21
22 import eu.etaxonomy.cdm.api.service.IService;
23 import eu.etaxonomy.cdm.model.common.CdmBase;
24
25
26
27
28
29 public class MapWrapper<T extends CdmBase> {
30 private static Logger logger = Logger.getLogger(MapWrapper.class);
31
32 private Map internalMap;
33 private IService<CdmBase> service = null;
34
35 public MapWrapper(IService<CdmBase> service){
36 makeNewMap(service);
37 }
38
39 public void put(Object id, T cdmBase){
40 if (service != null){
41 throw new RuntimeException();
42 }else{
43 internalMap.put(id, cdmBase);
44 }
45 }
46
47 public void put(Object id, UUID uuid){
48 if (service == null){
49 throw new RuntimeException();
50 }else{
51
52
53 internalMap.put(id, uuid);
54 }
55 }
56
57 public T get(Object id){
58 T result;
59 if (service == null){
60 result = (T)internalMap.get(id);
61 }else{
62 result = getObjectFromService(id);
63 }
64 return result;
65 }
66
67
68
69
70
71
72 public Set<T> getAllValues(){
73 Set<T> result = new HashSet<T>();
74 if (service == null){
75 result.addAll(internalMap.values());
76 }else{
77 result.addAll(internalMap.values());
78 logger.warn("getAll not yet implemented !!");
79
80
81 }
82 return result;
83 }
84
85 public boolean containsId(Object id){
86 return internalMap.containsKey(id);
87 }
88
89 public Collection<T> objects(){
90
91 return (Collection<T>)internalMap.values();
92 }
93
94 private T getObjectFromService(Object id){
95 if (service == null){
96 throw new RuntimeException("no service defined");
97 }else{
98 T result = null;
99 UUID uuid = (UUID)internalMap.get(id);
100 if (uuid == null){
101 result = null;
102 }else{
103
104
105
106 }
107 return result;
108 }
109 }
110
111 public boolean makeEmpty(){
112 return makeNewMap(service);
113 }
114
115 public boolean makeNewMap(IService<CdmBase> service){
116 if (service == null){
117 internalMap = new HashMap<Integer, CdmBase>();
118 }else{
119 this.service = service;
120 internalMap = new HashMap<Integer, UUID>();
121 }
122 return true;
123 }
124
125 public int size() {
126 return internalMap.size();
127 }
128
129 public Collection<T> objects(int start, int limit) {
130
131 Map internalPartMap = new HashMap<Integer, CdmBase>();
132 int index = 0;
133
134 for (int i = 0; i < limit; i++) {
135
136 int j = start + i;
137
138 Object object = internalMap.get(j);
139 if(object != null) {
140 internalPartMap.put(index, internalMap.get(j));
141 index++;
142 } else {
143 if (logger.isDebugEnabled()) { logger.debug("Object (" + j + ") is null"); }
144 }
145 }
146 return (Collection<T>)internalPartMap.values();
147 }
148
149
150 public Collection<T> removeObjects(int start, int limit) {
151
152 for (int i = start; i < start + limit; i++) {
153 internalMap.remove(i);
154 if (logger.isDebugEnabled()) { logger.debug("Object (" + i + ") removed"); }
155 }
156 return (Collection<T>)internalMap.values();
157 }
158
159
160 public Set<Object> keySet() {
161 return internalMap.keySet();
162 }
163
164 }