com.github.jlinq.LazyQueryable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jlinq Show documentation
Show all versions of jlinq Show documentation
A LINQ-like implementation that can be used to simplify the work with data classes.
The newest version!
package com.github.jlinq;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.SortedSet;
import java.util.TreeSet;
class LazyQueryable implements Queryable {
private final Iterable base;
public LazyQueryable(Iterable iterable) {
this.base = iterable;
}
@SafeVarargs
public LazyQueryable(T... elements) {
this(Arrays.asList(elements));
}
@Override
public Iterator iterator() {
return base.iterator();
}
public Queryable select(final Function super T, R> func) {
final Conversion conversion = new Conversion() {
@Override
public R perform(T value) {
return func.perform(value);
}
@Override
public boolean isIncluded(T input) {
return true;
}
};
return new LazyQueryable(
new LazyQueryableIterable(new Creator>() {
@Override
public Iterator create() {
return new LazyIterator(base.iterator(), conversion);
}
}));
}
@Override
public Queryable where(final Function super T, Boolean> func) {
final Conversion conversion = new Conversion() {
@Override
public T perform(T value) {
return value;
}
@Override
public boolean isIncluded(T input) {
return func.perform(input);
}
};
return new LazyQueryable(
new LazyQueryableIterable(new Creator>() {
@Override
public Iterator create() {
return new LazyIterator(base.iterator(), conversion);
}
}));
}
@Override
public QList toList() {
QList result = new QArrayList();
Iterator iter = iterator();
while (iter.hasNext()) {
result.add(iter.next());
}
return result;
}
@Override
public long count() {
long result = 0;
Iterator iter = iterator();
while (iter.hasNext()) {
iter.next();
result++;
}
return result;
}
@Override
public T first(T defaultValue) {
Iterator iter = iterator();
if (!iter.hasNext())
return defaultValue;
return iter.next();
}
@Override
public T first() {
return first(null);
}
@Override
public T last(T defaultValue) {
T result = defaultValue;
Iterator iter = iterator();
while (iter.hasNext())
result = iter.next();
return result;
}
@Override
public T last() {
return last(null);
}
@Override
public Queryable execute() {
return new LazyQueryable(toList());
}
@Override
public Map> group(final Function super T, R> func) {
TreeSet keys = new TreeSet();
Iterator iter = iterator();
while (iter.hasNext()) {
keys.add(func.perform(iter.next()));
}
Map> result = new HashMap>();
for (R key : keys) {
final R keyT = key;
result.put(key, where(new Function() {
@Override
public Boolean perform(T a) {
return func.perform(a).equals(keyT);
}
}));
}
return result;
}
@Override
public T find(Function super T, Boolean> func) {
return where(func).first();
}
@Override
public boolean contains(T value) {
Iterator iter = iterator();
while (iter.hasNext()) {
if (value == null && iter.next() == null)
return true;
if (value.equals(iter.next()))
return true;
}
return false;
}
@Override
public Queryable intersect(final Queryable other) {
if (other == this)
return this;
return where(new Function() {
@Override
public Boolean perform(T v) {
return other.contains(v);
}
});
}
@Override
public void forEach(ParameterizedCallback super T> consumer) {
Iterator iter = iterator();
while (iter.hasNext()) {
consumer.accept(iter.next());
}
}
@Override
public Queryable filter(final Function super T, Boolean> func) {
return where(new Function() {
@Override
public Boolean perform(T v) {
return !func.perform(v);
}
});
}
@Override
public Queryable combine(final Queryable other) {
return new LazyQueryable(new Iterable() {
@Override
public Iterator iterator() {
return new CombinedIterator(LazyQueryable.this.iterator(), other.iterator());
}
});
}
@Override
public Queryable filterDuplicates() {
return new LazyQueryable(new Iterable(){
@Override
public Iterator iterator() {
return new UnionIterator(LazyQueryable.this.iterator());
}
});
}
@Override
public Queryable filterDuplicates(final Function super T, ?> unifier) {
return new LazyQueryable(new Iterable(){
@Override
public Iterator iterator() {
return new UnionIterator(LazyQueryable.this.iterator(), unifier);
}
});
}
@Override
public T get(int index) {
int i = 0;
Iterator iterator = iterator();
T current = null;
while (iterator.hasNext()) {
current = iterator.next();
if (i == index)
return current;
i++;
}
throw new NoSuchElementException();
}
@Override
public Queryable get(int index, ParameterizedCallback super T> consumer) {
T element = get(index);
consumer.accept(element);
return this;
}
@Override
public > Queryable orderBy(final Function super T, R> func) {
final SortedSet result = new TreeSet(new Comparator() {
@Override
public int compare(T o1, T o2) {
return func.perform(o1).compareTo(func.perform(o2));
}
});
forEach(new ParameterizedCallback() {
@Override
public void accept(T e) {
result.add(e);
}
});
return new LazyQueryable(result);
}
@Override
public > Queryable orderInverse(final Function super T, R> func) {
final SortedSet result = new TreeSet(new Comparator() {
@Override
public int compare(T o1, T o2) {
return -1 * func.perform(o1).compareTo(func.perform(o2));
}
});
forEach(new ParameterizedCallback() {
@Override
public void accept(T e) {
result.add(e);
}
});
return new LazyQueryable(result);
}
@Override
public boolean all(Function super T, Boolean> condition) {
Iterator it = iterator();
T element;
while(it.hasNext()){
element = it.next();
if(!condition.perform(element)) return false;
}
return true;
}
@Override
public boolean any(Function super T, Boolean> condition) {
Iterator it = iterator();
T element;
while(it.hasNext()){
element = it.next();
if(condition.perform(element)) return true;
}
return false;
}
@Override
public Queryable notNull() {
return filter(new Function() {
@Override
public Boolean perform(T e) {
return e == null;
}
});
}
@Override
public Queryable union(Queryable other) {
return combine(other).filterDuplicates();
}
@Override
public Queryable union(Queryable other, Function super T, ?> unifier) {
return combine(other).filterDuplicates(unifier);
}
}