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

com.abubusoft.kripton.common.CollectionUtils Maven / Gradle / Ivy

There is a newer version: 8.2.0-rc.4
Show newest version
package com.abubusoft.kripton.common;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import com.abubusoft.kripton.exception.KriptonRuntimeException;

/**
 * Utility to work with list and array Json conversion.
 * 
 * @author Francesco Benincasa ([email protected])
 *
 */
public class CollectionUtils {

	/**
	 * create a collection set, with initial values.
	 * @param 
	 * 
	 * @param objects
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static  Set asSet(Class itemType, T... objects) {
		LinkedHashSet result = new LinkedHashSet();

		for (T item : objects) {
			result.add(item);
		}

		return result;
	}

	public static > E asList(Byte[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((Byte) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	//
	public static > E asList(Boolean[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((Boolean) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > E asList(Character[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((Character) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	@SuppressWarnings("unchecked")
	public static , T> E asList(T[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((T) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > E asList(Short[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((Short) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > E asList(Integer[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((Integer) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > List asList(Long[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((Long) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > List asList(Float[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((Float) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > List asList(Double[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((Double) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	//

	public static > E asList(boolean[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((boolean) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > E asList(char[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((char) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > E asList(short[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((short) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > E asList(int[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((int) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > List asList(long[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((long) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > List asList(float[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((float) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static > List asList(double[] array, Class listType) {
		E result;
		try {
			result = listType.newInstance();

			for (Object item : array) {
				result.add((double) item);
			}

			return result;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw (new KriptonRuntimeException(e.getMessage()));
		}
	}

	public static boolean[] asBooleanTypeArray(List input) {
		boolean[] result = new boolean[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static byte[] asByteTypeArray(List input) {
		byte[] result = new byte[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static char[] asCharacterTypeArray(List input) {
		char[] result = new char[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static short[] asShortTypeArray(List input) {
		short[] result = new short[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static int[] asIntegerTypeArray(List input) {
		int[] result = new int[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static long[] asLongTypeArray(List input) {
		long[] result = new long[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static float[] asFloatTypeArray(List input) {
		float[] result = new float[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static double[] asDoubleTypeArray(List input) {
		double[] result = new double[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static String[] asStringArray(List input) {
		String[] result = new String[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static Boolean[] asBooleanArray(List input) {
		Boolean[] result = new Boolean[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static Byte[] asByteArray(List input) {
		Byte[] result = new Byte[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static Character[] asCharacterArray(List input) {
		Character[] result = new Character[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static Short[] asShortArray(List input) {
		Short[] result = new Short[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static Integer[] asIntegerArray(List input) {
		Integer[] result = new Integer[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static Long[] asLongArray(List input) {
		Long[] result = new Long[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static  E[] asArray(List input, E[] newArray) {
		return input.toArray(newArray);
	}

	public static Float[] asFloatArray(List input) {
		Float[] result = new Float[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	public static Double[] asDoubleArray(List input) {
		Double[] result = new Double[input.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = input.get(i);
		}

		return result;
	}

	/**
	 * trim each element of lists.
	 * 
	 * @param value
	 */
	public static void trim(List value) {
		if (value==null) return;
		for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy