KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.mdr.persistence.*;
24 import org.netbeans.mdr.util.DebugException;
25 import org.netbeans.mdr.util.Logger;
26
27 /**
28  * Instances of this class are immutable, live collections representing
29  * results of index access.
30  *
31  * @author Martin Matula
32  */

33 public class IndexImmutSet implements Collection {
34
35     /* --------------------------------------------------------------------- */
36     /* -- Attributes (protected) ------------------------------------------- */
37     /* --------------------------------------------------------------------- */
38     
39     protected final MdrStorage storage;
40     protected final MultivaluedIndex index;
41     protected final Object JavaDoc indexKey;
42     
43     /* --------------------------------------------------------------------- */
44     /* -- Constructor (protected) ------------------------------------------ */
45     /* --------------------------------------------------------------------- */
46     
47     /**
48      * @param storage
49      * @param index the index where to look up the members of the current collection
50      * @param indexKey the key under which to look up the members of the current collection
51      */

52     protected IndexImmutSet(MdrStorage storage, MultivaluedIndex index, Object JavaDoc indexKey) {
53         this.storage = storage;
54         this.index = index;
55         this.indexKey = indexKey;
56     }
57
58     /* --------------------------------------------------------------------- */
59     /* -- Methods for index access ----------------------------------------- */
60     /* --------------------------------------------------------------------- */
61     
62     protected Collection getItems() {
63         try {
64             return index.getItems(indexKey);
65         } catch (StorageException e) {
66             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
67         }
68     }
69     
70     protected Collection getObjects() {
71         try {
72             return storage.getObjectsFromIndex(index, indexKey);
73         } catch (StorageException e) {
74             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
75         }
76     }
77     
78     /* --------------------------------------------------------------------- */
79     /* -- implements java.util.Collection ---------------------------------- */
80     /* --------------------------------------------------------------------- */
81     
82     public int size() {
83         return getItems().size();
84     }
85     
86     public boolean contains(Object JavaDoc obj) {
87         return getItems().contains(obj);
88     }
89     
90     public Iterator iterator() {
91         return new IndexImmutIterator(getObjects().iterator());
92     }
93     
94     public boolean isEmpty() {
95         return getItems().isEmpty();
96     }
97     
98     public boolean containsAll(Collection collection) {
99         return getItems().containsAll(collection);
100     }
101     
102     public Object JavaDoc[] toArray(Object JavaDoc[] obj) {
103         return getObjects().toArray(obj);
104     }
105     
106     public Object JavaDoc[] toArray() {
107         return getObjects().toArray();
108     }
109     
110    /**
111     * operation not supported
112     */

113     public boolean remove(Object JavaDoc o) {
114         throw new UnsupportedOperationException JavaDoc();
115     }
116     
117    /**
118     * operation not supported
119     */

120     public boolean add(Object JavaDoc obj) {
121         throw new UnsupportedOperationException JavaDoc();
122     }
123     
124    /**
125     * operation not supported
126     */

127     public boolean removeAll(Collection collection) {
128         throw new UnsupportedOperationException JavaDoc();
129     }
130     
131    /**
132     * operation not supported
133     */

134     public boolean addAll(Collection collection) {
135         throw new UnsupportedOperationException JavaDoc();
136     }
137     
138    /**
139     * operation not supported
140     */

141     public boolean retainAll(Collection collection) {
142         throw new UnsupportedOperationException JavaDoc();
143     }
144     
145    /**
146     * operation not supported
147     */

148     public void clear() {
149         throw new UnsupportedOperationException JavaDoc();
150     }
151     
152     /* --------------------------------------------------------------------- */
153     /* -- IndexImmutIterator (inner class) --------------------------------- */
154     /* --------------------------------------------------------------------- */
155     
156     /**
157      * Wrapper for the index access iterator diabling modifying accesses.
158      */

159     protected class IndexImmutIterator implements Iterator {
160         protected Iterator innerIterator;
161         
162         protected IndexImmutIterator(Iterator innerIterator) {
163             this.innerIterator = innerIterator;
164         }
165         
166         public boolean hasNext() {
167             return innerIterator.hasNext();
168         }
169         
170         public Object JavaDoc next() {
171             return innerIterator.next();
172         }
173         
174        /**
175         * operation not supported
176         */

177         public void remove() {
178             throw new UnsupportedOperationException JavaDoc();
179         }
180     }
181 }
182
Popular Tags