org.lambda.query.Query Maven / Gradle / Ivy
package org.lambda.query;
import com.spun.util.ArrayUtils;
import org.lambda.functions.Function1;
import org.lambda.query.OrderBy.Order;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.Map.Entry;
public class Query
{
public static Queryable select(Iterable list, Function1 function)
{
Queryable out = new Queryable();
for (In i : list)
{
out.add(function.call(i));
}
return out;
}
public static Queryable select(In[] list, Function1 function)
{
return select(ArrayUtils.asList(list), function);
}
public static Queryable where(Iterable list, Function1 funct)
{
Queryable out = new Queryable();
for (In i : list)
{
if (funct.call(i))
{
out.add(i);
}
}
return out;
}
public static In first(Iterable iterable)
{
return first(iterable, __ -> true);
}
public static In first(In[] list)
{
return first(list, __ -> true);
}
public static In first(In[] list, Function1 filter)
{
return first(ArrayUtils.asList(list), filter);
}
public static In first(Iterable list, Function1 filter)
{
for (In i : list)
{
if (filter.call(i))
{ return i; }
}
return null;
}
public static Queryable where(In[] list, Function1 filter)
{
Queryable out = new Queryable();
for (In i : list)
{
if (filter.call(i))
{
out.add(i);
}
}
return out;
}
public static > In max(Iterable list, Function1 f1)
{
return getTop(list, f1, 1);
}
public static > In max(In[] list, Function1 f1)
{
return getTop(list, f1, 1);
}
public static > In min(In[] array, Function1 f1)
{
return min(Arrays.asList(array), f1);
}
public static > In min(Iterable list, Function1 f1)
{
return getTop(list, f1, -1);
}
public static Double average(In[] array, Function1 f1)
{
return average(Arrays.asList(array), f1);
}
public static Double average(Iterable list, Function1 f1)
{
double total = 0.00;
for (In in : list)
{
total += f1.call(in).doubleValue();
}
return total / ArrayUtils.size(list);
}
private static > In getTop(In[] list, Function1 f1, int modifier)
{
return getTop(Arrays.asList(list), f1, modifier);
}
private static > In getTop(Iterable list, Function1 f1,
int modifier)
{
if (ArrayUtils.isEmpty(list))
{ return null; }
In found = list.iterator().next();
Out max = f1.call(found);
for (In in : list)
{
Out current = f1.call(in);
if (max.compareTo(current) * modifier < 0)
{
max = current;
found = in;
}
}
return found;
}
public static > T[] orderBy(T[] list, Function1 f1)
{
return orderBy(list, Order.Ascending, f1);
}
public static > T[] orderBy(T[] list, Order order, Function1 f1)
{
Arrays.sort(list, new OrderBy(order, f1));
return list;
}
public static Queryable orderBy(List list, Function1> f1)
{
return orderBy(list, Order.Ascending, f1);
}
public static Queryable orderBy(List list, Order order, Function1> f1)
{
Collections.sort(list, new OrderBy>(order, f1));
return Queryable.as(list);
}
/**
* Why does sum() return double? see {@link #sum(Number[])}
*/
public static Double sum(In[] list, Function1 f1)
{
return sum(ArrayUtils.asList(list), f1);
}
/**
* Why does sum() return double? see {@link #sum(Number[])}
*/
public static Double sum(Iterable list, Function1 f1)
{
double sum = 0;
for (In in : list)
{
sum += f1.call(in).doubleValue();
}
return sum;
}
/**
* Why does sum() return double? see {@link #sum(Number[])}
*/
public static Double sum(Iterable list)
{
return sum(list, a -> a);
}
/**
* Why does sum() return double?
* sum() needs to handle possible overflow.
* For example:
* Queryable.as(100, 100, 100).sum(n -> n.byteValue())
should equal 300.
* Because Double handles the largest range of any number, that is what is returned by default. You can convert
* it to whichever type you want, but be aware that overflowed values may be truncated.
*/
public static Double sum(Out[] list)
{
return sum(list, a -> a);
}
@SuppressWarnings({"rawtypes", "unchecked"})
public static In max(Iterable numbers)
{
return (In) max(numbers, (a) -> (Comparable) a);
}
public static In max(In[] numbers)
{
return max(ArrayUtils.asList(numbers));
}
@SuppressWarnings({"rawtypes", "unchecked"})
public static In min(In[] numbers)
{
return min(Arrays.asList(numbers));
}
@SuppressWarnings({"rawtypes", "unchecked"})
public static In min(Iterable numbers)
{
return (In) min((Iterable) numbers, (Comparable a) -> a);
}
public static boolean all(In[] array, Function1 funct)
{
return array.length == where(array, funct).size();
}
public static boolean all(Iterable array, Function1 funct)
{
return ArrayUtils.size(array) == where(array, funct).size();
}
public static boolean any(Iterable array, Function1 funct)
{
return first(array, funct) != null;
}
public static boolean any(In[] array, Function1 funct)
{
return first(array, funct) != null;
}
public static Queryable distinct(In[] array)
{
return distinct(Arrays.asList(array));
}
public static Queryable distinct(Iterable list)
{
Queryable distinct = new Queryable<>();
for (In in : list)
{
if (!distinct.contains(in))
{
distinct.add(in);
}
}
return distinct;
}
public static In last(In[] list)
{
return last(Arrays.asList(list));
}
static In last(Iterable asList)
{
return ArrayUtils.getLast(asList);
}
public static Queryable selectMany(In[] array, Function1> selector)
{
return selectMany(Arrays.asList(array), selector);
}
public static Queryable selectMany(Iterable list, Function1> selector)
{
Queryable out = new Queryable();
for (In i : list)
{
out.addAll(selector.call(i));
}
return out;
}
public static Queryable selectManyArray(In[] array, Function1 selector)
{
return selectManyArray(Arrays.asList(array), selector);
}
public static Queryable selectManyArray(Iterable list, Function1 selector)
{
Queryable out = new Queryable();
for (In i : list)
{
out.addAll(Arrays.asList(selector.call(i)));
}
return out;
}
public static Queryable>> groupBy(In[] array,
Function1 keySelector)
{
return groupBy(Arrays.asList(array), keySelector);
}
public static Queryable>> groupBy(Iterable list,
Function1 keySelector)
{
return groupBy(list, keySelector, v -> v, r -> r);
}
public static Queryable> groupBy(In[] array,
Function1 keySelector, Function1 valueSelector,
Function1, Out2> resultSelector)
{
return groupBy(Arrays.asList(array), keySelector, valueSelector, resultSelector);
}
public static Queryable> groupBy(Iterable list,
Function1 keySelector, Function1 valueSelector,
Function1, Out2> resultSelector)
{
Queryable>> tuples = new Queryable<>();
Queryable> objectsWithKey = Query.select(list,
i -> new SimpleEntry<>(keySelector.call(i), valueSelector.call(i)));
for (Entry tuple : objectsWithKey)
{
Map.Entry> first = tuples.first(o -> o.getKey().equals(tuple.getKey()));
if (first == null)
{
first = new SimpleEntry<>(tuple.getKey(), Queryable.as(tuple.getValue()));
tuples.add(first);
}
else
{
first.getValue().add(tuple.getValue());
}
}
return tuples.select(t -> new SimpleEntry<>(t.getKey(), resultSelector.call(t.getValue())));
}
public static Queryable skip(Iterable list, int number)
{
Queryable result = new Queryable<>();
if (list == null)
{ return result; }
int counter = 0;
for (In in : list)
{
counter++;
if (number < counter)
{
result.add(in);
}
}
return result;
}
public static Queryable skip(In[] list, int number)
{
if (list == null || list.length <= number)
{ return Queryable.createEmpty(list); }
In[] ins = Arrays.copyOfRange(list, number, list.length);
return Queryable.as(ins);
}
public static Queryable take(Iterable list, int number)
{
Queryable result = new Queryable<>();
if (list == null)
{ return result; }
int counter = 0;
for (In in : list)
{
counter++;
if (counter <= number)
{
result.add(in);
}
else
{
break;
}
}
return result;
}
public static Queryable take(In[] list, int number)
{
if (list == null)
{ return new Queryable<>(); }
In[] ins = Arrays.copyOfRange(list, 0, Math.min(number, list.length));
return Queryable.as(ins);
}
public static Queryable selectRecursivelyUntil(In[] array, Function1 selector,
Function1 until)
{
return selectRecursivelyUntil(Queryable.as(array), selector, until);
}
public static Queryable selectRecursivelyUntil(List list, Function1 selector,
Function1 until)
{
Queryable results = new Queryable<>();
for (In item : list)
{
do
{
results.add(item);
item = selector.call(item);
}
while (!until.call(item));
}
return results;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy