added fill missing epithets to CdmImportBase
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / MapWrapper.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
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 * @author a.mueller
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 //TODO
52 //service.save(cdmBase);
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 * Returns all values that are either stored in the wrapper or the database.
69 * If <code>service</code> is null then only the elements stored in the wrapper are returned.
70 * @return
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 //TODO Set<T> persitentAll = service.getAll();
80 //result.addAll(persistentALl);
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 //TODO from service
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 //logger.warn(uuid);
104 //TODO
105 //result = (T)service.getObjectUuid(uuid); //.getCdmObjectByUuid(uuid);// taxonService.getTaxonByUuid(taxonUuid);
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 }