Skip to content

Commit

Permalink
#10 Use java Arrays.toString() when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
doom369 committed Aug 23, 2019
1 parent 5729d6a commit b73c881
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 59 deletions.
62 changes: 7 additions & 55 deletions src/main/java/ru/yandex/clickhouse/util/ClickHouseArrayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ru.yandex.clickhouse.ClickHouseUtil;

import java.util.Arrays;
import java.util.Collection;

/**
Expand Down Expand Up @@ -34,73 +35,24 @@ public static String arrayToString(Object object, Boolean explicitEscape) {

private static String primitiveArrayToString(Object array) {
if (array instanceof int[]) {
return toString((int[]) array);
return Arrays.toString((int[]) array);
} else if (array instanceof long[]) {
return toString((long[]) array);
return Arrays.toString((long[]) array);
} else if (array instanceof float[]) {
return toString((float[]) array);
return Arrays.toString((float[]) array);
} else if (array instanceof double[]) {
return toString((double[]) array);
return Arrays.toString((double[]) array);
} else if (array instanceof char[]) {
return toString((char[]) array);
} else if (array instanceof byte[]) {
return toString((byte[]) array);
return Arrays.toString((byte[]) array);
} else if (array instanceof short[]) {
return toString((short[]) array);
return Arrays.toString((short[]) array);
} else {
throw new IllegalArgumentException("Wrong primitive type: " + array.getClass().getComponentType());
}
}

public static String toString(int[] values) {
ArrayBuilder builder = new ArrayBuilder(false, true);
for (int value : values) {
builder.append(value);
}
return builder.build();
}

public static String toString(long[] values) {
ArrayBuilder builder = new ArrayBuilder(false, true);
for (long value : values) {
builder.append(value);
}
return builder.build();
}

public static String toString(float[] values) {
ArrayBuilder builder = new ArrayBuilder(false, true);
for (float value : values) {
builder.append(value);
}
return builder.build();
}

public static String toString(double[] values) {
ArrayBuilder builder = new ArrayBuilder(false, true);
for (double value : values) {
builder.append(value);
}
return builder.build();
}

public static String toString(byte[] values) {
ArrayBuilder builder = new ArrayBuilder(false, true);
for (byte value : values) {
builder.append(value);
}
return builder.build();
}

public static String toString(short[] values) {
ArrayBuilder builder = new ArrayBuilder(false, true);
for (short value : values) {
builder.append(value);
}
return builder.build();
}


public static String toString(char[] values) {
ArrayBuilder builder = new ArrayBuilder(true, true);
for (char value : values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public void testArrayToString() {

Assert.assertEquals(
ClickHouseArrayUtil.arrayToString(new int[]{21, 42}),
"[21,42]"
"[21, 42]"
);

Assert.assertEquals(
ClickHouseArrayUtil.arrayToString(new double[]{0.1, 1.2}),
"[0.1,1.2]"
"[0.1, 1.2]"
);

Assert.assertEquals(
Expand All @@ -68,19 +68,24 @@ public void testArrayToString() {

Assert.assertEquals(
ClickHouseArrayUtil.arrayToString(new double[][]{{0.1, 1.2}, {0.2, 2.2}}),
"[[0.1,1.2],[0.2,2.2]]"
"[[0.1, 1.2],[0.2, 2.2]]"
);

Assert.assertEquals(
ClickHouseArrayUtil.arrayToString(new int[][]{{1, 2}, {3, 4}}),
"[[1,2],[3,4]]"
"[[1, 2],[3, 4]]"
);

Assert.assertEquals(
ClickHouseArrayUtil.arrayToString(new char[][]{{'a', 'b'}, {'c', 'd'}}),
"[['a','b'],['c','d']]"
);

Assert.assertEquals(ClickHouseArrayUtil.arrayToString(new short[] { 1,2,3 }), "[1, 2, 3]");
Assert.assertEquals(ClickHouseArrayUtil.arrayToString(new float[] { 1.2f, 2.3f, 3.4f }), "[1.2, 2.3, 3.4]");
Assert.assertEquals(ClickHouseArrayUtil.arrayToString(new byte[] {1, 2}), "[1, 2]");


}

@Test
Expand Down

0 comments on commit b73c881

Please sign in to comment.