KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > storagemodel > transientimpl > TransientMultivaluedOrderedIndex


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.mdr.storagemodel.transientimpl;
21
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.ListIterator JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import org.netbeans.mdr.persistence.MultivaluedOrderedIndex;
29 import org.netbeans.mdr.persistence.StorageException;
30 import org.netbeans.mdr.persistence.StorageBadRequestException;
31 import org.netbeans.mdr.persistence.Storage;
32 import org.netbeans.mdr.persistence.SinglevaluedIndex;
33 import org.netbeans.mdr.storagemodel.MdrStorage;
34 import org.netbeans.mdr.util.DebugException;
35
36
37
38 /**
39  *
40  * @author Tomas Zezula
41  */

42 public class TransientMultivaluedOrderedIndex extends TransientMultivaluedIndex implements MultivaluedOrderedIndex {
43     
44     public class OrderedSlotCollection extends SlotCollection implements List JavaDoc {
45         
46         
47         public OrderedSlotCollection(Object JavaDoc key, Collection JavaDoc st) {
48             this(key, st, null);
49         }
50         
51         public OrderedSlotCollection(Object JavaDoc key, Collection JavaDoc st, int index, int toIndex) {
52             this(key, st, null, index, toIndex);
53         }
54         
55         public OrderedSlotCollection(Object JavaDoc key, Collection JavaDoc st, SinglevaluedIndex repos) {
56             super(key, st, repos);
57         }
58         
59         public OrderedSlotCollection(Object JavaDoc key, Collection JavaDoc st, SinglevaluedIndex repos, int fromIndex, int toIndex) {
60             super(key, ((List JavaDoc)st).subList(fromIndex,toIndex), repos);
61         }
62         
63         public void add(int index, Object JavaDoc element) {
64             if (this.repos != null)
65                 throw new UnsupportedOperationException JavaDoc();
66             try {
67                 addToList((List JavaDoc)this.st, index, this.key, element);
68                 txlog.push( new CompensatingTransaction.AddOrderedCTx(this.key, element, index));
69             } catch (StorageException se) {
70                 throw new DebugException(se.toString());
71             }
72         }
73         
74         public boolean addAll(int index, Collection JavaDoc c) {
75             boolean result = false;
76             for (Iterator JavaDoc it = c.iterator(); it.hasNext(); index++) {
77                 this.add(index, it.next());
78                 result = true;
79             }
80             return result;
81         }
82         
83         public Object JavaDoc get(int index) {
84             Entry e = (Entry) ((List JavaDoc)this.st).get(index);
85             if (e == null)
86                 return null;
87             if (!e.isValid()) {
88                 ((List JavaDoc)this.st).remove(index);
89                 e.dispose();
90                 return null;
91             }
92             try {
93                 return (this.repos == null) ? e.getValue() : map(this.key, e.getValue(), this.repos);
94             } catch (StorageException se) {
95                 throw new DebugException(se.toString());
96             }
97         }
98         
99         public int indexOf(Object JavaDoc o) {
100             if (this.repos != null)
101                 throw new UnsupportedOperationException JavaDoc();
102             List JavaDoc l = (List JavaDoc) this.st;
103             for (int i= 0; i < l.size(); i++) {
104                 Entry e = (Entry) l.get(i);
105                 if (!e.isValid()) {
106                     l.remove(i);
107                     e.dispose();
108                     i--;
109                 }
110                 else if (o.equals(e.getValue())) {
111                     return i;
112                 }
113             }
114             return -1;
115         }
116         
117         public int lastIndexOf(Object JavaDoc o) {
118             if (this.repos != null)
119                 throw new UnsupportedOperationException JavaDoc();
120             List JavaDoc l = (List JavaDoc) this.st;
121             for (int i = l.size()-1; i>=0; i--) {
122                 Entry e = (Entry) l.get(i);
123                 if (!e.isValid()) {
124                     l.remove(i);
125                     e.dispose();
126                 }
127                 else if (o.equals(e.getValue())) {
128                     return i;
129                 }
130             }
131             return -1;
132         }
133         
134         public ListIterator JavaDoc listIterator() {
135             return new OrderedSlotIterator(this.key, (List JavaDoc)this.st, this.repos);
136         }
137         
138         
139         public ListIterator JavaDoc listIterator(int index) {
140             return new OrderedSlotIterator(this.key, (List JavaDoc)this.st, this.repos, index);
141         }
142         
143         public Object JavaDoc remove(int index) {
144             if (this.repos != null)
145                 throw new UnsupportedOperationException JavaDoc();
146             if (index < 0 || index >= this.st.size())
147                 throw new IndexOutOfBoundsException JavaDoc();
148             try {
149                 Object JavaDoc result = removeFromList((List JavaDoc)this.st, index);
150                 txlog.push( new CompensatingTransaction.RemoveOrderedCTx(this.key, result, index));
151                 return result;
152             } catch (StorageException se) {
153                 throw new DebugException(se.toString());
154             }
155         }
156         
157         
158         public Object JavaDoc set(int index, Object JavaDoc element) {
159             if (this.repos != null)
160                 throw new UnsupportedOperationException JavaDoc();
161             try {
162                 Object JavaDoc result = setInList((List JavaDoc)this.st, index, this.key, element);
163                 if (result != null) {
164                     txlog.push(new CompensatingTransaction.RemoveOrderedCTx(key, result, index));
165                 }
166                 txlog.push(new CompensatingTransaction.AddOrderedCTx(key, result, index));
167                 return result;
168             } catch (StorageException se) {
169                 throw new DebugException(se.toString());
170             }
171         }
172         
173         public List JavaDoc subList(int fromIndex, int toIndex) {
174             return new OrderedSlotCollection(this.key, this.st, this.repos, fromIndex, toIndex);
175         }
176         
177     }
178     
179     
180     protected class OrderedSlotIterator extends SlotIterator implements ListIterator JavaDoc {
181         
182         private Entry prev;
183         private int position;
184         
185         public OrderedSlotIterator(List JavaDoc l) {
186             this(null, l, null, 0);
187         }
188         
189         public OrderedSlotIterator(List JavaDoc l, int index) {
190             this(null, l, null, index);
191         }
192         
193         public OrderedSlotIterator(Object JavaDoc key, List JavaDoc l, SinglevaluedIndex repos) {
194             this(null, l, repos, 0);
195         }
196         
197         public OrderedSlotIterator(Object JavaDoc key, List JavaDoc l, SinglevaluedIndex repos, int index) {
198             super(key, l, repos, (index == 0) ? l.listIterator() : l.listIterator(index));
199         }
200         
201         public Object JavaDoc next() {
202             while (this.top == null) {
203                 this.top = (Entry) this.innerIt.next();
204                 if (! this.top.isValid()) {
205                     this.innerIt.remove();
206                     this.top.dispose();
207                     this.top = null;
208                 }
209             }
210             this.last = this.top;
211             this.top = prev = null;
212             try {
213                 Object JavaDoc result = map(this.key, this.last.getValue(), this.repos);
214                 this.position++;
215                 return result;
216             }catch (StorageException se) {
217                 throw new DebugException(se.toString());
218             }
219         }
220         
221         public boolean hasPrevious() {
222             while (this.prev == null) {
223                 if (!((ListIterator JavaDoc)this.innerIt).hasPrevious())
224                     return false;
225                 this.prev = (Entry) ((ListIterator JavaDoc)this.innerIt).previous();
226                 if (! this.prev.isValid()) {
227                     this.innerIt.remove();
228                     this.prev.dispose();
229                     this.prev = null;
230                 }
231             }
232             return true;
233         }
234         
235         public Object JavaDoc previous() {
236             while (this.prev == null) {
237                 this.prev = (Entry) ((ListIterator JavaDoc)this.innerIt).previous();
238                 if (! this.prev.isValid()) {
239                     this.innerIt.remove();
240                     this.prev.dispose();
241                     this.prev = null;
242                 }
243             }
244             this.last = this.prev;
245             this.top = prev = null;
246             try {
247                 Object JavaDoc result = map(this.key, this.last.getValue(), this.repos);
248                 this.position--;
249                 return result;
250             }catch (StorageException se) {
251                 throw new DebugException(se.toString());
252             }
253         }
254         
255         public int nextIndex() {
256             return ((ListIterator JavaDoc)this.innerIt).nextIndex();
257         }
258         
259         public int previousIndex() {
260             return ((ListIterator JavaDoc)this.innerIt).previousIndex();
261         }
262         
263         public void add(Object JavaDoc o) {
264             if (this.repos != null)
265                 throw new UnsupportedOperationException JavaDoc();
266             if (this.last == null)
267                 throw new IllegalStateException JavaDoc();
268             if (unique && this.collection.contains(o))
269                 throw new IllegalStateException JavaDoc("Object already contained in unique index.");
270             try {
271                 Entry e = new Entry(this.key, o);
272                 ((ListIterator JavaDoc)innerIt).add(e);
273                 txlog.push(new CompensatingTransaction.AddOrderedCTx(this.key, o, this.position));
274             }catch (StorageException se) {
275                 throw new DebugException();
276             }
277         }
278         
279         public void set(Object JavaDoc o) {
280             if (this.repos != null)
281                 throw new UnsupportedOperationException JavaDoc();
282             if (this.last == null)
283                 throw new IllegalStateException JavaDoc();
284             if (unique && this.collection.contains(o))
285                 throw new IllegalStateException JavaDoc("Object already contained in unique index.");
286             try {
287                 Entry e = new Entry(this.key, o);
288                 ((ListIterator JavaDoc)innerIt).set(e);
289                 txlog.push( new CompensatingTransaction.RemoveOrderedCTx(this.key,this.last.getValue(),this.position));
290                 txlog.push( new CompensatingTransaction.AddOrderedCTx(this.key, o, this.position));
291             } catch (StorageException se) {
292                 throw new DebugException();
293             }
294         }
295         
296     }
297     
298     
299     public TransientMultivaluedOrderedIndex(MdrStorage storage, String JavaDoc name, Storage.EntryType keyType, Storage.EntryType valueType, boolean unique) {
300         super(storage, name, keyType, valueType, unique);
301     }
302     
303     /** Inserts the specified element at the specified position in the list of values
304      * associated with the specified key.
305      * Throws StorageBadRequestException if the index is out of range.
306      * @param key
307      * @param index
308      * @param value
309      */

310     public void add(Object JavaDoc key, int index, Object JavaDoc value) throws StorageException {
311         if (this.map == null) {
312             this.map = new HashMap JavaDoc();
313         }
314         // Never expungeStaleEntries here, changes index
315
List JavaDoc c = (List JavaDoc) this.map.get(key);
316         if (c == null) {
317             c = new ArrayList JavaDoc();
318             this.map.put(key, c);
319         }
320         this.addToList(c, index, key, value);
321         this.txlog.push(new CompensatingTransaction.AddOrderedCTx(key, value, index));
322     }
323     
324     protected SlotCollection createSlotCollection (Object JavaDoc key, Collection JavaDoc c) {
325         return new OrderedSlotCollection (key, c);
326     }
327     
328     protected SlotCollection createSlotCollection (Object JavaDoc key, Collection JavaDoc c, SinglevaluedIndex repos) {
329         return new OrderedSlotCollection (key, c, repos);
330     }
331     
332     /** Returns a list view of the values assosiated in the index with specified key.
333      * Returned collection is read only and may not be modified.
334      * If there are no values associated with the key empty collection is returned.
335      * @param key
336      * @return
337      */

338     public java.util.List JavaDoc getItemsOrdered(Object JavaDoc key) throws StorageException {
339         return (List JavaDoc) this.getItems(key);
340     }
341     
342     /** Like getItemsOrdered, but if the index contains keys, this returns the objects
343      * corresponding to the key
344      * @return
345      * @param key
346      * @throws StorageException
347      */

348     public Collection JavaDoc getObjectsOrdered(Object JavaDoc key, SinglevaluedIndex repos) throws StorageException {
349         return this.getObjects(key, repos);
350     }
351     
352     /** Removes the element at the specified position in the list of values
353      * associated with the specified key.
354      * @return true if this index changed as a result of this call
355      * @param key
356      * @param index
357      */

358     public boolean remove(Object JavaDoc key, int index) throws StorageException {
359         if (this.map == null)
360             return false;
361         // Never expungeStaleEntries here, changes index
362
List JavaDoc list = (List JavaDoc) this.map.get(key);
363         Object JavaDoc result = this.removeFromList(list, index);
364         if (result != null)
365             this.txlog.push(new CompensatingTransaction.RemoveOrderedCTx(key, result, index));
366         return result != null;
367     }
368     
369     /** Replaces the element at the specified position in the list of values
370      * associated with the specified key with the specified element.
371      * Throws StorageBadRequestException if the index is out of range.
372      * @param key
373      * @param index
374      * @param element
375      * @throws StorageException
376      */

377     public void replace(Object JavaDoc key, int index, Object JavaDoc element) throws StorageException {
378         if (this.map == null)
379             throw new StorageBadRequestException();
380         // Never expungeStaleEntries here, changes index
381
List JavaDoc list = (List JavaDoc) this.map.get(key);
382         Object JavaDoc result = this.setInList(list, index, key, element);
383         if (result != null) {
384             this.txlog.push(new CompensatingTransaction.RemoveOrderedCTx(key, result, index));
385         }
386         this.txlog.push(new CompensatingTransaction.AddOrderedCTx(key, result, index));
387     }
388     
389     protected void addNoTx (Object JavaDoc key, Object JavaDoc value, int index) throws StorageException {
390         if (map == null)
391             return; // Healing
392
List JavaDoc l = (List JavaDoc) this.map.get (key);
393         this.addToList (l, index, key, value);
394     }
395     
396     protected Object JavaDoc removeNoTx (Object JavaDoc key, Object JavaDoc value, int index) throws StorageException {
397         if (map == null)
398             return null; //Healing
399
List JavaDoc l = (List JavaDoc) this.map.get (key);
400         return this.removeFromList (l, index);
401     }
402     
403     private void addToList(List JavaDoc c, int index, Object JavaDoc key, Object JavaDoc value) throws StorageException {
404         if (this.unique) {
405             for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
406                 Entry e = (Entry) it.next();
407                 if (value.equals(e.getValue()))
408                     throw new StorageBadRequestException("Value: "+value+" is already contained.");
409             }
410         }
411         Entry e = new Entry(key, value);
412         c.add(index, e);
413     }
414     
415     private Object JavaDoc removeFromList(List JavaDoc list, int index) throws StorageException {
416         if (list == null)
417             return null;
418         Entry e = (Entry) list.remove(index);
419         Object JavaDoc value = e.getValue();
420         e.dispose();
421         return value;
422     }
423     
424     private Object JavaDoc setInList(List JavaDoc list, int index,Object JavaDoc key, Object JavaDoc value) throws StorageException {
425         if (list == null)
426             throw new StorageBadRequestException();
427         Entry newEntry = new Entry(key, value);
428         Entry old = (Entry) list.set(index, newEntry);
429         if (old != null) {
430             old.dispose();
431             return old.getValue();
432         }
433         else
434             return null;
435     }
436     
437 }
438
Popular Tags