• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yuu-nkjm / sorm4j / #542

14 Mar 2025 07:20AM UTC coverage: 94.877% (-0.05%) from 94.924%
#542

push

yuu-nkjm
Modify Try util

837 of 942 branches covered (88.85%)

Branch coverage included in aggregate %.

46 of 51 new or added lines in 12 files covered. (90.2%)

12 existing lines in 3 files now uncovered.

4274 of 4445 relevant lines covered (96.15%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.74
/sorm4j/src/main/java/org/nkjmlab/sorm4j/internal/util/ArrayUtils.java
1
package org.nkjmlab.sorm4j.internal.util;
2

3
import java.lang.reflect.Array;
4
import java.sql.SQLException;
5
import java.util.ArrayList;
6
import java.util.Arrays;
7
import java.util.List;
8

9
import org.nkjmlab.sorm4j.util.function.exception.Try;
10

11
public final class ArrayUtils {
12
  private static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0];
1✔
13
  private static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];
1✔
14
  private static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0];
1✔
15
  private static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0];
1✔
16
  private static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];
1✔
17
  private static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
1✔
18
  private static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];
1✔
19
  private static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0];
1✔
20

21
  private ArrayUtils() {}
22

23
  public static int[] add(int[] array, int i) {
24
    final int arrayLength = Array.getLength(array);
1✔
25
    final int[] newArray = new int[arrayLength + 1];
1✔
26
    System.arraycopy(array, 0, newArray, 0, arrayLength);
1✔
27
    newArray[arrayLength] = i;
1✔
28
    return newArray;
1✔
29
  }
30

31
  @SafeVarargs
32
  public static <T> List<T[]> split(int size, T... objects) {
33
    int slotNum = Math.floorDiv(objects.length, size);
1✔
34
    List<T[]> result = new ArrayList<>(slotNum + 1);
1✔
35

36
    for (int i = 0; i < slotNum; i++) {
1✔
37
      result.add(Arrays.copyOfRange(objects, size * i, size * (i + 1)));
1✔
38
    }
39
    if (size * slotNum != objects.length) {
1!
40
      result.add(Arrays.copyOfRange(objects, size * slotNum, objects.length));
1✔
41
    }
42
    return result;
1✔
43
  }
44

45
  public static Boolean[] toObjectArray(boolean[] array) {
46
    if (array == null) {
1✔
47
      return null;
1✔
48
    } else if (array.length == 0) {
1✔
49
      return EMPTY_BOOLEAN_OBJECT_ARRAY;
1✔
50
    }
51
    final Boolean[] result = new Boolean[array.length];
1✔
52
    for (int i = 0; i < array.length; i++) {
1✔
53
      result[i] = Boolean.valueOf(array[i]);
1✔
54
    }
55
    return result;
1✔
56
  }
57

58
  public static Byte[] toObjectArray(byte[] array) {
59
    if (array == null) {
1✔
60
      return null;
1✔
61
    } else if (array.length == 0) {
1✔
62
      return EMPTY_BYTE_OBJECT_ARRAY;
1✔
63
    }
64
    final Byte[] result = new Byte[array.length];
1✔
65
    for (int i = 0; i < array.length; i++) {
1✔
66
      result[i] = Byte.valueOf(array[i]);
1✔
67
    }
68
    return result;
1✔
69
  }
70

71
  public static Character[] toObjectArray(char[] array) {
72
    if (array == null) {
1✔
73
      return null;
1✔
74
    } else if (array.length == 0) {
1✔
75
      return EMPTY_CHARACTER_OBJECT_ARRAY;
1✔
76
    }
77
    final Character[] result = new Character[array.length];
1✔
78
    for (int i = 0; i < array.length; i++) {
1✔
79
      result[i] = Character.valueOf(array[i]);
1✔
80
    }
81
    return result;
1✔
82
  }
83

84
  public static Double[] toObjectArray(double[] array) {
85
    if (array == null) {
1✔
86
      return null;
1✔
87
    } else if (array.length == 0) {
1✔
88
      return EMPTY_DOUBLE_OBJECT_ARRAY;
1✔
89
    }
90
    final Double[] result = new Double[array.length];
1✔
91
    for (int i = 0; i < array.length; i++) {
1✔
92
      result[i] = Double.valueOf(array[i]);
1✔
93
    }
94
    return result;
1✔
95
  }
96

97
  public static Float[] toObjectArray(float[] array) {
98
    if (array == null) {
1✔
99
      return null;
1✔
100
    } else if (array.length == 0) {
1✔
101
      return EMPTY_FLOAT_OBJECT_ARRAY;
1✔
102
    }
103
    final Float[] result = new Float[array.length];
1✔
104
    for (int i = 0; i < array.length; i++) {
1✔
105
      result[i] = Float.valueOf(array[i]);
1✔
106
    }
107
    return result;
1✔
108
  }
109

110
  public static Integer[] toObjectArray(int[] array) {
111
    if (array == null) {
1✔
112
      return null;
1✔
113
    } else if (array.length == 0) {
1✔
114
      return EMPTY_INTEGER_OBJECT_ARRAY;
1✔
115
    }
116
    final Integer[] result = new Integer[array.length];
1✔
117
    for (int i = 0; i < array.length; i++) {
1✔
118
      result[i] = Integer.valueOf(array[i]);
1✔
119
    }
120
    return result;
1✔
121
  }
122

123
  public static Long[] toObjectArray(long[] array) {
124
    if (array == null) {
1✔
125
      return null;
1✔
126
    } else if (array.length == 0) {
1✔
127
      return EMPTY_LONG_OBJECT_ARRAY;
1✔
128
    }
129
    final Long[] result = new Long[array.length];
1✔
130
    for (int i = 0; i < array.length; i++) {
1✔
131
      result[i] = Long.valueOf(array[i]);
1✔
132
    }
133
    return result;
1✔
134
  }
