KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > storagemodel > AssocEndIndexUList


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 package org.netbeans.mdr.storagemodel;
20
21 import java.util.*;
22
23 import javax.jmi.reflect.*;
24
25 import org.netbeans.mdr.handlers.BaseObjectHandler;
26 import org.netbeans.mdr.persistence.*;
27 import org.netbeans.mdr.util.DebugException;
28 import org.netbeans.mdr.util.Logger;
29
30 /**
31  *
32  * @author Martin Matula
33  */

34 public class AssocEndIndexUList extends AssocEndIndexSet implements List {
35     private final MultivaluedOrderedIndex orderedIndex;
36
37     protected AssocEndIndexUList(StorableAssociation storable, MOFID metaMofId, MOFID metaMofIdOther, MultivaluedOrderedIndex index, Object JavaDoc indexKey, Index secondIndex, Class JavaDoc type, int max, boolean mutable, boolean isAggregate, boolean isAggregateOther, boolean isIndexed, boolean isIndexedOther) {
38         super(storable, metaMofId, metaMofIdOther, index, indexKey, secondIndex, type, max, mutable, isAggregate, isAggregateOther, isIndexed, isIndexedOther);
39         this.orderedIndex = index;
40     }
41     
42     protected List getItemsList() {
43         try {
44             return orderedIndex.getItemsOrdered(indexKey);
45         } catch (StorageException e) {
46             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
47         }
48     }
49     
50     protected List getObjectsList() {
51         try {
52             return (List) storage.getObjectsFromIndex(index, indexKey);
53
54         } catch (StorageException e) {
55             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
56         }
57     }
58     
59     public Object JavaDoc get(int param) {
60         return getObjectsList().get(param);
61     }
62     
63     public ListIterator listIterator() {
64         return new IndexListIterator (getObjectsList().listIterator());
65     }
66     
67     public ListIterator listIterator(int param) {
68         return new IndexListIterator (getObjectsList().listIterator(param));
69     }
70     
71     public int indexOf(Object JavaDoc obj) {
72         return getItemsList().indexOf(obj);
73     }
74     
75     public int lastIndexOf(Object JavaDoc obj) {
76         return getItemsList().lastIndexOf(obj);
77     }
78     
79     public List subList(int param, int param1) {
80         throw new UnsupportedOperationException JavaDoc();
81 // return new SubListImpl(param, param1, listIterator(param));
82
}
83
84     public Object JavaDoc set(int index, Object JavaDoc object) {
85         try {
86             Object JavaDoc oldValue = ((List) getItems()).get(index);
87
88             if (isAggregate) {
89                 keyObject.clearComposite();
90                 keyObject.setComposite((MOFID) object, (MOFID) object, metaMofId);
91             } else if (isAggregateOther) {
92                 ((StorableObject) storage.getObject((MOFID) oldValue)).clearComposite();
93                 ((StorableObject) storage.getObject((MOFID) object)).setComposite(keyObject, (MOFID) object, metaMofId);
94             }
95
96             if (isIndexed) {
97                 keyObject.removeFromIndex (metaMofId);
98             }
99             if (isIndexedOther) {
100                 ((StorableObject) storage.getObject((MOFID) oldValue)).removeFromIndex (metaMofIdOther);
101                 ((StorableObject) storage.getObject((MOFID) object)).removeFromIndex (metaMofIdOther);
102             }
103             
104             orderedIndex.replace(indexKey, index, object);
105             if (secondIndex instanceof SinglevaluedIndex) {
106                 secondIndex.remove(oldValue);
107             } else {
108                 ((MultivaluedIndex) secondIndex).remove(oldValue, indexKey);
109             }
110             secondIndex.add(object, indexKey);
111             
112             if (isIndexed) {
113                 keyObject.addToIndex (metaMofId);
114             }
115             if (isIndexedOther) {
116                 ((StorableObject) storage.getObject((MOFID) oldValue)).addToIndex (metaMofIdOther);
117                 ((StorableObject) storage.getObject((MOFID) object)).addToIndex (metaMofIdOther);
118             }
119             
120             return storage.getObject((MOFID) oldValue);
121         } catch (StorageException e) {
122             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
123         }
124     }
125     
126     public void add(int index, Object JavaDoc element) {
127         try {
128             checkMaxSize(1);
129             StorableObject so = null;
130             if (isAggregate) {
131                 keyObject.setComposite((MOFID) element, (MOFID) element, metaMofId);
132             } else if (isAggregateOther) {
133                 if (so == null)
134                     so = (StorableObject) storage.getObject((MOFID) element);
135                 so.setComposite(keyObject, (MOFID) element, metaMofId);
136             }
137             if (isIndexed) {
138                 keyObject.removeFromIndex (metaMofId);
139             }
140             if (isIndexedOther) {
141                 if (so == null)
142                     so = (StorableObject) storage.getObject((MOFID) element);
143                 so.removeFromIndex (metaMofIdOther);
144             }
145
146             try {
147                 orderedIndex.add(indexKey, index, element);
148             } catch (StorageBadRequestException e) {
149                 throw new IndexOutOfBoundsException JavaDoc();
150             }
151             secondIndex.add(element, indexKey);
152             
153             if (isIndexed) {
154                 keyObject.addToIndex (metaMofId);
155             }
156             if (isIndexedOther) {
157                 so.addToIndex (metaMofIdOther);
158             }
159         } catch (StorageException e) {
160             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
161         }
162     }
163     
164     public Object JavaDoc remove(int index) {
165         try {
166             Object JavaDoc oldValue = getItemsList().get(index);
167             StorableObject so = null;
168             if (isAggregate) {
169                 keyObject.clearComposite();
170             } else if (isAggregateOther) {
171                 if (so == null)
172                     so = (StorableObject) storage.getObject((MOFID) oldValue);
173                 so.clearComposite();
174             }
175             
176             if (isIndexed) {
177                 keyObject.removeFromIndex (metaMofId);
178             }
179             if (isIndexedOther) {
180                 if (so == null)
181                     so = (StorableObject) storage.getObject((MOFID) oldValue);
182                 so.removeFromIndex (metaMofIdOther);
183             }
184             
185             orderedIndex.remove(indexKey, index);
186             if (secondIndex instanceof SinglevaluedIndex) {
187                 secondIndex.remove(oldValue);
188             } else {
189                 ((MultivaluedIndex) secondIndex).remove(oldValue, indexKey);
190             }
191             
192             if (isIndexed) {
193                 keyObject.addToIndex (metaMofId);
194             }
195             if (isIndexedOther) {
196                 so.addToIndex (metaMofIdOther);
197             }
198             
199             return storage.getObject((MOFID) oldValue);
200         } catch (StorageException e) {
201             throw new DebugException();
202         }
203     }
204     
205     public boolean addAll(int param, Collection collection) {
206         // should never be called
207
throw new DebugException();
208     }
209     
210     protected class IndexListIterator extends IndexIterator implements ListIterator {
211         private final ListIterator listIterator;
212         
213         protected IndexListIterator(ListIterator innerIterator) {
214             super(innerIterator);
215             listIterator = innerIterator;
216         }
217         
218         public void add(Object JavaDoc obj) {
219             listIterator.add(obj);
220             try {
221                 if (isAggregate) {
222                     keyObject.setComposite((MOFID) obj, (MOFID) obj, metaMofId);
223                 } else if (isAggregateOther) {
224                     ((StorableObject) storage.getObject((MOFID) obj)).setComposite(keyObject, (MOFID) obj, metaMofId);
225                 }
226                 if (isIndexed) {
227                     keyObject.removeFromIndex (metaMofId);
228                 }
229                 if (isIndexedOther) {
230                     ((StorableObject) storage.getObject((MOFID) obj)).removeFromIndex (metaMofIdOther);
231                 }
232                 secondIndex.add(obj, indexKey);
233                 if (isIndexed) {
234                     keyObject.addToIndex (metaMofId);
235                 }
236                 if (isIndexedOther) {
237                     ((StorableObject) storage.getObject((MOFID) obj)).addToIndex (metaMofIdOther);
238                 }
239             } catch (StorageException e) {
240                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
241             }
242         }
243         
244         public boolean hasPrevious() {
245             return listIterator.hasPrevious();
246         }
247         
248         public int nextIndex() {
249             return listIterator.nextIndex();
250         }
251         
252         public Object JavaDoc previous() {
253             return (lastRead = listIterator.previous());
254         }
255         
256         public int previousIndex() {
257             return listIterator.previousIndex();
258         }
259         
260         public void set(Object JavaDoc obj) {
261             MOFID key = ((StorableBaseObject) lastRead).getMofId ();
262             listIterator.set(obj);
263             try {
264                 if (isAggregate) {
265                     keyObject.clearComposite();
266                     keyObject.setComposite((MOFID) obj, (MOFID) obj, metaMofId);
267                 } else if (isAggregateOther) {
268                     ((StorableObject) lastRead).clearComposite();
269                     ((StorableObject) lastRead).setComposite(keyObject, (MOFID) key, metaMofId);
270                 }
271              
272                 if (isIndexed) {
273                     keyObject.removeFromIndex (metaMofId);
274                 }
275                 if (isIndexedOther) {
276                     ((StorableObject) lastRead).removeFromIndex (metaMofIdOther);
277                     ((StorableObject) storage.getObject((MOFID) obj)).removeFromIndex (metaMofIdOther);
278                 }
279                 
280                 if (secondIndex instanceof MultivaluedIndex) {
281                     ((MultivaluedIndex) secondIndex).remove(key, indexKey);
282                 } else {
283                     secondIndex.remove(key);
284                 }
285                 secondIndex.add(obj, indexKey);
286                 
287                 if (isIndexed) {
288                     keyObject.addToIndex (metaMofId);
289                 }
290                 if (isIndexedOther) {
291                     ((StorableObject) lastRead).addToIndex (metaMofIdOther);
292                     ((StorableObject) storage.getObject((MOFID) obj)).addToIndex (metaMofIdOther);
293                 }
294                 
295             } catch (StorageException e) {
296                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
297             }
298         }
299     }
300 }
301
Popular Tags