|
| 1 | +/** |
| 2 | + * Copyright 2009-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.apache.ibatis.type; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author wuwen.55@gmail.com |
| 20 | + */ |
| 21 | +public class ArraysUtils { |
| 22 | + |
| 23 | + private ArraysUtils() { |
| 24 | + // Prevent Instantiation |
| 25 | + } |
| 26 | + |
| 27 | + public static Object toPrimitiveArray(Object[] obj, Class<?> classes) { |
| 28 | + if (classes == int.class) { |
| 29 | + return toPrimitiveInt(obj); |
| 30 | + } else if (classes == boolean.class) { |
| 31 | + return toPrimitiveBoolean(obj); |
| 32 | + } else if (classes == byte.class) { |
| 33 | + return toPrimitiveByte(obj); |
| 34 | + } else if (classes == long.class) { |
| 35 | + return toPrimitiveLong(obj); |
| 36 | + } else if (classes == float.class) { |
| 37 | + return toPrimitiveFloat(obj); |
| 38 | + } else if (classes == double.class) { |
| 39 | + return toPrimitiveDouble(obj); |
| 40 | + } else if (classes == short.class) { |
| 41 | + return toPrimitiveShort(obj); |
| 42 | + } else if (classes == char.class) { |
| 43 | + return toPrimitiveChar(obj); |
| 44 | + } |
| 45 | + |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + public static int[] toPrimitiveInt(Object[] array) { |
| 51 | + final int[] result = new int[array.length]; |
| 52 | + for (int i = 0; i < array.length; i++) { |
| 53 | + result[i] = (Integer) array[i]; |
| 54 | + } |
| 55 | + return result; |
| 56 | + } |
| 57 | + |
| 58 | + public static long[] toPrimitiveLong(Object[] array) { |
| 59 | + final long[] result = new long[array.length]; |
| 60 | + for (int i = 0; i < array.length; i++) { |
| 61 | + result[i] = (Long) array[i]; |
| 62 | + } |
| 63 | + return result; |
| 64 | + } |
| 65 | + |
| 66 | + public static short[] toPrimitiveShort(Object[] array) { |
| 67 | + final short[] result = new short[array.length]; |
| 68 | + for (int i = 0; i < array.length; i++) { |
| 69 | + result[i] = (Short) array[i]; |
| 70 | + } |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + public static byte[] toPrimitiveByte(Object[] array) { |
| 75 | + final byte[] result = new byte[array.length]; |
| 76 | + for (int i = 0; i < array.length; i++) { |
| 77 | + result[i] = (Byte) array[i]; |
| 78 | + } |
| 79 | + return result; |
| 80 | + } |
| 81 | + |
| 82 | + public static double[] toPrimitiveDouble(Object[] array) { |
| 83 | + final double[] result = new double[array.length]; |
| 84 | + for (int i = 0; i < array.length; i++) { |
| 85 | + result[i] = (Double) array[i]; |
| 86 | + } |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + public static float[] toPrimitiveFloat(Object[] array) { |
| 91 | + |
| 92 | + final float[] result = new float[array.length]; |
| 93 | + for (int i = 0; i < array.length; i++) { |
| 94 | + result[i] = (Float) array[i]; |
| 95 | + } |
| 96 | + return result; |
| 97 | + } |
| 98 | + |
| 99 | + public static boolean[] toPrimitiveBoolean(Object[] array) { |
| 100 | + |
| 101 | + final boolean[] result = new boolean[array.length]; |
| 102 | + for (int i = 0; i < array.length; i++) { |
| 103 | + result[i] = (Boolean) array[i]; |
| 104 | + } |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + public static char[] toPrimitiveChar(Object[] array) { |
| 109 | + |
| 110 | + final char[] result = new char[array.length]; |
| 111 | + for (int i = 0; i < array.length; i++) { |
| 112 | + result[i] = (Character) array[i]; |
| 113 | + } |
| 114 | + return result; |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments