KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import java.lang.reflect.Array JavaDoc;
23 import javax.jmi.reflect.RefBaseObject;
24
25 import org.netbeans.mdr.storagemodel.*;
26 import org.netbeans.mdr.util.DebugException;
27
28 /**
29  * Wraps collection of storage content with handlers.
30  *
31  * @author Martin Matula
32  */

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