package com.anthony.library.utils;
import android.util.Pair;
import java.util.Arrays;
/**
* Array SwipeBackUtils
* <ul>
* <li>{@link #isEmpty(Object[])} is null or its length is 0</li>
* <li>{@link #getLast(Object[], Object, Object, boolean)} get last element of the target element, before the first one
* that match the target element front to back</li>
* <li>{@link #getNext(Object[], Object, Object, boolean)} get next element of the target element, after the first one
* that match the target element front to back</li>
* <li>{@link #getLast(Object[], Object, boolean)}</li>
* <li>{@link #getLast(int[], int, int, boolean)}</li>
* <li>{@link #getLast(long[], long, long, boolean)}</li>
* <li>{@link #getNext(Object[], Object, boolean)}</li>
* <li>{@link #getNext(int[], int, int, boolean)}</li>
* <li>{@link #getNext(long[], long, long, boolean)}</li>
* </ul>
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2011-10-24
*/
public class ArrayUtils {
private ArrayUtils() {
throw new AssertionError();
}
/**
* is null or its length is 0
*
* @param <V>
* @param sourceArray
* @return
*/
public static <V> boolean isEmpty(V[] sourceArray) {
return (sourceArray == null || sourceArray.length == 0);
}
/**
* get last element of the target element, before the first one that match the target element front to back
* <ul>
* <li>if array is empty, return defaultValue</li>
* <li>if target element is not exist in array, return defaultValue</li>
* <li>if target element exist in array and its index is not 0, return the last element</li>
* <li>if target element exist in array and its index is 0, return the last one in array if isCircle is true, else
* return defaultValue</li>
* </ul>
*
* @param <V>
* @param sourceArray
* @param value value of target element
* @param defaultValue default return value
* @param isCircle whether is circle
* @return
*/
public static <V> V getLast(V[] sourceArray, V value, V defaultValue, boolean isCircle) {
if (isEmpty(sourceArray)) {
return defaultValue;
}
int currentPosition = -1;
for (int i = 0; i < sourceArray.length; i++) {
if (ObjectUtils.isEquals(value, sourceArray[i])) {
currentPosition = i;
break;
}
}
if (currentPosition == -1) {
return defaultValue;
}
if (currentPosition == 0) {
return isCircle ? sourceArray[sourceArray.length - 1] : defaultValue;
}
return sourceArray[currentPosition - 1];
}
/**
* get next element of the target element, after the first one that match the target element front to back
* <ul>
* <li>if array is empty, return defaultValue</li>
* <li>if target element is not exist in array, return defaultValue</li>
* <li>if target element exist in array and not the last one in array, return the next element</li>
* <li>if target element exist in array and the last one in array, return the first one in array if isCircle is
* true, else return defaultValue</li>
* </ul>
*
* @param <V>
* @param sourceArray
* @param value value of target element
* @param defaultValue default return value
* @param isCircle whether is circle
* @return
*/
public static <V> V getNext(V[] sourceArray, V value, V defaultValue, boolean isCircle) {
if (isEmpty(sourceArray)) {
return defaultValue;
}
int currentPosition = -1;
for (int i = 0; i < sourceArray.length; i++) {
if (ObjectUtils.isEquals(value, sourceArray[i])) {
currentPosition = i;
break;
}
}
if (currentPosition == -1) {
return defaultValue;
}
if (currentPosition == sourceArray.length - 1) {
return isCircle ? sourceArray[0] : defaultValue;
}
return sourceArray[currentPosition + 1];
}
/**
* @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} defaultValue is null
*/
public static <V> V getLast(V[] sourceArray, V value, boolean isCircle) {
return getLast(sourceArray, value, null, isCircle);
}
/**
* @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} defaultValue is null
*/
public static <V> V getNext(V[] sourceArray, V value, boolean isCircle) {
return getNext(sourceArray, value, null, isCircle);
}
/**
* @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} Object is Long
*/
public static long getLast(long[] sourceArray, long value, long defaultValue, boolean isCircle) {
if (sourceArray.length == 0) {
throw new IllegalArgumentException("The length of source array must be greater than 0.");
}
Long[] array = ObjectUtils.transformLongArray(sourceArray);
return getLast(array, value, defaultValue, isCircle);
}
/**
* @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} Object is Long
*/
public static long getNext(long[] sourceArray, long value, long defaultValue, boolean isCircle) {
if (sourceArray.length == 0) {
throw new IllegalArgumentException("The length of source array must be greater than 0.");
}
Long[] array = ObjectUtils.transformLongArray(sourceArray);
return getNext(array, value, defaultValue, isCircle);
}
/**
* @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} Object is Integer
*/
public static int getLast(int[] sourceArray, int value, int defaultValue, boolean isCircle) {
if (sourceArray.length == 0) {
throw new IllegalArgumentException("The length of source array must be greater than 0.");
}
Integer[] array = ObjectUtils.transformIntArray(sourceArray);
return getLast(array, value, defaultValue, isCircle);
}
/**
* @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} Object is Integer
*/
public static int getNext(int[] sourceArray, int value, int defaultValue, boolean isCircle) {
if (sourceArray.length == 0) {
throw new IllegalArgumentException("The length of source array must be greater than 0.");
}
Integer[] array = ObjectUtils.transformIntArray(sourceArray);
return getNext(array, value, defaultValue, isCircle);
}
/**
* 获取数组的纬度
*
* @param objects
* @return
*/
public static int getArrayDimension(Object objects) {
int dim = 0;
for (int i = 0; i < objects.toString().length(); ++i) {
if (objects.toString().charAt(i) == '[') {
++dim;
} else {
break;
}
}
return dim;
}
public static Pair<Pair<Integer, Integer>, String> arrayToObject(Object object) {
StringBuilder builder = new StringBuilder();
int cross = 0, vertical = 0;
if (object instanceof int[][]) {
int[][] ints = (int[][]) object;
cross = ints.length;
vertical = cross == 0 ? 0 : ints[0].length;
for (int[] ints1 : ints) {
builder.append(arrayToString(ints1).second + "\n");
}
} else if (object instanceof byte[][]) {
byte[][] ints = (byte[][]) object;
cross = ints.length;
vertical = cross == 0 ? 0 : ints[0].length;
for (byte[] ints1 : ints) {
builder.append(arrayToString(ints1).second + "\n");
}
} else if (object instanceof short[][]) {
short[][] ints = (short[][]) object;
cross = ints.length;
vertical = cross == 0 ? 0 : ints[0].length;
for (short[] ints1 : ints) {
builder.append(arrayToString(ints1).second + "\n");
}
} else if (object instanceof long[][]) {
long[][] ints = (long[][]) object;
cross = ints.length;
vertical = cross == 0 ? 0 : ints[0].length;
for (long[] ints1 : ints) {
builder.append(arrayToString(ints1).second + "\n");
}
} else if (object instanceof float[][]) {
float[][] ints = (float[][]) object;
cross = ints.length;
vertical = cross == 0 ? 0 : ints[0].length;
for (float[] ints1 : ints) {
builder.append(arrayToString(ints1).second + "\n");
}
} else if (object instanceof double[][]) {
double[][] ints = (double[][]) object;
cross = ints.length;
vertical = cross == 0 ? 0 : ints[0].length;
for (double[] ints1 : ints) {
builder.append(arrayToString(ints1).second + "\n");
}
} else if (object instanceof boolean[][]) {
boolean[][] ints = (boolean[][]) object;
cross = ints.length;
vertical = cross == 0 ? 0 : ints[0].length;
for (boolean[] ints1 : ints) {
builder.append(arrayToString(ints1).second + "\n");
}
} else if (object instanceof char[][]) {
char[][] ints = (char[][]) object;
cross = ints.length;
vertical = cross == 0 ? 0 : ints[0].length;
for (char[] ints1 : ints) {
builder.append(arrayToString(ints1).second + "\n");
}
} else {
Object[][] objects = (Object[][]) object;
cross = objects.length;
vertical = cross == 0 ? 0 : objects[0].length;
for (Object[] objects1 : objects) {
builder.append(arrayToString(objects1).second + "\n");
}
}
return Pair.create(Pair.create(cross, vertical), builder.toString());
}
/**
* 数组转化为字符串
*
* @param object
* @return
*/
public static Pair arrayToString(Object object) {
StringBuilder builder = new StringBuilder("[");
int length = 0;
if (object instanceof int[]) {
int[] ints = (int[]) object;
length = ints.length;
for (int i : ints) {
builder.append(i + ",\t");
}
} else if (object instanceof byte[]) {
byte[] bytes = (byte[]) object;
length = bytes.length;
for (byte item : bytes) {
builder.append(item + ",\t");
}
} else if (object instanceof short[]) {
short[] shorts = (short[]) object;
length = shorts.length;
for (short item : shorts) {
builder.append(item + ",\t");
}
} else if (object instanceof long[]) {
long[] longs = (long[]) object;
length = longs.length;
for (long item : longs) {
builder.append(item + ",\t");
}
} else if (object instanceof float[]) {
float[] floats = (float[]) object;
length = floats.length;
for (float item : floats) {
builder.append(item + ",\t");
}
} else if (object instanceof double[]) {
double[] doubles = (double[]) object;
length = doubles.length;
for (double item : doubles) {
builder.append(item + ",\t");
}
} else if (object instanceof boolean[]) {
boolean[] booleans = (boolean[]) object;
length = booleans.length;
for (boolean item : booleans) {
builder.append(item + ",\t");
}
} else if (object instanceof char[]) {
char[] chars = (char[]) object;
length = chars.length;
for (char item : chars) {
builder.append(item + ",\t");
}
} else {
Object[] objects = (Object[]) object;
length = objects.length;
for (Object item : objects) {
builder.append(StringUtils.objectToString(item)+ ",\t");
}
}
return Pair.create(length, builder.replace(builder.length() - 2, builder.length(), "]").toString());
}
/**
* 是否为数组
*
* @param object
* @return
*/
public static boolean isArray(Object object) {
return object.getClass().isArray();
}
/**
* 获取数组类型
*
* @param object 如L为int型
* @return
*/
public static char getType(Object object) {
if (isArray(object)) {
String str = object.toString();
return str.substring(str.lastIndexOf("[") + 1, str.lastIndexOf("[") + 2).charAt(0);
}
return 0;
}
/**
* 遍历数组
*
* @param result
* @param object
*/
private static void traverseArray(StringBuilder result, Object object) {
if (!isArray(object)) {
result.append(object.toString());
return;
}
if (getArrayDimension(object) == 1) {
switch (getType(object)) {
case 'I':
result.append(Arrays.toString((int[]) object)).append("\n");
return;
case 'D':
result.append(Arrays.toString((double[]) object)).append("\n");
return;
case 'Z':
result.append(Arrays.toString((boolean[]) object)).append("\n");
return;
case 'B':
result.append(Arrays.toString((byte[]) object)).append("\n");
return;
case 'S':
result.append(Arrays.toString((short[]) object)).append("\n");
return;
case 'J':
result.append(Arrays.toString((long[]) object)).append("\n");
return;
case 'F':
result.append(Arrays.toString((float[]) object)).append("\n");
return;
case 'L':
result.append(Arrays.toString((Object[]) object)).append("\n");
default:
return;
}
}
for (int i = 0; i < ((Object[]) object).length; i++) {
traverseArray(result, ((Object[]) object)[i]);
}
}
public static String traverseArray(Object object) {
StringBuilder result = new StringBuilder();
traverseArray(result, object);
return result.toString();
}
}