net.servicestack.func.Func Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of client Show documentation
Show all versions of client Show documentation
A client library to call your ServiceStack webservices.
The newest version!
// Copyright (c) 2013-present ServiceStack, Inc. All rights reserved.
// License: https://servicestack.net/bsd-license.txt
package net.servicestack.func;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Func {
public static ArrayList toList(int... xs) {
ArrayList to = new ArrayList<>();
for (int x : xs) {
to.add(x);
}
return to;
}
public static ArrayList toList(double... xs) {
ArrayList to = new ArrayList<>();
for (double x : xs) {
to.add(x);
}
return to;
}
@SafeVarargs
public static ArrayList toList(T... xs) {
ArrayList to = new ArrayList<>();
for (T x : xs) {
to.add(x);
}
return to;
}
public static ArrayList toList(Iterable xs) {
ArrayList to = new ArrayList<>();
if (xs == null) return to;
for (T x : xs) {
to.add(x);
}
return to;
}
public static T[] toArray(Iterable xs, Class cls) {
return toArray(toList(xs), cls);
}
@SuppressWarnings("unchecked")
public static T[] toArray(List list, Class cls) {
T[] array = (T[]) Array.newInstance(cls, list.size());
return list.toArray(array);
}
public static HashMap toDictionary(K k1, V v1) {
HashMap to = new HashMap<>();
to.put(k1, v1);
return to;
}
public static HashMap toDictionary(K k1, V v1, K k2, V v2) {
HashMap to = new HashMap<>();
to.put(k1, v1);
to.put(k2, v2);
return to;
}
public static HashMap toDictionary(K k1, V v1, K k2, V v2, K k3, V v3) {
HashMap to = toDictionary(k1, v1, k2, v2);
to.put(k3, v3);
return to;
}
public static HashMap toDictionary(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
HashMap to = toDictionary(k1, v1, k2, v2, k3, v3);
to.put(k4, v4);
return to;
}
public static HashMap toDictionary(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
HashMap to = toDictionary(k1, v1, k2, v2, k3, v3, k4, v4);
to.put(k5, v5);
return to;
}
public static HashMap toDictionary(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) {
HashMap to = toDictionary(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
to.put(k6, v6);
return to;
}
public static HashMap toDictionary(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) {
HashMap to = toDictionary(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6);
to.put(k7, v7);
return to;
}
public static HashMap toDictionary(Tuple... xs) {
HashMap to = new HashMap<>();
for (Tuple x : xs) {
to.put(x.A, x.B);
}
return to;
}
public static HashMap toDictionary(Iterable xs, Function f) {
HashMap to = new HashMap<>();
for (T x : xs) {
K key = f.apply(x);
to.put(key, x);
}
return to;
}
public static ArrayList ofType(Iterable xs, Class cls) {
ArrayList to = new ArrayList();
for (Object x : xs) {
if (cls.isInstance(x))
to.add((T) x);
}
return to;
}
public static ArrayList map(T[] xs, Function f) {
return map(toList(xs), f);
}
public static ArrayList map(Iterable xs, Function f) {
ArrayList to = new ArrayList<>();
if (xs == null) return to;
for (T x : xs) {
R ret = f.apply(x);
to.add(ret);
}
return to;
}
public static ArrayList mapi(T[] xs, FunctionIndex f) {
return mapi(toList(xs), f);
}
public static ArrayList mapi(Iterable xs, FunctionIndex f) {
ArrayList to = new ArrayList<>();
if (xs == null) return to;
int i = 0;
for (T x : xs) {
R ret = f.apply(x, i++);
to.add(ret);
}
return to;
}
public static void each(T[] xs, Action f) {
each(toList(xs), f);
}
public static void each(Iterable xs, Action f) {
if (xs == null) return;
for (T x : xs) {
f.apply(x);
}
}
public static void forEach(T[] xs, Action f) {
each(toList(xs), f);
}
public static void forEach(Iterable xs, Action f){
each(xs, f);
}
public static ArrayList filter(T[] xs, Predicate predicate) {
return filter(toList(xs), predicate);
}
public static ArrayList filter(Iterable xs, Predicate predicate) {
ArrayList to = new ArrayList<>();
if (xs == null) return to;
for (T x : xs) {
if (predicate.apply(x)) {
to.add(x);
}
}
return to;
}
public static ArrayList filteri(T[] xs, PredicateIndex predicate) {
return filteri(toList(xs), predicate);
}
public static ArrayList filteri(Iterable xs, PredicateIndex predicate) {
ArrayList to = new ArrayList<>();
if (xs == null) return to;
int i = 0;
for (T x : xs) {
if (predicate.apply(x, i++)) {
to.add(x);
}
}
return to;
}
public static T first(Iterable xs, Predicate predicate) {
if (xs == null) return null;
for (T x : xs) {
if (predicate.apply(x)) {
return x;
}
}
return null;
}
public static T first(T[] xs) {
return xs == null || xs.length == 0 ? null : xs[0];
}
public static T first(Iterable xs) {
return firstOrDefault(xs, (T) null);
}
public static T firstOrDefault(Iterable xs, T defaultValue) {
if (xs == null)
return defaultValue;
for (T x : xs) {
return x;
}
return defaultValue;
}
public static T last(T[] xs, Predicate predicate) {
return last(toList(xs), predicate);
}
public static T last(Iterable xs, Predicate predicate) {
if (xs == null) return null;
for (T x : reverse(xs)) {
if (predicate.apply(x)) {
return x;
}
}
return null;
}
public static T last(T[] xs) {
return xs == null ? null : xs[xs.length - 1];
}
public static T last(Iterable xs) {
if (xs == null) return null;
T last = null;
for (T x : xs) {
last = x;
}
return last;
}
public static boolean contains(T[] xs, Predicate predicate) {
return contains(toList(xs), predicate);
}
public static boolean contains(Iterable xs, Predicate predicate) {
return first(xs, predicate) != null;
}
public static ArrayList skip(T[] xs, int skip) {
return skip(toList(xs), skip);
}
public static ArrayList skip(Iterable xs, int skip) {
int i = 0;
ArrayList to = new ArrayList<>();
if (xs == null) return to;
for (T x : xs) {
if (i++ >= skip) {
to.add(x);
}
}
return to;
}
public static ArrayList skipWhile(T[] xs, Predicate predicate) {
return skipWhile(toList(xs), predicate);
}
public static ArrayList skipWhile(Iterable xs, Predicate predicate) {
ArrayList to = new ArrayList<>();
if (xs == null) return to;
boolean started = false;
for (T x : xs) {
if (!started && predicate.apply(x)) {
continue;
}
started = true;
to.add(x);
}
return to;
}
public static ArrayList skipWhilei(Iterable xs, PredicateIndex predicate) {
ArrayList to = new ArrayList<>();
if (xs == null) return to;
int i = 0;
boolean started = false;
for (T x : xs) {
if (!started && predicate.apply(x, i++)) {
continue;
}
started = true;
to.add(x);
}
return to;
}
public static ArrayList takeWhile(T[] xs, Predicate predicate) {
return takeWhile(toList(xs), predicate);
}
public static ArrayList takeWhile(Iterable xs, Predicate predicate) {
ArrayList to = new ArrayList<>();
if (xs == null) return to;
for (T x : xs) {
if (!predicate.apply(x)) {
return to;
}
to.add(x);
}
return to;
}
public static ArrayList takeWhilei(Iterable xs, PredicateIndex predicate) {
ArrayList to = new ArrayList<>();
if (xs == null) return to;
int i = 0;
for (T x : xs) {
if (!predicate.apply(x, i++)) {
return to;
}
to.add(x);
}
return to;
}
public static ArrayList take(T[] xs, int take) {
return take(toList(xs), take);
}
public static ArrayList take(Iterable xs, int take) {
int i = 0;
ArrayList to = new ArrayList<>();
if (xs == null) return to;
for (T x : xs) {
if (i++ >= take) {
return to;
}
to.add(x);
}
return to;
}
public static boolean any(T[] xs, Predicate predicate) {
return any(toList(xs), predicate);
}
public static boolean any(Iterable xs, Predicate predicate) {
if (xs == null) return false;
for (T x : xs) {
if (predicate.apply(x)) {
return true;
}
}
return false;
}
public static boolean all(T[] xs, Predicate predicate) {
return all(toList(xs), predicate);
}
public static boolean all(Iterable xs, Predicate predicate) {
if (xs == null) return false;
for (T x : xs) {
if (!predicate.apply(x)) {
return false;
}
}
return true;
}
public static ArrayList expand(Iterable... xss) {
ArrayList to = new ArrayList<>();
if (xss == null) return to;
for (Iterable xs : xss) {
for (T x : xs) {
to.add(x);
}
}
return to;
}
public static ArrayList expand(Iterable> xss) {
ArrayList to = new ArrayList<>();
if (xss == null) return to;
for (Iterable xs : xss) {
for (T x : xs) {
to.add(x);
}
}
return to;
}
public static T elementAt(T[] xs, int index) {
return elementAt(toList(xs), index);
}
public static T elementAt(Iterable xs, int index) {
if (xs == null) return null;
int i = 0;
for (T x : xs) {
if (i++ == index) {
return x;
}
}
return null;
}
public static ArrayList reverse(T[] xs) {
return reverse(toList(xs));
}
public static ArrayList reverse(Iterable xs) {
if (xs == null) return new ArrayList();
ArrayList clone = toList(xs);
Collections.reverse(clone);
return clone;
}
//Already cloned
private static ArrayList asReversed(ArrayList xs) {
if (xs == null) return new ArrayList();
Collections.reverse(xs);
return xs;
}
public static E reduce(T[] xs, E initialValue, Reducer reducer) {
return reduce(toList(xs), initialValue, reducer);
}
public static E reduce(Iterable xs, E initialValue, Reducer reducer) {
if (xs == null) return initialValue;
E currentValue = initialValue;
for (T x : xs) {
currentValue = reducer.reduce(currentValue, x);
}
return currentValue;
}
public static E reduceRight(T[] xs, E initialValue, Reducer reducer) {
return reduceRight(toList(xs), initialValue, reducer);
}
public static E reduceRight(Iterable xs, E initialValue, Reducer reducer) {
return reduce(reverse(xs), initialValue, reducer);
}
public static String join(T[] xs, String separator) {
return join(toList(xs), separator);
}
public static String join(Iterable xs, String separator) {
StringBuilder sb = new StringBuilder();
if (xs == null) return sb.toString();
for (T x : xs) {
if (sb.length() > 0)
sb.append(separator);
sb.append(x);
}
return sb.toString();
}
public static > ArrayList orderBy(T[] xs) {
return orderBy(toList(xs));
}
public static > ArrayList orderBy(Iterable xs) {
return orderBy(toList(xs));
}
private static > ArrayList orderBy(ArrayList cloned) {
Collections.sort(cloned);
return cloned;
}
public static > ArrayList orderBy(T[] xs, final Function f) {
return orderBy(toList(xs), f);
}
public static > ArrayList orderBy(Iterable xs, final Function f) {
return orderBy(toList(xs), f);
}
private static > ArrayList orderBy(ArrayList cloned, final Function f) {
Collections.sort(cloned, new Comparator() {
@Override
public int compare(T a, T b) {
R aVal = f.apply(a);
R bVal = f.apply(b);
if (aVal == null && bVal == null)
return 0;
if (aVal == null)
return -1;
if (bVal == null)
return 1;
return aVal.compareTo(bVal);
}
});
return cloned;
}
public static ArrayList orderBy(T[] xs, Comparator comparer) {
return orderBy(toList(xs), comparer);
}
public static ArrayList orderBy(Iterable xs, Comparator comparer) {
return orderBy(toList(xs), comparer);
}
private static ArrayList orderBy(ArrayList cloned, Comparator comparer) {
Collections.sort(cloned, comparer);
return cloned;
}
public static > ArrayList orderByDesc(T[] xs, final Function f) {
return asReversed(orderBy(toList(xs), f));
}
public static > ArrayList orderByDesc(Iterable xs, final Function f) {
return asReversed(orderBy(toList(xs), f));
}
public static > List orderByDesc(T[] xs) {
return asReversed(orderBy(toList(xs)));
}
public static > List orderByDesc(Iterable xs) {
return asReversed(orderBy(toList(xs)));
}
public static List orderByDesc(T[] xs, Comparator comparer) {
return asReversed(orderBy(toList(xs), comparer));
}
public static List orderByDesc(Iterable xs, Comparator comparer) {
return asReversed(orderBy(toList(xs), comparer));
}
@SafeVarargs
public static