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.
10 package eu
.etaxonomy
.cdm
.io
.common
;
12 import java
.util
.HashMap
;
13 import java
.util
.HashSet
;
15 import java
.util
.Collection
;
17 import java
.util
.UUID
;
20 import org
.apache
.log4j
.Logger
;
22 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 public class MapWrapper
<T
extends CdmBase
> {
30 private static Logger logger
= Logger
.getLogger(MapWrapper
.class);
32 private Map internalMap
;
33 private IService
<CdmBase
> service
= null;
35 public MapWrapper(IService
<CdmBase
> service
){
39 public void put(Object id
, T cdmBase
){
41 throw new RuntimeException();
43 internalMap
.put(id
, cdmBase
);
47 public void put(Object id
, UUID uuid
){
49 throw new RuntimeException();
52 //service.save(cdmBase);
53 internalMap
.put(id
, uuid
);
57 public T
get(Object id
){
60 result
= (T
)internalMap
.get(id
);
62 result
= getObjectFromService(id
);
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.
72 public Set
<T
> getAllValues(){
73 Set
<T
> result
= new HashSet
<T
>();
75 result
.addAll(internalMap
.values());
77 result
.addAll(internalMap
.values());
78 logger
.warn("getAll not yet implemented !!");
79 //TODO Set<T> persitentAll = service.getAll();
80 //result.addAll(persistentALl);
85 public boolean containsId(Object id
){
86 return internalMap
.containsKey(id
);
89 public Collection
<T
> objects(){
91 return (Collection
<T
>)internalMap
.values();
94 private T
getObjectFromService(Object id
){
96 throw new RuntimeException("no service defined");
99 UUID uuid
= (UUID
)internalMap
.get(id
);
105 //result = (T)service.getObjectUuid(uuid); //.getCdmObjectByUuid(uuid);// taxonService.getTaxonByUuid(taxonUuid);
111 public boolean makeEmpty(){
112 return makeNewMap(service
);
115 public boolean makeNewMap(IService
<CdmBase
> service
){
116 if (service
== null){
117 internalMap
= new HashMap
<Integer
, CdmBase
>();
119 this.service
= service
;
120 internalMap
= new HashMap
<Integer
, UUID
>();
126 return internalMap
.size();
129 public Collection
<T
> objects(int start
, int limit
) {
131 Map internalPartMap
= new HashMap
<Integer
, CdmBase
>();
134 for (int i
= 0; i
< limit
; i
++) {
138 Object object
= internalMap
.get(j
);
140 internalPartMap
.put(index
, internalMap
.get(j
));
143 if (logger
.isDebugEnabled()) { logger
.debug("Object (" + j
+ ") is null"); }
146 return (Collection
<T
>)internalPartMap
.values();
150 public Collection
<T
> removeObjects(int start
, int limit
) {
152 for (int i
= start
; i
< start
+ limit
; i
++) {
153 internalMap
.remove(i
);
154 if (logger
.isDebugEnabled()) { logger
.debug("Object (" + i
+ ") removed"); }
156 return (Collection
<T
>)internalMap
.values();
160 public Set
<Object
> keySet() {
161 return internalMap
.keySet();