1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.iterators;
  17. import java.util.ListIterator;
  18. import java.util.NoSuchElementException;
  19. import org.apache.commons.collections.Predicate;
  20. /**
  21. * A proxy {@link ListIterator ListIterator} which
  22. * takes a {@link Predicate Predicate} instance to filter
  23. * out objects from an underlying <code>ListIterator</code>
  24. * instance. Only objects for which the specified
  25. * <code>Predicate</code> evaluates to <code>true</code> are
  26. * returned by the iterator.
  27. *
  28. * @since Commons Collections 2.0
  29. * @version $Revision: 1.7 $ $Date: 2004/02/18 00:59:50 $
  30. *
  31. * @author Rodney Waldhoff
  32. */
  33. public class FilterListIterator implements ListIterator {
  34. /** The iterator being used */
  35. private ListIterator iterator;
  36. /** The predicate being used */
  37. private Predicate predicate;
  38. /**
  39. * The value of the next (matching) object, when
  40. * {@link #nextObjectSet} is true.
  41. */
  42. private Object nextObject;
  43. /**
  44. * Whether or not the {@link #nextObject} has been set
  45. * (possibly to <code>null</code>).
  46. */
  47. private boolean nextObjectSet = false;
  48. /**
  49. * The value of the previous (matching) object, when
  50. * {@link #previousObjectSet} is true.
  51. */
  52. private Object previousObject;
  53. /**
  54. * Whether or not the {@link #previousObject} has been set
  55. * (possibly to <code>null</code>).
  56. */
  57. private boolean previousObjectSet = false;
  58. /**
  59. * The index of the element that would be returned by {@link #next}.
  60. */
  61. private int nextIndex = 0;
  62. //-----------------------------------------------------------------------
  63. /**
  64. * Constructs a new <code>FilterListIterator</code> that will not
  65. * function until
  66. * {@link ProxyListIterator#setListIterator(ListIterator) setListIterator}
  67. * and {@link #setPredicate(Predicate) setPredicate} are invoked.
  68. */
  69. public FilterListIterator() {
  70. super();
  71. }
  72. /**
  73. * Constructs a new <code>FilterListIterator</code> that will not
  74. * function until {@link #setPredicate(Predicate) setPredicate} is invoked.
  75. *
  76. * @param iterator the iterator to use
  77. */
  78. public FilterListIterator(ListIterator iterator ) {
  79. super();
  80. this.iterator = iterator;
  81. }
  82. /**
  83. * Constructs a new <code>FilterListIterator</code>.
  84. *
  85. * @param iterator the iterator to use
  86. * @param predicate the predicate to use
  87. */
  88. public FilterListIterator(ListIterator iterator, Predicate predicate) {
  89. super();
  90. this.iterator = iterator;
  91. this.predicate = predicate;
  92. }
  93. /**
  94. * Constructs a new <code>FilterListIterator</code> that will not
  95. * function until
  96. * {@link ProxyListIterator#setListIterator(ListIterator) setListIterator}
  97. * is invoked.
  98. *
  99. * @param predicate the predicate to use.
  100. */
  101. public FilterListIterator(Predicate predicate) {
  102. super();
  103. this.predicate = predicate;
  104. }
  105. //-----------------------------------------------------------------------
  106. /** Not supported. */
  107. public void add(Object o) {
  108. throw new UnsupportedOperationException("FilterListIterator.add(Object) is not supported.");
  109. }
  110. public boolean hasNext() {
  111. if(nextObjectSet) {
  112. return true;
  113. } else {
  114. return setNextObject();
  115. }
  116. }
  117. public boolean hasPrevious() {
  118. if(previousObjectSet) {
  119. return true;
  120. } else {
  121. return setPreviousObject();
  122. }
  123. }
  124. public Object next() {
  125. if(!nextObjectSet) {
  126. if(!setNextObject()) {
  127. throw new NoSuchElementException();
  128. }
  129. }
  130. nextIndex++;
  131. Object temp = nextObject;
  132. clearNextObject();
  133. return temp;
  134. }
  135. public int nextIndex() {
  136. return nextIndex;
  137. }
  138. public Object previous() {
  139. if(!previousObjectSet) {
  140. if(!setPreviousObject()) {
  141. throw new NoSuchElementException();
  142. }
  143. }
  144. nextIndex--;
  145. Object temp = previousObject;
  146. clearPreviousObject();
  147. return temp;
  148. }
  149. public int previousIndex() {
  150. return (nextIndex-1);
  151. }
  152. /** Not supported. */
  153. public void remove() {
  154. throw new UnsupportedOperationException("FilterListIterator.remove() is not supported.");
  155. }
  156. /** Not supported. */
  157. public void set(Object o) {
  158. throw new UnsupportedOperationException("FilterListIterator.set(Object) is not supported.");
  159. }
  160. //-----------------------------------------------------------------------
  161. /**
  162. * Gets the iterator this iterator is using.
  163. *
  164. * @return the iterator.
  165. */
  166. public ListIterator getListIterator() {
  167. return iterator;
  168. }
  169. /**
  170. * Sets the iterator for this iterator to use.
  171. * If iteration has started, this effectively resets the iterator.
  172. *
  173. * @param iterator the iterator to use
  174. */
  175. public void setListIterator(ListIterator iterator) {
  176. this.iterator = iterator;
  177. }
  178. //-----------------------------------------------------------------------
  179. /**
  180. * Gets the predicate this iterator is using.
  181. *
  182. * @return the predicate.
  183. */
  184. public Predicate getPredicate() {
  185. return predicate;
  186. }
  187. /**
  188. * Sets the predicate this the iterator to use.
  189. *
  190. * @param predicate the transformer to use
  191. */
  192. public void setPredicate(Predicate predicate) {
  193. this.predicate = predicate;
  194. }
  195. //-----------------------------------------------------------------------
  196. private void clearNextObject() {
  197. nextObject = null;
  198. nextObjectSet = false;
  199. }
  200. private boolean setNextObject() {
  201. // if previousObjectSet,
  202. // then we've walked back one step in the
  203. // underlying list (due to a hasPrevious() call)
  204. // so skip ahead one matching object
  205. if(previousObjectSet) {
  206. clearPreviousObject();
  207. if(!setNextObject()) {
  208. return false;
  209. } else {
  210. clearNextObject();
  211. }
  212. }
  213. while(iterator.hasNext()) {
  214. Object object = iterator.next();
  215. if(predicate.evaluate(object)) {
  216. nextObject = object;
  217. nextObjectSet = true;
  218. return true;
  219. }
  220. }
  221. return false;
  222. }
  223. private void clearPreviousObject() {
  224. previousObject = null;
  225. previousObjectSet = false;
  226. }
  227. private boolean setPreviousObject() {
  228. // if nextObjectSet,
  229. // then we've walked back one step in the
  230. // underlying list (due to a hasNext() call)
  231. // so skip ahead one matching object
  232. if(nextObjectSet) {
  233. clearNextObject();
  234. if(!setPreviousObject()) {
  235. return false;
  236. } else {
  237. clearPreviousObject();
  238. }
  239. }
  240. while(iterator.hasPrevious()) {
  241. Object object = iterator.previous();
  242. if(predicate.evaluate(object)) {
  243. previousObject = object;
  244. previousObjectSet = true;
  245. return true;
  246. }
  247. }
  248. return false;
  249. }
  250. }