javolution.util.internal.collection.DistinctCollectionImpl Maven / Gradle / Ivy
The newest version!
/*
* Javolution - Java(TM) Solution for Real-Time and Embedded Systems
* Copyright (C) 2012 - Javolution (http://javolution.org/)
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software is
* freely granted, provided that this notice is preserved.
*/
package javolution.util.internal.collection;
import java.util.Iterator;
import javolution.util.FastSet;
import javolution.util.function.Equality;
import javolution.util.service.CollectionService;
/**
* A view which does not iterate twice over the same elements.
*/
public class DistinctCollectionImpl extends CollectionView {
/** Peeking ahead iterator. */
private class IteratorImpl implements Iterator {
private boolean ahead;
private final FastSet iterated = new FastSet(comparator());
private E next;
private final Iterator targetIterator = target().iterator();
@Override
public boolean hasNext() {
if (ahead) return true;
while (targetIterator.hasNext()) {
next = targetIterator.next();
if (!iterated.contains(next)) {
ahead = true;
return true;
}
}
return false;
}
@Override
public E next() {
hasNext(); // Moves ahead.
ahead = false;
return next;
}
@Override
public void remove() {
targetIterator.remove();
}
}
private static final long serialVersionUID = 0x600L; // Version.
public DistinctCollectionImpl(CollectionService target) {
super(target);
}
@Override
public boolean add(E element) {
if (target().contains(element)) return false;
return target().add(element);
}
@Override
public void clear() {
target().clear();
}
@Override
public Equality super E> comparator() {
return target().comparator();
}
@Override
public boolean contains(Object o) {
return target().contains(o);
}
@Override
public boolean isEmpty() {
return target().isEmpty();
}
@Override
public Iterator iterator() {
return new IteratorImpl();
}
@Override
public boolean remove(Object o) { // Remove all instances.
boolean changed = false;
while (true) {
if (!remove(o)) return changed;
changed = true;
}
}
}