org.lambda.query.Queryable Maven / Gradle / Ivy
package org.lambda.query;
import com.spun.util.ClassUtils;
import com.spun.util.ObjectUtils;
import org.lambda.Extendable;
import org.lambda.functions.Function1;
import org.lambda.query.OrderBy.Order;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Queryable extends ArrayList
{
// TODO: autogenerate this;
private final long serialVersionUID = 1L;
private Class type;
public Queryable()
{
this(null);
}
public Queryable(Class type)
{
this.type = type;
}
public static Queryable createEmpty(In[] list)
{
if (list == null)
{
return new Queryable<>();
}
else
{
return new Queryable(list.getClass().getComponentType());
}
}
public >> T use(Class that)
{
try
{
T t = that.newInstance();
t.setCaller(this);
return t;
}
catch (Throwable t)
{
throw ObjectUtils.throwAsError(t);
}
}
public Queryable select(Function1 function)
{
return Query.select(this, function);
}
public Queryable where(Function1 funct)
{
return Query.where(this, funct);
}
public In first()
{
return firstOrDefault(null);
}
public In first(Function1 filter)
{
return Query.first(this, filter);
}
public In firstOrDefault(In defaultValue)
{
return this.isEmpty() ? defaultValue : this.get(0);
}
public boolean all(Function1 filter)
{
return Query.all(this, filter);
}
public > In max(Function1 f1)
{
return Query.max(this, f1);
}
public > In min(Function1 f1)
{
return Query.min(this, f1);
}
public Double average(Function1 f1)
{
return Query.average(this, f1);
}
public Queryable orderBy(Function1> f1)
{
return Query.orderBy(this, f1);
}
public Queryable orderBy(Order order, Function1> f1)
{
return Query.orderBy(this, order, f1);
}
/**
* Why does sum() return double? see {@link org.lambda.query.Query#sum(Number[])}
*/
public Double sum(Function1 f1)
{
return Query.sum(this, f1);
}
/**
* Why does sum() return double? see {@link org.lambda.query.Query#sum(Number[])}
*/
public Double sum()
{
return Query.sum((List) this);
}
public In max()
{
return (In) Query.max((List) this);
}
public In min()
{
return (In) Query.min((List) this);
}
public boolean any(Function1 funct)
{
return Query.any(this, funct);
}
public static Queryable of(List list)
{
return as(list);
}
public static Queryable of(List list, Class type)
{
return as(list, type);
}
public static Queryable of(T... array)
{
return as(array);
}
public static Queryable of(Stream stream)
{
return as(stream);
}
public static Queryable as(List list)
{
Class> type = ClassUtils.getGreatestCommonBaseType(list);
return Queryable.as(list, (Class) type);
}
public static Queryable as(List list, Class type)
{
if (list instanceof Queryable)
{ return (Queryable) list; }
Queryable q = new Queryable(type);
q.addAll(list);
return q;
}
public static Queryable as(T... array)
{
return as(Arrays.asList(array), (Class) array.getClass().getComponentType());
}
public static Queryable as(Stream stream)
{
return Queryable.as(stream.collect(Collectors.toList()));
}
/**
* Maintains order
*/
public Queryable distinct()
{
return Query.distinct(this);
}
public In[] asArray()
{
int size = this.size();
In[] result = (In[]) Array.newInstance(getType(), size);
return toArray(result);
}
public Class getType()
{
if (type == null)
{
type = (Class) ClassUtils.getGreatestCommonBaseType(this);
}
return type;
}
public Queryable selectMany(Function1> selector)
{
return Query.selectMany(this, selector);
}
public Queryable selectManyArray(Function1 selector)
{
return Query.selectManyArray(this, selector);
}
public Queryable>> groupBy(Function1 keySelector)
{
return Query.groupBy(this, keySelector);
}
public Queryable> groupBy(Function1 keySelector,
Function1 valueSelector, Function1, Out2> resultSelector)
{
return Query.groupBy(this, keySelector, valueSelector, resultSelector);
}
public String join(String joinCharacter)
{
return String.join(joinCharacter, this.select(t -> "" + t));
}
public String join(String joinCharacter, Function1 transformer)
{
return String.join(joinCharacter, this.select(t -> "" + transformer.call(t)));
}
public Queryable> split(Function1 function)
{
Queryable> results = new Queryable<>();
Queryable part = new Queryable<>();
for (In in : this)
{
if (function.call(in))
{
results.add(part);
part = new Queryable<>();
}
else
{
part.add(in);
}
}
return results;
}
public Queryable skip(int number)
{
return Query.skip(this, number);
}
public Queryable take(int number)
{
return Query.take(this, number);
}
public Queryable combine(Queryable iterables)
{
Queryable queryable = new Queryable<>(this.type);
queryable.addAll(this);
queryable.addAll(iterables);
return queryable;
}
public Queryable selectRecursivelyUntil(Function1 selector, Function1 until)
{
return Query.selectRecursivelyUntil(this, selector, until);
}
public In last()
{
return Query.last(this);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy