1 /*
2 * Copyright (c) 2003, Henri Yandell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the
7 * following conditions are met:
8 *
9 * + Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * + Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * + Neither the name of Genjava-Core nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 package com.generationjava.collections;
33
34 import java.util.Iterator;
35 import java.util.LinkedList;
36
37 /***
38 * A piping Iterator which caches all objects that pass through it.
39 * When reset is called, it returns to the start of its cache.
40 * This however only allows for one version of the iterator at a time
41 * to be used. It gets used again and again.
42 * The alternative is to use the static cache method, or the iterate
43 * method. Both of which may be used to get a new Iterator over the
44 * same data.
45 *
46 * @date 2001-12-01
47 */
48 public class CachingIterator implements ResetableIterator {
49
50 static Iterator cache(Iterator iterator) {
51 if(iterator instanceof CachingIterator) {
52 return ((CachingIterator)iterator).iterate();
53 } else {
54 return new CachingIterator(iterator);
55 }
56 }
57
58 private Iterator iterator;
59 private LinkedList cacheList;
60 private Iterator cacheIterator;
61 private boolean caching;
62
63 public CachingIterator(Iterator iterator) {
64 this.iterator = iterator;
65 this.cacheList = new LinkedList();
66 }
67
68 public Object next() {
69 if(this.caching) {
70 return this.cacheIterator.next();
71 } else {
72 Object tmp = iterator.next();
73 this.cacheList.add(tmp);
74 return tmp;
75 }
76 }
77
78 public boolean hasNext() {
79 if(this.caching) {
80 boolean tmp = this.cacheIterator.hasNext();
81 return tmp;
82 } else {
83 boolean tmp = iterator.hasNext();
84 if(!tmp) {
85 this.caching = true;
86 reset();
87 }
88 return tmp;
89 }
90 }
91
92 public void remove() {
93 if(this.caching) {
94 throw new UnsupportedOperationException("Not possible to remove "+
95 "from the Iterator once it has begun to cache. ");
96 } else {
97 this.iterator.remove();
98 }
99 }
100
101 /***
102 * Force a loading of the wrapped iterator into cache.
103 */
104 public void loadCache() {
105 if(!this.caching) {
106 while(this.iterator.hasNext()) {
107 this.cacheList.add(this.iterator.next());
108 }
109 }
110 }
111
112 public void reset() {
113 this.cacheIterator = iterate();
114 }
115
116 public Iterator iterate() {
117 loadCache();
118 return this.cacheList.iterator();
119 }
120 }
This page was automatically generated by Maven