1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.datastore;
20
21 import java.util.Comparator;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.TreeMap;
26
27 /***
28 * This implementation of DataIndex wraps a TreeMap and
29 * adds the ability to contain objects with duplicate keys.
30 */
31 public class DefaultDataIndex implements DataIndex
32 {
33 static final long serialVersionUID = -3759982714240822885L;
34
35 protected String name;
36 protected String property;
37 private TreeMap treeMap;
38 private Comparator comparator;
39
40 public DefaultDataIndex()
41 {
42 comparator = new DefaultComparator();
43 setTreeMap( new TreeMap( new DefaultComparator() ) );
44 }
45
46 public DefaultDataIndex( String aName, String aProperty )
47 {
48 this();
49 setName( aName );
50 setProperty( aProperty );
51 }
52
53
54 public Comparator getComparator() { return comparator; }
55 public void setComparator( Comparator aComparator )
56 {
57 comparator = aComparator;
58
59 TreeMap map = getTreeMap();
60 setTreeMap( new TreeMap( comparator ) );
61 getTreeMap().putAll( map );
62
63 }
64
65 public String getName() { return name; };
66 public void setName( String aName ) { name = aName; }
67 public String getProperty() { return property; };
68 public void setProperty( String aProperty ) { property = aProperty; }
69 public TreeMap getTreeMap() { return treeMap; }
70 public void setTreeMap( TreeMap aMap ) { treeMap = aMap; }
71
72 public List query( Object beginValue, Object endValue )
73 {
74
75 List result = new LinkedList();
76 if ( endValue == null )
77 {
78 if ( beginValue == null )
79 {
80
81 populateListFromIterator( result, treeMap.values().iterator() );
82 return result;
83 }
84
85
86 populateListFromIterator( result,
87 treeMap.tailMap( beginValue ).values().iterator() );
88 return result;
89 }
90 else
91 if ( beginValue == null )
92 {
93
94 populateListFromIterator( result,
95 treeMap.headMap( endValue ).values().iterator() );
96 }
97 else
98 {
99
100 populateListFromIterator( result,
101 treeMap.subMap( beginValue, endValue ).values().iterator() );
102 }
103
104
105 Object o = treeMap.get( endValue );
106 if ( o != null )
107 {
108 if ( o instanceof DuplicateList )
109 {
110 populateListFromIterator( result,
111 ((DuplicateList)o).iterator() );
112 }
113 else
114 {
115 result.add( o );
116 }
117 }
118
119
120 return result;
121 }
122
123 protected void populateListFromIterator( List aList, Iterator it )
124 {
125 Object o;
126 while ( it.hasNext() )
127 {
128 o = it.next();
129 if ( o instanceof DuplicateList )
130 {
131 populateListFromIterator(
132 aList, ((DuplicateList)o).iterator() );
133 }
134 else
135 {
136 aList.add( o );
137 }
138 }
139 }
140
141 public Object addObject( Object anObject, Object newValue )
142 {
143 Object o = treeMap.get( newValue );
144 if ( o != null )
145 {
146 if ( o instanceof DuplicateList )
147 {
148 ((DuplicateList)o).add( anObject );
149 return anObject;
150 }
151
152 DuplicateList list = new DuplicateList();
153 list.add( o );
154 list.add( anObject );
155 anObject = list;
156
157 }
158 if ( anObject == null ) new RuntimeException().printStackTrace();
159
160 treeMap.put( newValue, anObject );
161 return anObject;
162 }
163
164 public Object updateObject( Object anObject,
165 Object oldValue, Object newValue )
166 {
167 removeObject( anObject, oldValue );
168 return addObject( anObject, newValue );
169 }
170
171 public Object removeObject( Object anObject, Object oldValue )
172 {
173 Object o = treeMap.get( oldValue );
174 if ( o != null )
175 {
176 if ( o instanceof DuplicateList )
177 {
178
179 DuplicateList list = (DuplicateList) o;
180 list.remove( anObject );
181
182
183 if ( list.size() > 1 )
184 return anObject;
185
186
187 if ( list.size() == 0 )
188 {
189 System.out.println( "DefaultDataIndex.deleteObject: " + oldValue
190 + " : list size is 1 : this should never happen." );
191 return null;
192 }
193
194
195 treeMap.remove( oldValue );
196 treeMap.put( oldValue, list.getFirst() );
197 return anObject;
198 }
199
200
201 treeMap.remove( oldValue );
202 }
203 return anObject;
204 }
205
206 public void clear()
207 {
208 treeMap.clear();
209 }
210
211 public String toString()
212 {
213 return "DefaultDataIndex: " + name + " : " + property + " : " + treeMap.toString();
214 }
215
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239