All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jlinq.LazyQueryable Maven / Gradle / Ivy

Go to download

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 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 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 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 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 consumer) {
		Iterator iter = iterator();
		while (iter.hasNext()) {
			consumer.accept(iter.next());
		}
	}

	@Override
	public Queryable filter(final Function 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 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 consumer) {
		T element = get(index);
		consumer.accept(element);
		return this;
	}

	@Override
	public > Queryable orderBy(final Function 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 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 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 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 unifier) {
		return combine(other).filterDuplicates(unifier);
	}

	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy