Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
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 */
26 package com.sun.xml.internal.xsom.util;
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;
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;
46 /**
47 * Stores values that are already fetched from {@link #result}.
48 */
49 private final List<T> archive = new ArrayList<T>();
51 public DeferedCollection(Iterator<T> result) {
52 this.result = result;
53 }
55 public boolean isEmpty() {
56 if(archive.isEmpty())
57 fetch();
58 return archive.isEmpty();
59 }
61 public int size() {
62 fetchAll();
63 return archive.size();
64 }
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 }
78 public boolean containsAll(Collection<?> c) {
79 for (Object o : c) {
80 if(!contains(o))
81 return false;
82 }
83 return true;
84 }
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 }
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 }
103 public void remove() {
104 // TODO
105 }
106 };
107 }
109 public Object[] toArray() {
110 fetchAll();
111 return archive.toArray();
112 }
114 public <T>T[] toArray(T[] a) {
115 fetchAll();
116 return archive.toArray(a);
117 }
121 private void fetchAll() {
122 while(result.hasNext())
123 archive.add(result.next());
124 }
126 /**
127 * Fetches another item from {@link
128 */
129 private void fetch() {
130 if(result.hasNext())
131 archive.add(result.next());
132 }
134 // mutation methods are unsupported
135 public boolean add(T o) {
136 throw new UnsupportedOperationException();
137 }
139 public boolean remove(Object o) {
140 throw new UnsupportedOperationException();
141 }
143 public boolean addAll(Collection<? extends T> c) {
144 throw new UnsupportedOperationException();
145 }
147 public boolean removeAll(Collection<?> c) {
148 throw new UnsupportedOperationException();
149 }
151 public boolean retainAll(Collection<?> c) {
152 throw new UnsupportedOperationException();
153 }
155 public void clear() {
156 throw new UnsupportedOperationException();
157 }
158 }