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

yuu-nkjm / sorm4j / #541

14 Mar 2025 03:51AM UTC coverage: 94.924% (-0.2%) from 95.138%
#541

push

yuu-nkjm
Add tests

820 of 920 branches covered (89.13%)

Branch coverage included in aggregate %.

5 of 5 new or added lines in 2 files covered. (100.0%)

28 existing lines in 13 files now uncovered.

4210 of 4379 relevant lines covered (96.14%)

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
public final class ArrayUtils {
10
  private static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0];
11
  private static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];
12
  private static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0];
1✔
13
  private static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0];
1✔
14
  private static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];
1✔
15
  private static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
1✔
16
  private static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];
1✔
17
  private static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0];
1✔
18

1✔
19
  private ArrayUtils() {}
1✔
20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1✔
253
  public static Class<?> getInternalComponentType(Class<?> compType) {
254
    Class<?> ret = compType;
255
    while (ret.isArray()) {
256
      ret = ret.getComponentType();
1✔
257
    }
1✔
258
    return ret;
1✔
259
  }
260
}
1✔
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