KickJava   Java API By Example, From Geeks To Geeks.

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


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

34 public class AssocEndIndexSet extends IndexImmutSet implements TypedCollection {
35     protected final MOFID metaMofId;
36     protected final MOFID metaMofIdOther;
37     protected final Class JavaDoc type;
38     protected final int maxSize;
39     protected final StorableAssociation storable;
40     protected final Index secondIndex;
41     protected final boolean mutable;
42     protected final boolean isAggregate;
43     protected final boolean isAggregateOther;
44     protected final boolean isIndexed;
45     protected final boolean isIndexedOther;
46     protected final StorableObject keyObject;
47
48     protected AssocEndIndexSet(StorableAssociation storable, MOFID metaMofId, MOFID metaMofIdOther, MultivaluedIndex index, Object JavaDoc indexKey, Index secondIndex, Class JavaDoc type, int max, boolean mutable, boolean isAggregate, boolean isAggregateOther, boolean isIndexed, boolean isIndexedOther) {
49         super(storable.getMdrStorage(), index, indexKey);
50         this.metaMofId = metaMofId;
51         this.metaMofIdOther = metaMofIdOther;
52         this.type = type;
53         this.maxSize = max;
54         this.storable = storable;
55         this.secondIndex = secondIndex;
56         this.mutable = mutable;
57         this.isAggregate = isAggregate;
58         this.isAggregateOther = isAggregateOther;
59         this.isIndexed = isIndexed;
60         this.isIndexedOther = isIndexedOther;
61         
62         if (isAggregate || isAggregateOther || isIndexed) {
63             try {
64                 keyObject = (StorableObject) storage.getObject((MOFID) indexKey);
65             } catch (StorageException e) {
66                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
67             }
68         } else {
69             keyObject = null;
70         }
71     }
72
73     public void checkType(Object JavaDoc obj) {
74         if (!mutable) throw new UnsupportedOperationException JavaDoc();
75         
76         if (obj == null) {
77             throw new NullPointerException JavaDoc();
78         }
79         
80         if (!type.isInstance(obj)) {
81             throw new TypeMismatchException(type, obj, getMetaElement());
82         }
83     }
84     
85     protected void checkMaxSize(int size) {
86         if (type == null) {
87             throw new UnsupportedOperationException JavaDoc();
88         } else if (maxSize != -1) {
89             if (maxSize < size + size()) throw new WrongSizeException(getMetaElement());
90         }
91     }
92
93     protected RefObject getMetaElement() {
94         try {
95             return (RefObject) storage.getRepository().getHandler(storage.getObject(metaMofId));
96         } catch (Exception JavaDoc e) {
97             return null;
98         }
99     }
100     
101     public Iterator iterator() {
102         return new IndexIterator(getObjects().iterator());
103     }
104     
105     public boolean remove(Object JavaDoc o) {
106         try {
107             StorableObject so = null;
108             
109             if (isAggregate) {
110                 keyObject.clearComposite();
111             } else if (isAggregateOther) {
112                 if (so == null)
113                     so = (StorableObject) storage.getObject((MOFID) o);
114                 so.clearComposite();
115             }
116             
117             if (isIndexed) {
118                 keyObject.removeFromIndex (metaMofId);
119             }
120             if (isIndexedOther) {
121                 if (so == null)
122                     so = (StorableObject) storage.getObject((MOFID) o);
123                 so.removeFromIndex (metaMofIdOther);
124             }
125                         
126             boolean result = this.index.remove(indexKey, o);
127             if (result) {
128                 if (secondIndex instanceof SinglevaluedIndex) {
129                     secondIndex.remove(o);
130                 } else {
131                     ((MultivaluedIndex) secondIndex).remove(o, indexKey);
132                 }
133             }
134             if (isIndexed) {
135                 keyObject.addToIndex (metaMofId);
136             }
137             if (isIndexedOther) {
138                 so.addToIndex (metaMofIdOther);
139             }
140             
141             return result;
142         } catch (StorageException e) {
143             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
144         }
145     }
146     
147     public boolean add(Object JavaDoc obj) {
148         checkMaxSize(1);
149         try {
150             StorableObject so = null;
151             if (isAggregate) {
152                 keyObject.setComposite((MOFID) obj, (MOFID) obj, metaMofId);
153             } else if (isAggregateOther) {
154                 if (so == null)
155                     so = (StorableObject) storage.getObject((MOFID) obj);
156                 so.setComposite(keyObject, (MOFID) obj, metaMofId);
157             }
158             if (isIndexed) {
159                 keyObject.removeFromIndex (metaMofId);
160             }
161             if (isIndexedOther) {
162                 if (so == null)
163                     so = (StorableObject) storage.getObject((MOFID) obj);
164                 so.removeFromIndex (metaMofIdOther);
165             }
166             index.add(indexKey, obj);
167             secondIndex.add(obj, indexKey);
168             if (isIndexed) {
169                 keyObject.addToIndex (metaMofId);
170             }
171             if (isIndexedOther) {
172                 so.addToIndex (metaMofIdOther);
173             }
174             return true;
175         } catch (StorageBadRequestException e) {
176             // duplicate element -> return false
177
return false;
178         } catch (StorageException e) {
179             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
180         }
181     }
182     
183     public boolean removeAll(Collection collection) {
184         // should never be called
185
throw new DebugException();
186     }
187     
188     public boolean addAll(Collection collection) {
189         // should never be called
190
throw new DebugException();
191     }
192     
193     public boolean retainAll(Collection collection) {
194         // should never be called
195
throw new DebugException();
196     }
197     
198     public void clear() {
199         // should never be called
200
throw new DebugException();
201     }
202     
203     protected class IndexIterator extends IndexImmutIterator {
204         protected Object JavaDoc lastRead = null;
205         
206         protected IndexIterator(Iterator innerIterator) {
207             super(innerIterator);
208         }
209         
210         public Object JavaDoc next() {
211             return (lastRead = super.next());
212         }
213         
214         public void remove() {
215             innerIterator.remove();
216             try {
217                 if (isAggregate) {
218                     keyObject.clearComposite();
219                 } else if (isAggregateOther) {
220                     ((StorableObject) lastRead).clearComposite();
221                 }
222
223                 if (isIndexed) {
224                     keyObject.removeFromIndex (metaMofId);
225                 }
226                 if (isIndexedOther) {
227                     ((StorableObject) lastRead).removeFromIndex (metaMofIdOther);
228                 }
229                 
230                 MOFID key = ((StorableBaseObject) lastRead).getMofId ();
231                 if (secondIndex instanceof MultivaluedIndex) {
232                     ((MultivaluedIndex) secondIndex).remove(key, indexKey);
233                 } else {
234                     secondIndex.remove(key);
235                 }
236                 
237                 if (isIndexed) {
238                     keyObject.addToIndex (metaMofId);
239                 }
240                 if (isIndexedOther) {
241                     ((StorableObject) lastRead).addToIndex (metaMofIdOther);
242                 }
243             } catch (StorageException e) {
244                 throw new DebugException();
245             }
246         }
247     }
248 }
249
Popular Tags