package com.marshalchen.ua.common.commonUtils.basicUtils;
import com.marshalchen.ua.common.commonUtils.logUtils.Logs;
import java.util.*;
/**
* Array Utils
* <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 {
/**
* 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 (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 (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 = 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 = 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 = 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 = transformIntArray(sourceArray);
return getNext(array, value, defaultValue, isCircle);
}
public static void iteratorArrayList(ArrayList arrayList) {
Iterator it1 = arrayList.iterator();
while (it1.hasNext()) {
int i = 0;
Iterator it = ((HashMap<String, String>) it1.next()).entrySet().iterator();
while (it.hasNext()) {
i++;
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
Logs.d("key--" + key + " value " + value + "\n");
}
}
}
public static void iteratorList(List inputList) {
Iterator it1 = inputList.iterator();
while (it1.hasNext()) {
int i = 0;
Iterator it = ((HashMap<String, String>) it1.next()).entrySet().iterator();
while (it.hasNext()) {
i++;
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
Logs.d("key--" + key + " value " + value + "\n");
}
}
}
public static void iteratorHashMap(HashMap<String, Object> hashMap) {
Iterator it = hashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
Logs.d(pairs.getKey() + " = " + pairs.getValue());
}
}
public static void iteratorHashMapString(HashMap<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
Logs.d(entry.getKey() + "/" + entry.getValue());
}
}
/**
* compare two object
*
* @param actual
* @param expected
* @return <ul>
* <li>if both are null, return true</li>
* <li>return actual.{@link Object#equals(Object)}</li>
* </ul>
*/
public static boolean isEquals(Object actual, Object expected) {
return actual == expected || (actual == null ? expected == null : actual.equals(expected));
}
/**
* convert long array to Long array
*
* @param source
* @return
*/
public static Long[] transformLongArray(long[] source) {
Long[] destin = new Long[source.length];
for (int i = 0; i < source.length; i++) {
destin[i] = source[i];
}
return destin;
}
/**
* convert Long array to long array
*
* @param source
* @return
*/
public static long[] transformLongArray(Long[] source) {
long[] destin = new long[source.length];
for (int i = 0; i < source.length; i++) {
destin[i] = source[i];
}
return destin;
}
/**
* convert int array to Integer array
*
* @param source
* @return
*/
public static Integer[] transformIntArray(int[] source) {
Integer[] destin = new Integer[source.length];
for (int i = 0; i < source.length; i++) {
destin[i] = source[i];
}
return destin;
}
/**
* convert Integer array to int array
*
* @param source
* @return
*/
public static int[] transformIntArray(Integer[] source) {
int[] destin = new int[source.length];
for (int i = 0; i < source.length; i++) {
destin[i] = source[i];
}
return destin;
}
}