Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.spun.util.ArrayUtils Maven / Gradle / Ivy
package com.spun.util;
import org.lambda.functions.Function0;
import org.lambda.functions.Function1;
import org.lambda.query.Queryable;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.stream.StreamSupport;
/**
* A static class of convenience methods for arrays and collections.
*/
public class ArrayUtils
{
public static java.util.Collection addArray(java.util.Collection v, T[] array)
{
if ((array == null) || (v == null))
{ return v; }
for (int i = 0; i < array.length; i++)
{
v.add(array[i]);
}
return v;
}
public static String toString(T[] values, Function1 formatter)
{
return toString(asList(values), formatter);
}
public static List asList(T[] values)
{
return values == null ? Collections.emptyList() : Arrays.asList(values);
}
public static String toString(Iterable values, Function1 formatter)
{
StringBuffer b = new StringBuffer();
for (T t : values)
{
b.append(formatter.call(t) + "\n");
}
return b.toString();
}
/**
* @deprecated for removal in 16.0.0
*/
@Deprecated
public static Vector toReverseVector(Vector vector)
{
Vector reverse = new Vector<>(vector.size());
for (int i = vector.size() - 1; i >= 0; i--)
{
reverse.add(vector.elementAt(i));
}
return reverse;
}
public static T[] toReverseArray(T[] array)
{
for (int i = 0; i < array.length / 2; i++)
{
T o1 = array[i];
int end = array.length - i - 1;
T o2 = array[end];
array[i] = o2;
array[end] = o1;
}
return array;
}
public static T[] addToArray(T[] array, T... objects)
{
return combine(array, objects);
}
public static T[] getSubsection(T[] array, int startInclusive, int endExclusive)
{
int length = endExclusive - startInclusive;
length = length > array.length ? array.length : length;
T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), length);
System.arraycopy(array, startInclusive, newArray, 0, length);
return newArray;
}
public static boolean isEmpty(Object[] array)
{
return ((array == null) || (array.length == 0));
}
public static boolean isEmpty(Iterable> collection)
{
return ((collection == null) || (!collection.iterator().hasNext()));
}
public static T getSingleton(T[] parts)
{
if (parts == null)
{ return null; }
switch (parts.length)
{
case 0 :
return null;
case 1 :
return parts[0];
default :
throw new Error("Called with more than one object in the array " + Arrays.asList(parts));
}
}
public static T getFirst(T[] array)
{
return array == null || array.length == 0 ? null : array[0];
}
public static T getFirst(T[] array, Comparator compartor)
{
return get(array, compartor, true);
}
public static T getFirst(Collection array, Comparator sorter)
{
return get((T[]) array.toArray(), sorter, true);
}
public static T getLast(T[] array)
{
if (array == null || array.length < 1)
{ return null; }
return array[array.length - 1];
}
public static T getLast(T[] array, Comparator sorter)
{
return get(array, sorter, false);
}
public static T getLast(Collection array, Comparator sorter)
{
return get((T[]) array.toArray(), sorter, false);
}
private static T get(T[] array, Comparator sorter, boolean wantFirst)
{
if (isEmpty(array))
{ return null; }
T last = array[0];
for (int i = 1; i < array.length; i++)
{
int compare = sorter.compare(last, array[i]);
if ((wantFirst && compare > 0) || (!wantFirst && compare < 0))
{
last = array[i];
}
}
return last;
}
public static List> combineResults(Object[] array, String invokeMethod)
{
if (ArrayUtils.isEmpty(array))
{ return Collections.EMPTY_LIST; }
try
{
return combineResults(array, ObjectUtils.getGreatestCommonDenominator(array, invokeMethod));
}
catch (Exception e)
{
throw ObjectUtils.throwAsError(e);
}
}
@SuppressWarnings("rawtypes")
public static List combineResults(Object[] array, Method method)
{
if (isEmpty(array))
{ return new ArrayList(0); }
try
{
ArrayList list = new ArrayList();
for (int i = 0; i < array.length; i++)
{
addArray(list, (Object[]) method.invoke(array[i], (Object[]) null));
}
return list;
}
catch (Throwable e)
{
throw ObjectUtils.throwAsError(e);
}
}
public static T[] combine(T[] a, T[] b)
{
if (isEmpty(a) && isEmpty(b))
{
if (a != null)
{
return (T[]) Array.newInstance(a.getClass().getComponentType(), 0);
}
else if (b != null)
{
return (T[]) Array.newInstance(b.getClass().getComponentType(), 0);
}
else
{
return (T[]) Collections.emptyList().toArray();
}
}
else if (isEmpty(a))
{
return b;
}
else if (isEmpty(b))
{
return a;
}
else
{
T[] newArray = (T[]) Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
System.arraycopy(a, 0, newArray, 0, a.length);
System.arraycopy(b, 0, newArray, a.length, b.length);
return newArray;
}
}
public static T[] combine(T a, T... b)
{
if (b == null)
{
b = (T[]) Array.newInstance(getClassFor(a), 1);
}
final Class> type = b.getClass().getComponentType();
final T[] toArray = (T[]) Array.newInstance(type, 1);
toArray[0] = a;
return combine(toArray, b);
}
private static Class> getClassFor(T object)
{
return object != null ? object.getClass() : Object.class;
}
public static boolean contains(T[] values, T value)
{
for (int i = 0; i < values.length; i++)
{
if (value.equals(values[i]))
{ return true; }
}
return false;
}
public static boolean contains(int[] values, int value)
{
for (int i = 0; i < values.length; i++)
{
if (value == values[i])
{ return true; }
}
return false;
}
public static T getLast(List list)
{
return list.get(list.size() - 1);
}
public static T getDefault(HashMap map, K key, T defaultValue)
{
T value = map.get(key);
if (value == null)
{
map.put(key, defaultValue);
value = defaultValue;
}
return value;
}
public static int countValues(HashMap out, V matching)
{
return count(matching, out.values());
}
public static int count(V matching, Collection values)
{
int count = 0;
for (V value : values)
{
if (matching.equals(value))
{
count++;
}
}
return count;
}
public static List combine(List list1, List list2)
{
List all = new ArrayList();
all.addAll(list1);
all.addAll(list2);
return all;
}
public static Iterable asIterable(Iterator iterator)
{
return new IterableWrapper(iterator);
}
public static T[] toArray(List list)
{
return Queryable.as(list).asArray();
}
public static T[] toArray(List list, Class type)
{
return Queryable.as(list, type).asArray();
}
public static SPECIFIC_VALUE getOrElse(Map fields,
KEY key, Function0 defaultIfNotFound)
{
if (fields.containsKey(key))
{
return (SPECIFIC_VALUE) fields.get(key);
}
else
{
return defaultIfNotFound.call();
}
}
public static Collection asList(char[] toCharArray)
{
ArrayList allChars = new ArrayList<>();
for (char ch : toCharArray)
{
allChars.add(ch);
}
return allChars;
}
public static T[] of(T valueOfEachElement, int sizeOfArray)
{
Queryable queryable = new Queryable(valueOfEachElement.getClass());
for (int i = 0; i < sizeOfArray; i++)
{
queryable.add(valueOfEachElement);
}
return queryable.asArray();
}
public static int size(Iterable array)
{
if (array instanceof Collection)
{ return ((Collection) array).size(); }
return (int) StreamSupport.stream(array.spliterator(), false).count();
}
public static In getLast(Iterable array)
{
if (array == null)
{ return null; }
if (array instanceof List)
{
List array1 = (List) array;
if (array1.isEmpty())
{ return null; }
return array1.get(array1.size() - 1);
}
In last = null;
for (In in : array)
{
last = in;
}
return last;
}
public static class IterableWrapper implements Iterable
{
private final Iterator iterator;
public IterableWrapper(Iterator iterator)
{
this.iterator = iterator;
}
@Override
public Iterator iterator()
{
return iterator;
}
}
}