/** * Gets an iterator that filters another iterator. * <p> * The returned iterator will only return objects that match the specified * filtering predicate. * * @param iterator the iterator to use, not null * @param predicate the predicate to use as a filter, not null * @return a new filtered iterator * @throws NullPointerException if either parameter is null */ public static Iterator filteredIterator(Iterator iterator, Predicate predicate) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } return new FilterIterator(iterator, predicate); }
/** * Test that when the iterator is changed, the hasNext method returns the * correct response for the new iterator. */ public void testSetIterator() { Iterator iter1 = Collections.singleton(new Object()).iterator(); Iterator iter2 = Collections.EMPTY_LIST.iterator(); FilterIterator filterIterator = new FilterIterator(iter1); filterIterator.setPredicate(TruePredicate.getInstance()); // this iterator has elements assertEquals(true, filterIterator.hasNext()); // this iterator has no elements filterIterator.setIterator(iter2); assertEquals(false, filterIterator.hasNext()); }
private void verifyElementsInPredicate(final String[] elements) { Predicate pred = new Predicate() { public boolean evaluate(Object x) { for (int i = 0; i < elements.length; i++) if (elements[i].equals(x)) return true; return false; } }; initIterator(); iterator.setPredicate(pred); for (int i = 0; i < elements.length; i++) { String s = (String)iterator.next(); assertEquals(elements[i], s); assertTrue(i == elements.length - 1 ? !iterator.hasNext() : iterator.hasNext()); } verifyNoMoreElements(); // test removal initIterator(); iterator.setPredicate(pred); if (iterator.hasNext()) { Object last = iterator.next(); iterator.remove(); assertTrue("Base of FilterIterator still contains removed element.", !list.contains(last)); } }
/** * Test that when the predicate is changed, the hasNext method returns the * correct response for the new predicate. */ public void testSetPredicate() { Iterator iter = Collections.singleton(null).iterator(); FilterIterator filterIterator = new FilterIterator(iter); filterIterator.setPredicate(TruePredicate.getInstance()); // this predicate matches assertEquals(true, filterIterator.hasNext()); // this predicate doesn't match filterIterator.setPredicate(NotNullPredicate.getInstance()); assertEquals(false, filterIterator.hasNext()); }
/** * Returns true if the underlying iterator contains an object that * matches the predicate. * * @return true if there is another object that matches the predicate * @throws NullPointerException if either the iterator or predicate are null */ public boolean hasNext() { if (nextObjectSet) { return true; } else { return setNextObject(); } }
public void testRepeatedNext() { for (int i = 0; i < array.length; i++) iterator.next(); verifyNoMoreElements(); }
public void testRepeatedHasNext() { for (int i = 0; i <= array.length; i++) { assertTrue(iterator.hasNext()); } }
private void free() { if (_state != OPEN) return; List itrs = _itr.getIterators(); Iterator itr; for (int i = 0; i < itrs.size(); i++) { itr = (Iterator) itrs.get(i); if (itr instanceof FilterIterator) itr = ((FilterIterator) itr).getIterator(); ImplHelper.close(itr); } }
/** * Returns true if the underlying iterator contains an object that * matches the predicate. * * @return true if there is another object that matches the predicate * @throws NullPointerException if either the iterator or predicate are null */ public boolean hasNext() { if (nextObjectSet) { return true; } else { return setNextObject(); } }
public Object next() { if (_closed) throw new NoSuchElementException(); return super.next(); }
public boolean hasNext() { return (_closed) ? false : super.hasNext(); }
private void free() { if (_state != OPEN) return; List itrs = _itr.getIterators(); Iterator itr; for (int i = 0; i < itrs.size(); i++) { itr = (Iterator) itrs.get(i); if (itr instanceof FilterIterator) itr = ((FilterIterator) itr).getIterator(); ImplHelper.close(itr); } }
/** * Gets an iterator that filters another iterator. * <p> * The returned iterator will only return objects that match the specified * filtering predicate. * * @param iterator the iterator to use, not null * @param predicate the predicate to use as a filter, not null * @return a new filtered iterator * @throws NullPointerException if either parameter is null */ public static Iterator filteredIterator(Iterator iterator, Predicate predicate) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } return new FilterIterator(iterator, predicate); }
/** * Returns the next object that matches the predicate. * * @return the next object which matches the given predicate * @throws NullPointerException if either the iterator or predicate are null * @throws NoSuchElementException if there are no more elements that * match the predicate */ public Object next() { if (!nextObjectSet) { if (!setNextObject()) { throw new NoSuchElementException(); } } nextObjectSet = false; return nextObject; }
public Object next() { if (_closed) throw new NoSuchElementException(); return super.next(); }
public boolean hasNext() { return (_closed) ? false : super.hasNext(); }
private void free() { if (_state != OPEN) return; List itrs = _itr.getIterators(); Iterator itr; for (int i = 0; i < itrs.size(); i++) { itr = (Iterator) itrs.get(i); if (itr instanceof FilterIterator) itr = ((FilterIterator) itr).getIterator(); ImplHelper.close(itr); } }
/** * Returns a FilterIterator that does not filter * any of its elements * * @param i the Iterator to "filter" * @return "filtered" iterator */ protected FilterIterator makePassThroughFilter(Iterator i) { Predicate pred = new Predicate() { public boolean evaluate(Object x) { return true; } }; return new FilterIterator(i,pred); }
/** * Returns the next object that matches the predicate. * * @return the next object which matches the given predicate * @throws NullPointerException if either the iterator or predicate are null * @throws NoSuchElementException if there are no more elements that * match the predicate */ public Object next() { if (!nextObjectSet) { if (!setNextObject()) { throw new NoSuchElementException(); } } nextObjectSet = false; return nextObject; }