package com.xnlogic.pacer.pipes;

import java.util.NoSuchElementException;

import com.tinkerpop.pipes.AbstractPipe;
import com.tinkerpop.pipes.filter.DuplicateFilterPipe;
import com.tinkerpop.pipes.util.iterators.ExpandableIterator;

public class IsUniquePipe<T> extends AbstractPipe<T, T> {
    private boolean unique;
    private ExpandableIterator<T> expando;
    private DuplicateFilterPipe<T> uniquePipe;
  
    public IsUniquePipe() {
        super();
        this.prepareState();
    }

    protected T processNextStart() {
        T value = this.starts.next();

        if (this.unique)
            this.checkUniqueness(value);
        
        return value;
    }

    public void reset() {
        super.reset();
        this.prepareState();
    }
    
    public boolean isUnique() {
        return this.unique;
    }

    public boolean getSideEffect() {
        return this.unique;
    }
    
    protected void checkUniqueness(T value) {
        try {
            this.expando.add(value);
            this.uniquePipe.next();
        } catch (NoSuchElementException nsee) {
            this.unique = false;
            this.uniquePipe = null;
            this.expando = null;
        }
    }

    protected void prepareState() {
        this.unique = true;
        this.expando = new ExpandableIterator<T>();
        this.uniquePipe = new DuplicateFilterPipe<T>();
        this.uniquePipe.setStarts(this.expando);
    }
}

@@ -72,165 +72,102 @@
 on;%0A
-%0A
 import 
-com.tinkerpop.pipes.AbstractPipe;%0Aimport com.tinkerpop.pipes.filter.DuplicateFilterPipe;%0Aimport com.tinkerpop.pipes.util.iterators.ExpandableIterator
+java.util.LinkedHashSet;%0Aimport java.util.Set;%0A%0Aimport com.tinkerpop.pipes.AbstractPipe
 ;%0A%0Ap
@@ -267,84 +267,22 @@
 ate 
-ExpandableIterator%3CT%3E expando;%0A    private DuplicateFilterPipe%3CT%3E uniquePipe
+Set historySet
 ;%0A  
@@ -438,17 +438,16 @@
 next();%0A
-%0A
         
@@ -508,17 +508,8 @@
 e);%0A
-        %0A
     
@@ -823,131 +823,39 @@
     
-try %7B%0A            this.expando.add(value);%0A            this.uniquePipe.next();%0A        %7D catch (NoSuchElementException nsee
+if (!this.historySet.add(value)
 ) %7B%0A
@@ -908,51 +908,18 @@
 his.
-uniquePipe = null;%0A            this.expando
+historySet
  = n
@@ -993,178 +993,68 @@
 his.
-unique = true;%0A        this.expando = new ExpandableIterator%3CT%3E();%0A        this.uniquePipe = new DuplicateFilterPipe%3CT%3E();%0A        this.uniquePipe.setStarts(this.expando)
+historySet = new LinkedHashSet();%0A        this.unique = true
 ;%0A  

Simplify IsUniquePipe