com.timgroup.karg.reference.Refs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of karg Show documentation
Show all versions of karg Show documentation
A library that helps you write Java functions using keyword arguments.
package com.timgroup.karg.reference;
import java.util.Iterator;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
public final class Refs {
public static Iterable toIterable(final Ref ref) {
return new Iterable() {
@Override public Iterator iterator() {
return new Iterator() {
private boolean consumed = ref.get() == null;
@Override public boolean hasNext() {
return !consumed;
}
@Override public T next() {
if (consumed) {
return null;
}
consumed = true;
return ref.get();
}
@Override public void remove() {
ref.set(null);
}
};
}
};
}
public static Function, Iterable> toIterable() {
return new Function, Iterable>() {
@Override public Iterable apply(Ref arg0) {
return toIterable(arg0);
}
};
}
public static Iterable toIterable(Iterable extends Ref> refs) {
Function, Iterable> toIterable = toIterable();
return Iterables.concat(Iterables.transform(refs, toIterable));
}
private Refs() { }
}