com.databricks.sdk.support.Dedupe Maven / Gradle / Ivy
package com.databricks.sdk.support;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Function;
/**
* De-duplicating iterator decorator
*
* Based on internal set of identifiers, decide either or not to propagate the item to the
* iteration loop.
*
* @param item of iteration
* @param identifier
*/
class Dedupe implements Iterator {
private final Iterator inner;
private final Function idGetter;
private IT current;
private final Set seen = new HashSet<>();
public Dedupe(Iterator inner, Function idGetter) {
this.inner = inner;
this.idGetter = idGetter;
}
@Override
public boolean hasNext() {
IT tmp;
while (inner.hasNext()) {
tmp = inner.next();
ID id = idGetter.apply(tmp);
if (seen.contains(id)) {
continue;
}
seen.add(id);
current = tmp;
return true;
}
return false;
}
@Override
public IT next() {
return current;
}
}