OnJava8-Examples/onjava/ConvertTo.java

106 lines
3.0 KiB
Java
Raw Permalink Normal View History

// onjava/ConvertTo.java
// (c)2021 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 {
static boolean[] primitive(Boolean[] in) {
2015-06-15 17:47:35 -07:00
boolean[] result = new boolean[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i]; // Autounboxing
return result;
}
static char[] primitive(Character[] in) {
2015-06-15 17:47:35 -07:00
char[] result = new char[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static byte[] primitive(Byte[] in) {
2015-06-15 17:47:35 -07:00
byte[] result = new byte[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static short[] primitive(Short[] in) {
2015-06-15 17:47:35 -07:00
short[] result = new short[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static int[] primitive(Integer[] in) {
2015-06-15 17:47:35 -07:00
int[] result = new int[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static long[] primitive(Long[] in) {
2015-06-15 17:47:35 -07:00
long[] result = new long[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static float[] primitive(Float[] in) {
2015-06-15 17:47:35 -07:00
float[] result = new float[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static double[] primitive(Double[] in) {
2015-06-15 17:47:35 -07:00
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:
static Boolean[] boxed(boolean[] in) {
2016-01-25 18:05:55 -08:00
Boolean[] result = new Boolean[in.length];
for(int i = 0; i < in.length; i++)
2017-05-15 16:15:22 -06:00
result[i] = in[i]; // Autoboxing
2016-01-25 18:05:55 -08:00
return result;
}
static Character[] boxed(char[] in) {
2016-01-25 18:05:55 -08:00
Character[] result = new Character[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static Byte[] boxed(byte[] in) {
2016-01-25 18:05:55 -08:00
Byte[] result = new Byte[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static Short[] boxed(short[] in) {
2016-01-25 18:05:55 -08:00
Short[] result = new Short[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static Integer[] boxed(int[] in) {
2016-01-25 18:05:55 -08:00
Integer[] result = new Integer[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static Long[] boxed(long[] in) {
2016-01-25 18:05:55 -08:00
Long[] result = new Long[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static Float[] boxed(float[] in) {
2016-01-25 18:05:55 -08:00
Float[] result = new Float[in.length];
for(int i = 0; i < in.length; i++)
result[i] = in[i];
return result;
}
static Double[] boxed(double[] in) {
2016-01-25 18:05:55 -08:00
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
}