All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.andyshao.util.ArrayTools Maven / Gradle / Ivy

There is a newer version: 4.0
Show newest version
package com.github.andyshao.util;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Map;

import com.github.andyshao.convert.Convert;

/**
 * Some tools of {@link Array}
*

* At least JDK1.8 *

* * @author Andy */ public final class ArrayTools { /** * Let a Object[][] convert to a Map. * Let Object[i][0] is key and Object[i][1] is value. * * @param convertK the convert of key's * @param convertV the convert of value's * @param arrays the information * @param the type of key * @param the type of value * @return the Map what you want to get */ public static Map convertToMap( Convert convertK , Convert convertV , Object[]... arrays) { Map map = new HashMap(); for (Object[] array : arrays) { map.put(convertK.convert(array[0]) , convertV.convert(array[1])); } return map; } /** * find out the location of first item. * * @param array * the array which be processed * @param item The item which should be remove * @param the type of array * @return if can't find out anything then return -1 */ public static int findFirstItem(T array , Object item) { for (int i = 0 ; i < Array.getLength(array) ; i++) { if (Array.get(array , i).equals(item)) { return i; } } return -1; } /** * find out the location of last item. * * @param array * the array which be processed * @param item * The item which should be remove * @param the type of array * @return if can't find out anything then return -1 */ public static int findLastItem(T array , Object item) { for (int i = Array.getLength(array) - 1 ; i >= 0 ; i--) { if (Array.get(array , i).equals(item)) { return i; } } return -1; } /** * * @param array the array which is processed. * @param the type of array * @return A flip array. */ @SuppressWarnings("unchecked") public static T flipArray(T array) { if (!array.getClass().isArray()) { throw new IllegalArgumentException("The array must be a array"); } int length = Array.getLength(array); if (length == 0) { return array; } T temp = (T) Array.newInstance(array.getClass().getComponentType() , length); for (int i = length - 1 , b = 0 ; i >= 0 ; i-- , b++) { Array.set(temp , b , Array.get(array , i)); } return temp; } @SuppressWarnings("unchecked") public static T getValue(T[] array , int index , T nullDefault) { if (!array.getClass().isArray()) { throw new IllegalArgumentException("The input must be a array."); } if (Array.getLength(array) <= index) { return nullDefault; } return (T) Array.get(array , index); } /** * Merge the Arrays.
* * @param array_type * the class type of the finally array. * @param arrays which should be merged * @param the type of array * @return the finally array which contain all of information of arrays. */ @SuppressWarnings("unchecked") @SafeVarargs public static T mergeArray(Class array_type , T... arrays) { if (!array_type.isArray()) { throw new IllegalArgumentException("The inputs must be a array"); } int length = 0; for (T array : arrays) { length += Array.getLength(array); } T result = (T) Array.newInstance(array_type.getComponentType() , length); for (int i = 0 , point = 0 ; i < arrays.length ; point += Array.getLength(arrays[i]) , i++) { System.arraycopy(arrays[i] , 0 , result , point , Array.getLength(arrays[i])); } return result; } /** * Try to convert the array.
* eg.
* int[] -- Integer[]
* int[] -- long[]
* Take Note: long[] can't convert to int[]. * It is low efficient. It need use a for loop copy array. * * @param in input array * @param outClazz output array type * @param Input array type * @param Ouput array type * @return output array */ @SuppressWarnings("unchecked") public static OUT pack_unpack(IN in , Class outClazz) { if (!in.getClass().isArray() || !outClazz.isArray()) { throw new IllegalArgumentException( "in or outClazz is not array"); } OUT result = (OUT) Array.newInstance(outClazz.getComponentType() , Array.getLength(in)); for (int i = 0 ; i < Array.getLength(in) ; i++) { Array.set(result , i , Array.get(in , i)); } return result; } /** * Remove all of item which be included in array. * * @param array * The array be processed. * @param item The item which should be remove * @param the type of array * @param the type of member of array * @return a array which has been processed. */ public static T removeAllItem(T array , I item) { ARRAY: for (int i = 0 ; i < Array.getLength(array) ; i++) { if (Array.get(array , i).equals(item)) { array = ArrayTools.removeItem(array , i); continue ARRAY; } } return array; } /** * Remove the first location which included the item in array. * * @param array * the array which be processed. * @param item the item which should be remove. * @param the type of array * @param the type of member of array * @return a array which has been processed. */ public static T removeFirstItem(T array , I item) { int index = ArrayTools.findFirstItem(array , item); if (index >= 0) { array = ArrayTools.removeItem(array , index); } return array; } /** * Remove the array[i] from the array. * * @param array * the array be processed. * @param i the location which should be removed. * @param the type of array * @return a array which has been processed. */ public static T removeItem(T array , int i) { return ArrayTools.removeItem(array , i , i + 1); } /** * Remove the array from start to end. the end is not include. * * @param array the array be processed. * @param start start index * @param end end index * @param the type of array * @return a array which has been processed. */ @SuppressWarnings("unchecked") public static T removeItem(T array , int start , int end) { if (!array.getClass().isArray()) { throw new IllegalArgumentException("The array is not the ARRAY type."); } if (start < 0 || end < 0 || end <= start) { throw new IllegalArgumentException( "The argument that start or end is not right."); } if (start >= Array.getLength(array)) { return array; } T head = start == 0 ? ArrayTools.splitArray(array , end , Array.getLength(array)) : ArrayTools.splitArray(array , 0 , start); T tail = start == 0 ? (T) Array.newInstance(array.getClass().getComponentType() , 0) : ArrayTools.splitArray(array , end , Array.getLength(array)); array = ArrayTools.mergeArray((Class) array.getClass() , head , tail); return array; } /** * Remove the last location of item from array. * * @param array * the array which be processed. * @param item the item which should be remove. * @param the type of array * @param the type of member of array * @return a array which has been processed. */ public static T removeLastItem(T array , I item) { int index = ArrayTools.findLastItem(array , item); if (index >= 0) { array = ArrayTools.removeItem(array , index); } return array; } /** * Get a small array from the array. * * @param array * the array be processed. * @param from * the start of location of array * @param end the end of location of array. NOTE: never include itself. * @param the type of array * @return a array which has been processed. */ @SuppressWarnings("unchecked") public static T splitArray(T array , int from , int end) { if (!array.getClass().isArray()) { throw new IllegalArgumentException("The array must be a array"); } T result = (T) Array.newInstance(array.getClass().getComponentType() , end - from); System.arraycopy(array , from , result , 0 , end - from); return result; } /** * It is a easy way could create a array. * * @param targets the method what you want to return * @param the type of array * @return just return the targets */ @SafeVarargs public static T[] toArray(T... targets) { return targets; } private ArrayTools() { throw new AssertionError("No ArrayTools instances for you!"); } }