|
| 1 | +package Utils; |
| 2 | + |
| 3 | +/** |
| 4 | + * |
| 5 | + * @author kdgyun |
| 6 | + * |
| 7 | + * @version 1.1.0 |
| 8 | + * @since 1.1.0 |
| 9 | + * |
| 10 | + * {@link https://st-lab.tistory.com} |
| 11 | + * {@link https://github.com/kdgyun} |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +import java.util.stream.DoubleStream; |
| 16 | +import java.util.stream.IntStream; |
| 17 | +import java.util.stream.LongStream; |
| 18 | + |
| 19 | +public class Convert { |
| 20 | + |
| 21 | + private static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0]; |
| 22 | + private static final Character[] EMPTY_CHAR_OBJECT_ARRAY = new Character[0]; |
| 23 | + private static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0]; |
| 24 | + private static final Integer[] EMPTY_INT_OBJECT_ARRAY = new Integer[0]; |
| 25 | + private static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0]; |
| 26 | + private static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0]; |
| 27 | + private static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0]; |
| 28 | + |
| 29 | + public static Byte[] toByteArray(byte[] a) { |
| 30 | + return toByteArray(a, a.length); |
| 31 | + } |
| 32 | + |
| 33 | + private static final Byte[] toByteArray(byte[] a, int size) { |
| 34 | + if (a == null) { |
| 35 | + return null; |
| 36 | + } else if (size == 0) { |
| 37 | + return EMPTY_BYTE_OBJECT_ARRAY; |
| 38 | + } |
| 39 | + final Byte[] res = new Byte[size]; |
| 40 | + int i = 0; |
| 41 | + for (byte v : a) { |
| 42 | + res[i++] = v; |
| 43 | + } |
| 44 | + return res; |
| 45 | + } |
| 46 | + |
| 47 | + public static void tobyteArray(Byte[] a, byte[] dest) { |
| 48 | + tobyteArray(a, dest, dest.length); |
| 49 | + } |
| 50 | + |
| 51 | + private static final void tobyteArray(Byte[] a, byte[] dest, int size) { |
| 52 | + if (a == null) { |
| 53 | + throw new NullPointerException(); |
| 54 | + } else if (size == 0) { |
| 55 | + return; |
| 56 | + } |
| 57 | + int i = 0; |
| 58 | + for (byte v : a) { |
| 59 | + dest[i++] = v; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static Character[] toCharacterArray(char[] a) { |
| 64 | + return toCharacterArray(a, a.length); |
| 65 | + } |
| 66 | + |
| 67 | + private static final Character[] toCharacterArray(char[] a, int size) { |
| 68 | + if (a == null) { |
| 69 | + return null; |
| 70 | + } else if (size == 0) { |
| 71 | + return EMPTY_CHAR_OBJECT_ARRAY; |
| 72 | + } |
| 73 | + final Character[] res = new Character[size]; |
| 74 | + int i = 0; |
| 75 | + for (char v : a) { |
| 76 | + res[i++] = v; |
| 77 | + } |
| 78 | + return res; |
| 79 | + } |
| 80 | + |
| 81 | + public static void tocharArray(Character[] a, char[] dest) { |
| 82 | + tocharArray(a, dest, dest.length); |
| 83 | + } |
| 84 | + |
| 85 | + private static final void tocharArray(Character[] a, char[] dest, int size) { |
| 86 | + if (a == null) { |
| 87 | + throw new NullPointerException(); |
| 88 | + } else if (size == 0) { |
| 89 | + return; |
| 90 | + } |
| 91 | + int i = 0; |
| 92 | + for (char v : a) { |
| 93 | + dest[i++] = v; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static Short[] toShortArray(short[] a) { |
| 98 | + return toShortArray(a, a.length); |
| 99 | + } |
| 100 | + |
| 101 | + private static final Short[] toShortArray(short[] a, int size) { |
| 102 | + if (a == null) { |
| 103 | + return null; |
| 104 | + } else if (size == 0) { |
| 105 | + return EMPTY_SHORT_OBJECT_ARRAY; |
| 106 | + } |
| 107 | + final Short[] res = new Short[size]; |
| 108 | + int i = 0; |
| 109 | + for (short v : a) { |
| 110 | + res[i++] = v; |
| 111 | + } |
| 112 | + return res; |
| 113 | + } |
| 114 | + |
| 115 | + public static void toshortArray(Short[] a, short[] dest) { |
| 116 | + toshortArray(a, dest, dest.length); |
| 117 | + } |
| 118 | + |
| 119 | + private static final void toshortArray(Short[] a, short[] dest, int size) { |
| 120 | + if (a == null) { |
| 121 | + throw new NullPointerException(); |
| 122 | + } else if (size == 0) { |
| 123 | + return; |
| 124 | + } |
| 125 | + int i = 0; |
| 126 | + for (short v : a) { |
| 127 | + dest[i++] = v; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public static Integer[] toIntegerArray(int[] a) { |
| 132 | + return toIntegerArray(a, a.length); |
| 133 | + } |
| 134 | + |
| 135 | + private static final Integer[] toIntegerArray(int[] a, int size) { |
| 136 | + if (a == null) { |
| 137 | + return null; |
| 138 | + } else if (size == 0) { |
| 139 | + return EMPTY_INT_OBJECT_ARRAY; |
| 140 | + } |
| 141 | + final Integer[] res = IntStream.of(a).boxed().toArray(Integer[]::new); |
| 142 | + return res; |
| 143 | + } |
| 144 | + |
| 145 | + public static void tointtArray(Integer[] a, int[] dest) { |
| 146 | + tointtArray(a, dest, dest.length); |
| 147 | + } |
| 148 | + |
| 149 | + private static final void tointtArray(Integer[] a, int[] dest, int size) { |
| 150 | + if (a == null) { |
| 151 | + throw new NullPointerException(); |
| 152 | + } else if (size == 0) { |
| 153 | + return; |
| 154 | + } |
| 155 | + int i = 0; |
| 156 | + for (int v : a) { |
| 157 | + dest[i++] = v; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + public static Long[] toLongArray(long[] a) { |
| 162 | + return toLongArray(a, a.length); |
| 163 | + } |
| 164 | + |
| 165 | + private static final Long[] toLongArray(long[] a, int size) { |
| 166 | + if (a == null) { |
| 167 | + return null; |
| 168 | + } else if (size == 0) { |
| 169 | + return EMPTY_LONG_OBJECT_ARRAY; |
| 170 | + } |
| 171 | + final Long[] res = LongStream.of(a).boxed().toArray(Long[]::new); |
| 172 | + return res; |
| 173 | + } |
| 174 | + |
| 175 | + public static void tolongArray(Long[] a, long[] dest) { |
| 176 | + tolongArray(a, dest, dest.length); |
| 177 | + } |
| 178 | + |
| 179 | + private static final void tolongArray(Long[] a, long[] dest, int size) { |
| 180 | + if (a == null) { |
| 181 | + throw new NullPointerException(); |
| 182 | + } else if (size == 0) { |
| 183 | + return; |
| 184 | + } |
| 185 | + int i = 0; |
| 186 | + for (long v : a) { |
| 187 | + dest[i++] = v; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + public static Float[] toFloatArray(float[] a) { |
| 192 | + return toFloatArray(a, a.length); |
| 193 | + } |
| 194 | + |
| 195 | + private static final Float[] toFloatArray(float[] a, int size) { |
| 196 | + if (a == null) { |
| 197 | + return null; |
| 198 | + } else if (size == 0) { |
| 199 | + return EMPTY_FLOAT_OBJECT_ARRAY; |
| 200 | + } |
| 201 | + final Float[] res = new Float[size]; |
| 202 | + int i = 0; |
| 203 | + for (float v : a) { |
| 204 | + res[i++] = v; |
| 205 | + } |
| 206 | + return res; |
| 207 | + } |
| 208 | + |
| 209 | + public static void toflostArray(Float[] a, float[] dest) { |
| 210 | + toflostArray(a, dest, dest.length); |
| 211 | + } |
| 212 | + |
| 213 | + private static final void toflostArray(Float[] a, float[] dest, int size) { |
| 214 | + if (a == null) { |
| 215 | + throw new NullPointerException(); |
| 216 | + } else if (size == 0) { |
| 217 | + return; |
| 218 | + } |
| 219 | + int i = 0; |
| 220 | + for (float v : a) { |
| 221 | + dest[i++] = v; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + public static Double[] toDoubleArray(double[] a) { |
| 226 | + return toDoubleArray(a, a.length); |
| 227 | + } |
| 228 | + |
| 229 | + private static final Double[] toDoubleArray(double[] a, int size) { |
| 230 | + if (a == null) { |
| 231 | + return null; |
| 232 | + } else if (size == 0) { |
| 233 | + return EMPTY_DOUBLE_OBJECT_ARRAY; |
| 234 | + } |
| 235 | + final Double[] res = DoubleStream.of(a).boxed().toArray(Double[]::new); |
| 236 | + return res; |
| 237 | + } |
| 238 | + |
| 239 | + public static void todoubleArray(Double[] a, double[] dest) { |
| 240 | + todoubleArray(a, dest, dest.length); |
| 241 | + } |
| 242 | + |
| 243 | + private static final void todoubleArray(Double[] a, double[] dest, int size) { |
| 244 | + if (a == null) { |
| 245 | + throw new NullPointerException(); |
| 246 | + } else if (size == 0) { |
| 247 | + return; |
| 248 | + } |
| 249 | + int i = 0; |
| 250 | + for (double v : a) { |
| 251 | + dest[i++] = v; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | +} |
0 commit comments