KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > handlers > AttrImmutCollWrapper


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.handlers;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import org.netbeans.mdr.persistence.StorageException;
24 import org.netbeans.mdr.storagemodel.MdrStorage;
25 import org.netbeans.mdr.storagemodel.StorableFeatured;
26 import org.netbeans.mdr.storagemodel.StorableObject;
27 import org.netbeans.mdr.util.DebugException;
28 import org.netbeans.mdr.util.Logger;
29 import org.netbeans.mdr.util.TransactionMutex;
30
31 /**
32  *
33  * @author Martin Matula
34  */

35 public class AttrImmutCollWrapper implements Collection JavaDoc {
36     protected final MdrStorage storage;
37     protected FeaturedHandler source;
38     protected final String JavaDoc attrName;
39     protected final int attrIndex;
40     protected final Collection JavaDoc inner;
41     
42     /** Creates new CollectionWrapper */
43     public AttrImmutCollWrapper(MdrStorage storage, FeaturedHandler source, int attrIndex, String JavaDoc attrName) {
44         this.storage = storage;
45         this.source = source;
46         this.attrName = attrName;
47         this.attrIndex = attrIndex;
48         this.inner = null;
49     }
50     
51     public AttrImmutCollWrapper(MdrStorage storage, Collection JavaDoc inner) {
52         this.storage = storage;
53         this.inner = inner;
54         this.source = null;
55         this.attrName = null;
56         this.attrIndex = 0;
57     }
58     
59     public Collection JavaDoc getInnerCollection() {
60         if (inner != null) {
61             return inner;
62         } else {
63             try {
64                 if (attrName != null)
65                     return (Collection JavaDoc) ((StorableFeatured) source._getDelegate()).getAttribute(attrName);
66                 else
67                     return (Collection JavaDoc) ((StorableObject) source._getDelegate()).getAttribute(attrIndex);
68             } catch (StorageException e) {
69                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
70             }
71         }
72     }
73     
74     protected final void lock(boolean write) {
75         storage.getRepository().beginTrans(write);
76     }
77     
78     protected final void unlock() {
79         storage.getRepository().endTrans();
80     }
81     
82     protected final void unlock(boolean fail) {
83         storage.getRepository().endTrans(fail);
84     }
85
86     public final boolean contains(Object JavaDoc obj) {
87         lock(false);
88         try {
89             return getInnerCollection().contains(obj);
90         } finally {
91             unlock();
92         }
93     }
94     
95     public Iterator JavaDoc iterator() {
96         lock(false);
97         try {
98             return new AttrImmutIteratorWrapper(getInnerCollection().iterator());
99         } finally {
100             unlock();
101         }
102     }
103     
104     public final int size() {
105         lock(false);
106         try {
107             return getInnerCollection().size();
108         } finally {
109             unlock();
110         }
111     }
112     
113     public final boolean isEmpty() {
114         lock(false);
115         try {
116             return getInnerCollection().isEmpty();
117         } finally {
118             unlock();
119         }
120     }
121     
122     public final boolean containsAll(Collection JavaDoc collection) {
123         lock(false);
124         try {
125             return getInnerCollection().containsAll(collection);
126         } finally {
127             unlock();
128         }
129     }
130     
131     public final Object JavaDoc[] toArray(Object JavaDoc[] obj) {
132         lock(false);
133         try {
134             return getInnerCollection().toArray(obj);
135         } finally {
136             unlock();
137         }
138     }
139     
140     public final Object JavaDoc[] toArray() {
141         lock(false);
142         try {
143             return getInnerCollection().toArray();
144         } finally {
145             unlock();
146         }
147     }
148     
149     public boolean equals(Object JavaDoc object) {
150         if (object == this) return true;
151         if (!(object instanceof Collection JavaDoc)) return false;
152         lock(false);
153         try {
154             Iterator JavaDoc it1 = iterator();
155             Iterator JavaDoc it2 = ((Collection JavaDoc) object).iterator();
156             while(it1.hasNext() && it2.hasNext()) {
157                 Object JavaDoc o1 = it1.next();
158                 Object JavaDoc o2 = it2.next();
159                 if (!(o1==null ? o2==null : o1.equals(o2)))
160                     return false;
161             }
162             return !(it1.hasNext() || it2.hasNext());
163         } finally {
164             unlock();
165         }
166     }
167     
168     public int hashCode() {
169         lock(false);
170         try {
171             int hashCode = 1;
172             for (Iterator JavaDoc it = iterator(); it.hasNext();) {
173                 Object JavaDoc obj = it.next();
174                 hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
175             }
176             return hashCode;
177         } finally {
178             unlock();
179         }
180     }
181
182     public boolean addAll(Collection JavaDoc collection) {
183         throw new UnsupportedOperationException JavaDoc();
184     }
185     
186     public boolean remove(Object JavaDoc obj) {
187         throw new UnsupportedOperationException JavaDoc();
188     }
189     
190     public boolean add(Object JavaDoc obj) {
191         throw new UnsupportedOperationException JavaDoc();
192     }
193     
194     public boolean retainAll(Collection JavaDoc collection) {
195         throw new UnsupportedOperationException JavaDoc();
196     }
197     
198     public boolean removeAll(Collection JavaDoc collection) {
199         throw new UnsupportedOperationException JavaDoc();
200     }
201     
202     public void clear() {
203         throw new UnsupportedOperationException JavaDoc();
204     }
205     
206     protected class AttrImmutIteratorWrapper implements Iterator JavaDoc {
207         protected final Iterator JavaDoc innerIterator;
208         
209         protected AttrImmutIteratorWrapper(Iterator JavaDoc innerIterator) {
210             this.innerIterator = innerIterator;
211         }
212         
213         public final boolean hasNext() {
214             lock(false);
215             try {
216                 return innerIterator.hasNext();
217             } finally {
218                 unlock();
219             }
220         }
221         
222         public Object JavaDoc next() {
223             lock(false);
224             try {
225                 return innerIterator.next();
226             } finally {
227                 unlock();
228             }
229         }
230         
231         public void remove() {
232             throw new UnsupportedOperationException JavaDoc();
233         }
234     }
235 }
236
Popular Tags