01: // Space4J(TM) - Object Persistence in RAM
02: // Copyright (C) 2003 Sergio Oliveira Junior
03: // This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
04:
05: package org.space4j.implementation;
06:
07: import org.space4j.*;
08: import org.space4j.indexing.*;
09:
10: import java.util.*;
11:
12: /**
13: * A trivial implementation of Space, in other words, our space just use a Map.
14: */
15: public class SimpleSpace implements Space {
16:
17: private Map map = null;
18:
19: /** Initializes the SimpleSpace. */
20: public SimpleSpace() {
21: map = new HashMap();
22: }
23:
24: /** Initializes the SimpleSpace with a Map. */
25: public SimpleSpace(Map map) {
26: this .map = map;
27: }
28:
29: public Object getObject(Object key) {
30: return map.get(key);
31: }
32:
33: public void putObject(Object key, Object obj) {
34: map.put(key, obj);
35: }
36:
37: public Iterator getSafeIterator(Object key) {
38: Object obj = getObject(key);
39: if (obj instanceof Map) {
40: Map map = (Map) obj;
41: ArrayList list = new ArrayList(map.values());
42: return list.iterator();
43: } else if (obj instanceof Collection) {
44: Collection coll = (Collection) obj;
45: ArrayList list = new ArrayList(coll);
46: return list.iterator();
47: }
48: return null;
49: }
50:
51: public Iterator getSafeKeyIterator(Object key) {
52: Object obj = getObject(key);
53: if (obj instanceof Map) {
54: Map map = (Map) obj;
55: Object[] array = map.keySet().toArray();
56: ArrayList list = new ArrayList(array.length);
57: for (int i = 0; i < array.length; i++)
58: list.add(array[i]);
59: return list.iterator();
60: }
61: return null;
62: }
63:
64: public IndexManager getIndexManager() {
65: return (IndexManager) getObject("sys_IndexManager");
66: }
67:
68: }
|