2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.common
;
11 import java
.util
.Collection
;
12 import java
.util
.HashMap
;
13 import java
.util
.HashSet
;
16 import java
.util
.UUID
;
18 import org
.apache
.logging
.log4j
.LogManager
;
19 import org
.apache
.logging
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 public class MapWrapper
<T
extends CdmBase
> {
29 private static final Logger logger
= LogManager
.getLogger();
31 private Map internalMap
;
32 private IService
<CdmBase
> service
= null;
34 public MapWrapper(IService
<CdmBase
> service
){
38 public void put(Object id
, T cdmBase
){
40 throw new RuntimeException();
42 internalMap
.put(id
, cdmBase
);
46 public void put(Object id
, UUID uuid
){
48 throw new RuntimeException();
51 //service.save(cdmBase);
52 internalMap
.put(id
, uuid
);
56 public T
get(Object id
){
59 result
= (T
)internalMap
.get(id
);
61 result
= getObjectFromService(id
);
67 * Returns all values that are either stored in the wrapper or the database.
68 * If <code>service</code> is null then only the elements stored in the wrapper are returned.
71 public Set
<T
> getAllValues(){
72 Set
<T
> result
= new HashSet
<T
>();
74 result
.addAll(internalMap
.values());
76 result
.addAll(internalMap
.values());
77 logger
.warn("getAll not yet implemented !!");
78 //TODO Set<T> persitentAll = service.getAll();
79 //result.addAll(persistentALl);
84 public boolean containsId(Object id
){
85 return internalMap
.containsKey(id
);
88 public Collection
<T
> objects(){
90 return internalMap
.values();
93 private T
getObjectFromService(Object id
){
95 throw new RuntimeException("no service defined");
98 UUID uuid
= (UUID
)internalMap
.get(id
);
104 //result = (T)service.getObjectUuid(uuid); //.getCdmObjectByUuid(uuid);// taxonService.getTaxonByUuid(taxonUuid);
110 public boolean makeEmpty(){
111 return makeNewMap(service
);
114 public boolean makeNewMap(IService
<CdmBase
> service
){
115 if (service
== null){
116 internalMap
= new HashMap
<Integer
, CdmBase
>();
118 this.service
= service
;
119 internalMap
= new HashMap
<Integer
, UUID
>();
125 return internalMap
.size();
128 public Collection
<T
> objects(int start
, int limit
) {
130 Map internalPartMap
= new HashMap
<Integer
, CdmBase
>();
133 for (int i
= 0; i
< limit
; i
++) {
137 Object object
= internalMap
.get(j
);
139 internalPartMap
.put(index
, internalMap
.get(j
));
142 if (logger
.isDebugEnabled()) { logger
.debug("Object (" + j
+ ") is null"); }
145 return internalPartMap
.values();
149 public Collection
<T
> removeObjects(int start
, int limit
) {
151 for (int i
= start
; i
< start
+ limit
; i
++) {
152 internalMap
.remove(i
);
153 if (logger
.isDebugEnabled()) { logger
.debug("Object (" + i
+ ") removed"); }
155 return internalMap
.values();
159 public Set
<Object
> keySet() {
160 return internalMap
.keySet();