135

136
  public static Short[] toObjectArray(short[] array) {
137
    if (array == null) {
1✔
138
      return null;
1✔
139
    } else if (array.length == 0) {
1✔
140
      return EMPTY_SHORT_OBJECT_ARRAY;
1✔
141
    }
142
    final Short[] result = new Short[array.length];
1✔
143
    for (int i = 0; i < array.length; i++) {
1✔
144
      result[i] = Short.valueOf(array[i]);
1✔
145
    }
146
    return result;
1✔
147
  }
148

149
  /**
150
   * @param <T>
151
   * @param toComponentType
152
   * @param srcArray
153
   * @return
154
   */
155
  public static Object convertSqlArrayToArray(Class<?> toComponentType, java.sql.Array srcArray) {
156
    return convertSqlArrayToArrayAux(toComponentType, srcArray);
1✔
157
  }
158

159
  private static Object convertSqlArrayToArrayAux(Class<?> toComponentType, Object srcArray) {
160
    if (srcArray == null) {
1✔
161
      return null;
1✔
162
    }
163
    if (srcArray instanceof java.sql.Array) {
1✔
164
      try {
165
        java.sql.Array sqlArray = (java.sql.Array) srcArray;
1✔
166
        Object array = sqlArray.getArray();
1✔
167
        return convertSqlArrayToArrayAux(toComponentType, array);
1✔
UNCOV
168
      } catch (SQLException e) {
×
UNCOV
169
        throw Try.rethrow(e);
×
170
      }
171
    }
172
    if (toComponentType.isArray()) {
1✔
173
      int length = java.lang.reflect.Array.getLength(srcArray);
1✔
174
      Class<?> compArrType = toComponentType.getComponentType();
1✔
175
      Object destArray = java.lang.reflect.Array.newInstance(toComponentType, length);
1✔
176
      for (int i = 0; i < length; i++) {
1!
177
        Object v = java.lang.reflect.Array.get(srcArray, i);
1✔
UNCOV
178
        java.lang.reflect.Array.set(destArray, i, convertSqlArrayToArrayAux(compArrType, v));
×
179
      }
180
      return destArray;
×
181
    } else {
182
      int length = java.lang.reflect.Array.getLength(srcArray);
1✔
183
      Object destArray = java.lang.reflect.Array.newInstance(toComponentType, length);
1✔
184
      for (int i = 0; i < length; i++) {
1✔
185
        Object v = java.lang.reflect.Array.get(srcArray, i);
1✔
186
        java.lang.reflect.Array.set(destArray, i, v);
1✔
187
      }
188
      return destArray;
1✔
189
    }
190
  }
191

192
  public static <T> T[] convertToObjectArray(Class<T> componentType, Object srcArray) {
193
    if (srcArray == null) {
1✔
194
      return null;
1✔
195
    }
196
    final int length = Array.getLength(srcArray);
1✔
197
    Object destArray =
198
        Array.newInstance(
1✔
199
            componentType.isPrimitive()
1!
UNCOV
200
                ? ClassUtils.primitiveToWrapper(componentType)
×
201
                : componentType,
1✔
202
            length);
203
    for (int i = 0; i < length; i++) {
1✔
204
      Object v = Array.get(srcArray, i);
1✔
205
      Array.set(destArray, i, v);
1✔
206
    }
207
    @SuppressWarnings("unchecked")
208
    T[] ret = (T[]) destArray;
1✔
209
    return ret;
1✔
210
  }
211

212
  public static Object[] convertToObjectArray(Object srcArray) {
213
    if (srcArray == null) {
1✔
214
      return null;
1✔
215
    }
216
    Class<?> componentType = srcArray.getClass().getComponentType();
1✔
217
    if (componentType == null) {
1✔
218
      throw new IllegalArgumentException("the argument could not be convert to an object array.");
1✔
219
    }
220
    if (!componentType.isArray()) {
1✔
221
      switch (srcArray.getClass().getComponentType().toString()) {
1!
222
        case "boolean":
223
          return toObjectArray((boolean[]) srcArray);
1✔
224
        case "byte":
225
          return toObjectArray((byte[]) srcArray);
1✔
226
        case "char":
227
          return toObjectArray((char[]) srcArray);
1✔
228
        case "short":
229
          return toObjectArray((short[]) srcArray);
1✔
230
        case "int":
231
          return toObjectArray((int[]) srcArray);
1✔
232
        case "long":
233
          return toObjectArray((long[]) srcArray);
1✔
234
        case "float":
235
          return toObjectArray((float[]) srcArray);
1✔
236
        case "double":
237
          return toObjectArray((double[]) srcArray);
1✔
238
        default:
UNCOV
239
          return (Object[]) srcArray;
×
240
      }
241
    }
242
    Object o = Array.get(srcArray, 0);
1✔
243
    final int length = Array.getLength(srcArray);
1✔
244
    Object destArray =
1✔
245
        Array.newInstance(
1✔
246
            Array.newInstance(convertToObjectArray(o).getClass().getComponentType(), 0).getClass(),
1✔
247
            length);
248
    for (int i = 0; i < length; i++) {
1✔
249
      Object v = Array.get(srcArray, i);
1✔
250
      Array.set(destArray, i, convertToObjectArray(v));
1✔
251
    }
252
    return (Object[]) destArray;
1✔
253
  }
254

255
  public static Class<?> getInternalComponentType(Class<?> compType) {
256
    Class<?> ret = compType;
1✔
257
    while (ret.isArray()) {
1✔
258
      ret = ret.getComponentType();
1✔
259
    }
260
    return ret;
1✔
261
  }
262
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc