Created
June 19, 2019 10:57
-
-
Save Guiorgy/70227f86707d18c86a5767acbffa02d5 to your computer and use it in GitHub Desktop.
A simple List to primitive array, and primitive array to List converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressWarnings({"WeakerAccess", "unused"}) | |
public abstract class PrimitiveContainerConverter { | |
public interface ListByteRef extends Supplier<List<? extends Byte>> {} | |
/** | |
* Convert Lists of {@link Byte} to array of primitive type byte | |
* | |
* usage: array = toPrimitiveArray(() -> list); | |
*/ | |
public static byte[] toPrimitiveArray(final ListByteRef byteListRef) { | |
List<? extends Byte> byteList = byteListRef.get(); | |
final byte[] primitives = new byte[byteList.size()]; | |
int index = 0; | |
for (Byte object : byteList) { | |
primitives[index++] = object; | |
} | |
return primitives; | |
} | |
/** | |
* Convert array of primitive type byte to Lists of {@link Byte} | |
*/ | |
public static List<Byte> toList(final byte[] byteArray) { | |
final List<Byte> list = new ArrayList<Byte>(byteArray.length); | |
for (byte b : byteArray) { | |
list.add(b); | |
} | |
return list; | |
} | |
public interface ListShortRef extends Supplier<List<? extends Short>> {} | |
/** | |
* Convert Lists of {@link Short} to array of primitive type short | |
* | |
* usage: array = toPrimitiveArray(() -> list); | |
*/ | |
public static short[] toPrimitiveArray(final ListShortRef shortListRef) { | |
List<? extends Short> shortList = shortListRef.get(); | |
final short[] primitives = new short[shortList.size()]; | |
int index = 0; | |
for (Short object : shortList) { | |
primitives[index++] = object; | |
} | |
return primitives; | |
} | |
/** | |
* Convert array of primitive type short to Lists of {@link Short} | |
*/ | |
public static List<Short> toList(final short[] shortArray) { | |
final List<Short> list = new ArrayList<Short>(shortArray.length); | |
for (short s : shortArray) { | |
list.add(s); | |
} | |
return list; | |
} | |
public interface ListIntegerRef extends Supplier<List<? extends Integer>> {} | |
/** | |
* Convert Lists of {@link Integer} to array of primitive type int | |
* | |
* usage: array = toPrimitiveArray(() -> list); | |
*/ | |
public static int[] toPrimitiveArray(final ListIntegerRef intListRef) { | |
List<? extends Integer> intList = intListRef.get(); | |
final int[] primitives = new int[intList.size()]; | |
int index = 0; | |
for (Integer object : intList) { | |
primitives[index++] = object; | |
} | |
return primitives; | |
} | |
/** | |
* Convert array of primitive type int to Lists of {@link Integer} | |
*/ | |
public static List<Integer> toList(final int[] intArray) { | |
final List<Integer> list = new ArrayList<Integer>(intArray.length); | |
for (int i : intArray) { | |
list.add(i); | |
} | |
return list; | |
} | |
public interface ListLongRef extends Supplier<List<? extends Long>> {} | |
/** | |
* Convert Lists of {@link Long} to array of primitive type long | |
* | |
* usage: array = toPrimitiveArray(() -> list); | |
*/ | |
public static long[] toPrimitiveArray(final ListLongRef longListRef) { | |
List<? extends Long> longList = longListRef.get(); | |
final long[] primitives = new long[longList.size()]; | |
int index = 0; | |
for (Long object : longList) { | |
primitives[index++] = object; | |
} | |
return primitives; | |
} | |
/** | |
* Convert array of primitive type long to Lists of {@link Long} | |
*/ | |
public static List<Long> toList(final long[] longArray) { | |
final List<Long> list = new ArrayList<Long>(longArray.length); | |
for (long l : longArray) { | |
list.add(l); | |
} | |
return list; | |
} | |
public interface ListFloatRef extends Supplier<List<? extends Float>> {} | |
/** | |
* Convert Lists of {@link Float} to array of primitive type float | |
* | |
* usage: array = toPrimitiveArray(() -> list); | |
*/ | |
public static float[] toPrimitiveArray(final ListFloatRef floatListRef) { | |
List<? extends Float> floatList = floatListRef.get(); | |
final float[] primitives = new float[floatList.size()]; | |
int index = 0; | |
for (Float object : floatList) { | |
primitives[index++] = object; | |
} | |
return primitives; | |
} | |
/** | |
* Convert array of primitive type float to Lists of {@link Float} | |
*/ | |
public static List<Float> toList(final float[] floatArray) { | |
final List<Float> list = new ArrayList<Float>(floatArray.length); | |
for (float f : floatArray) { | |
list.add(f); | |
} | |
return list; | |
} | |
public interface ListDoubleRef extends Supplier<List<? extends Double>> {} | |
/** | |
* Convert Lists of {@link Double} to array of primitive type double | |
* | |
* usage: array = toPrimitiveArray(() -> list); | |
*/ | |
public static double[] toPrimitiveArray(final ListDoubleRef doubleListRef) { | |
List<? extends Double> doubleList = doubleListRef.get(); | |
final double[] primitives = new double[doubleList.size()]; | |
int index = 0; | |
for (Double object : doubleList) { | |
primitives[index++] = object; | |
} | |
return primitives; | |
} | |
/** | |
* Convert array of primitive type double to Lists of {@link Double} | |
*/ | |
public static List<Double> toList(final double[] doubleArray) { | |
final List<Double> list = new ArrayList<Double>(doubleArray.length); | |
for (double d : doubleArray) { | |
list.add(d); | |
} | |
return list; | |
} | |
public interface ListBooleanRef extends Supplier<List<? extends Boolean>> {} | |
/** | |
* Convert Lists of {@link Boolean} to array of primitive type boolean | |
* | |
* usage: array = toPrimitiveArray(() -> list); | |
*/ | |
public static boolean[] toPrimitiveArray(final ListBooleanRef booleanListRef) { | |
List<? extends Boolean> booleanList = booleanListRef.get(); | |
final boolean[] primitives = new boolean[booleanList.size()]; | |
int index = 0; | |
for (Boolean object : booleanList) { | |
primitives[index++] = object; | |
} | |
return primitives; | |
} | |
/** | |
* Convert array of primitive type boolean to Lists of {@link Boolean} | |
*/ | |
public static List<Boolean> toList(final boolean[] booleanArray) { | |
final List<Boolean> list = new ArrayList<Boolean>(booleanArray.length); | |
for (boolean b : booleanArray) { | |
list.add(b); | |
} | |
return list; | |
} | |
public interface ListCharacterRef extends Supplier<List<? extends Character>> {} | |
/** | |
* Convert Lists of {@link Character} to array of primitive type char | |
* | |
* usage: array = toPrimitiveArray(() -> list); | |
*/ | |
public static char[] toPrimitiveArray(final ListCharacterRef charListRef) { | |
List<? extends Character> charList = charListRef.get(); | |
final char[] primitives = new char[charList.size()]; | |
int index = 0; | |
for (Character object : charList) { | |
primitives[index++] = object; | |
} | |
return primitives; | |
} | |
/** | |
* Convert array of primitive type char to Lists of {@link Character} | |
*/ | |
public static List<Character> toList(final char[] charArray) { | |
final List<Character> list = new ArrayList<Character>(charArray.length); | |
for (char c : charArray) { | |
list.add(c); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment