KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.Array JavaDoc;
23
24 import org.netbeans.mdr.util.DebugException;
25 import org.netbeans.mdr.persistence.StorageException;
26 import org.netbeans.mdr.util.Logger;
27
28 /**
29  * Read-only collection. Wraps an inner collection all entries of which are
30  * MOF IDs. The accessor methods of this collection return the objects from
31  * storage instead of their IDs.
32  *
33  * @author Petr Hrebejk, Martin Matula
34  */

35 class CachedCollection implements Collection {
36     private final Collection innerCollection;
37     private final MdrStorage storage;
38     
39     /** Creates a new CachedCollection.
40      *
41      * @param storage the storage to resolve MOF IDs
42      * @param innerCollection the collection of MOF IDs
43      */

44     public CachedCollection(MdrStorage storage, Collection innerCollection) {
45         this.innerCollection = innerCollection;
46         this.storage = storage;
47     }
48
49     public boolean retainAll(Collection collection) {
50         throw new UnsupportedOperationException JavaDoc();
51     }
52
53     public boolean contains(Object JavaDoc obj) {
54         return innerCollection.contains(obj);
55     }
56
57     public Object JavaDoc[] toArray(Object JavaDoc[] obj) {
58         Object JavaDoc[] value = toArray();
59         Object JavaDoc[] result = obj;
60         if (value.length > result.length) {
61             if (value.getClass() == result.getClass()) {
62                 return value;
63             } else {
64                 result = (Object JavaDoc[]) Array.newInstance(obj.getClass(), value.length);
65             }
66         }
67         for (int i = 0; i < result.length; i++) {
68             result[i] = (i < value.length) ? value[i] : null;
69         }
70         return result;
71     }
72
73     public Iterator iterator() {
74         return new CachedIterator(innerCollection.iterator());
75     }
76
77     public boolean removeAll(Collection collection) {
78         throw new UnsupportedOperationException JavaDoc();
79     }
80
81     public Object JavaDoc[] toArray() {
82         Object JavaDoc[] array = innerCollection.toArray();
83
84         try {
85             for (int i = 0; i < array.length; i++) {
86                 array[i] = storage.getObject((org.netbeans.mdr.persistence.MOFID) array[i]);
87             }
88         } catch (Exception JavaDoc e) {
89             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
90         }
91
92         return array;
93     }
94
95     public boolean remove(Object JavaDoc obj) {
96         throw new UnsupportedOperationException JavaDoc();
97     }
98
99     public void clear() {
100         throw new UnsupportedOperationException JavaDoc();
101     }
102
103     public boolean addAll(Collection collection) {
104         throw new UnsupportedOperationException JavaDoc();
105     }
106
107     public int size() {
108         return innerCollection.size();
109     }
110
111     public boolean containsAll(Collection collection) {
112         return innerCollection.containsAll(collection);
113     }
114
115     public boolean add(Object JavaDoc obj) {
116         throw new UnsupportedOperationException JavaDoc();
117     }
118
119     public boolean isEmpty() {
120         return innerCollection.isEmpty();
121     }
122     
123     private class CachedIterator implements Iterator {
124         private final Iterator innerIterator;
125         
126         private CachedIterator(Iterator innerIterator) {
127             this.innerIterator = innerIterator;
128         }
129         
130         public boolean hasNext() {
131             return innerIterator.hasNext();
132         }
133
134         public Object JavaDoc next() {
135             try {
136                 return storage.getObject((org.netbeans.mdr.persistence.MOFID) innerIterator.next());
137             } catch (StorageException e) {
138                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
139             }
140         }
141
142         public void remove() {
143             throw new UnsupportedOperationException JavaDoc();
144         }
145     }
146 }
147
Popular Tags