OnJava8-Examples/onjava/ConvertTo.java

106 lines
3.1 KiB
Java
Raw Normal View History

// onjava/ConvertTo.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
package onjava;
2015-06-15 17:47:35 -07:00
2016-01-25 18:05:55 -08:00
public interface ConvertTo {
2015-06-15 17:47:35 -07:00
public static boolean[] primitive(Boolean[] in) {
boolean[] result = new boolean[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i]; // Autounboxing
return result;
}
public static char[] primitive(Character[] in) {
char[] result = new char[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static byte[] primitive(Byte[] in) {
byte[] result = new byte[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static short[] primitive(Short[] in) {
short[] result = new short[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static int[] primitive(Integer[] in) {
int[] result = new int[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static long[] primitive(Long[] in) {
long[] result = new long[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static float[] primitive(Float[] in) {
float[] result = new float[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static double[] primitive(Double[] in) {
double[] result = new double[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
2016-01-25 18:05:55 -08:00
// Convert from primitive array to wrapped array:
public static Boolean[] boxed(boolean[] in) {
Boolean[] result = new Boolean[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i]; // Autboxing
return result;
}
public static Character[] boxed(char[] in) {
Character[] result = new Character[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static Byte[] boxed(byte[] in) {
Byte[] result = new Byte[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static Short[] boxed(short[] in) {
Short[] result = new Short[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static Integer[] boxed(int[] in) {
Integer[] result = new Integer[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static Long[] boxed(long[] in) {
Long[] result = new Long[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static Float[] boxed(float[] in) {
Float[] result = new Float[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
public static Double[] boxed(double[] in) {
Double[] result = new Double[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
2015-09-07 11:44:36 -06:00
}