author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.xml.internal.xsom.util; |
|
27 |
||
28 |
import java.util.ArrayList; |
|
29 |
import java.util.Collection; |
|
30 |
import java.util.Iterator; |
|
31 |
import java.util.List; |
|
32 |
import java.util.NoSuchElementException; |
|
33 |
||
34 |
/** |
|
35 |
* {@link Collection} that returns the view of objects which are actually fetched |
|
36 |
* lazily from an {@link Iterator}. |
|
37 |
* |
|
38 |
* @author Kohsuke Kawaguchi |
|
39 |
*/ |
|
40 |
public class DeferedCollection<T> implements Collection<T> { |
|
41 |
/** |
|
42 |
* The iterator that lazily evaluates SCD query. |
|
43 |
*/ |
|
44 |
private final Iterator<T> result; |
|
45 |
||
46 |
/** |
|
47 |
* Stores values that are already fetched from {@link #result}. |
|
48 |
*/ |
|
49 |
private final List<T> archive = new ArrayList<T>(); |
|
50 |
||
51 |
public DeferedCollection(Iterator<T> result) { |
|
52 |
this.result = result; |
|
53 |
} |
|
54 |
||
55 |
public boolean isEmpty() { |
|
56 |
if(archive.isEmpty()) |
|
57 |
fetch(); |
|
58 |
return archive.isEmpty(); |
|
59 |
} |
|
60 |
||
61 |
public int size() { |
|
62 |
fetchAll(); |
|
63 |
return archive.size(); |
|
64 |
} |
|
65 |
||
66 |
public boolean contains(Object o) { |
|
67 |
if(archive.contains(o)) |
|
68 |
return true; |
|
69 |
while(result.hasNext()) { |
|
70 |
T value = result.next(); |
|
71 |
archive.add(value); |
|
72 |
if(value.equals(o)) |
|
73 |
return true; |
|
74 |
} |
|
75 |
return false; |
|
76 |
} |
|
77 |
||
78 |
public boolean containsAll(Collection<?> c) { |
|
79 |
for (Object o : c) { |
|
80 |
if(!contains(o)) |
|
81 |
return false; |
|
82 |
} |
|
83 |
return true; |
|
84 |
} |
|
85 |
||
86 |
public Iterator<T> iterator() { |
|
87 |
return new Iterator<T>() { |
|
88 |
int idx=0; |
|
89 |
public boolean hasNext() { |
|
90 |
if(idx<archive.size()) |
|
91 |
return true; |
|
92 |
return result.hasNext(); |
|
93 |
} |
|
94 |
||
95 |
public T next() { |
|
96 |
if(idx==archive.size()) |
|
97 |
fetch(); |
|
98 |
if(idx==archive.size()) |
|
99 |
throw new NoSuchElementException(); |
|
100 |
return archive.get(idx++); |
|
101 |
} |
|
102 |
||
103 |
public void remove() { |
|
104 |
// TODO |
|
105 |
} |
|
106 |
}; |
|
107 |
} |
|
108 |
||
109 |
public Object[] toArray() { |
|
110 |
fetchAll(); |
|
111 |
return archive.toArray(); |
|
112 |
} |
|
113 |
||
114 |
public <T>T[] toArray(T[] a) { |
|
115 |
fetchAll(); |
|
116 |
return archive.toArray(a); |
|
117 |
} |
|
118 |
||
119 |
||
120 |
||
121 |
private void fetchAll() { |
|
122 |
while(result.hasNext()) |
|
123 |
archive.add(result.next()); |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Fetches another item from {@link |
|
128 |
*/ |
|
129 |
private void fetch() { |
|
130 |
if(result.hasNext()) |
|
131 |
archive.add(result.next()); |
|
132 |
} |
|
133 |
||
134 |
// mutation methods are unsupported |
|
135 |
public boolean add(T o) { |
|
136 |
throw new UnsupportedOperationException(); |
|
137 |
} |
|
138 |
||
139 |
public boolean remove(Object o) { |
|
140 |
throw new UnsupportedOperationException(); |
|
141 |
} |
|
142 |
||
143 |
public boolean addAll(Collection<? extends T> c) { |
|
144 |
throw new UnsupportedOperationException(); |
|
145 |
} |
|
146 |
||
147 |
public boolean removeAll(Collection<?> c) { |
|
148 |
throw new UnsupportedOperationException(); |
|
149 |
} |
|
150 |
||
151 |
public boolean retainAll(Collection<?> c) { |
|
152 |
throw new UnsupportedOperationException(); |
|
153 |
} |
|
154 |
||
155 |
public void clear() { |
|
156 |
throw new UnsupportedOperationException(); |
|
157 |
} |
|
158 |
} |