Skip to content

Commit c88bc78

Browse files
committed
CFUtil varargs utils
1 parent c11ca37 commit c88bc78

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

src/main/java/com/trivago/fastutilconcurrentwrapper/util/CFUtil.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.trivago.fastutilconcurrentwrapper.util;
22

33
import it.unimi.dsi.fastutil.HashCommon;
4+
import it.unimi.dsi.fastutil.objects.ObjectArrays;
45
import jakarta.validation.constraints.Positive;
56
import jakarta.validation.constraints.PositiveOrZero;
67
import lombok.AccessLevel;
@@ -75,4 +76,27 @@ Fast. Safe for negative keys (including Long.MIN_VALUE, Integer.MIN_VALUE)
7576
public static long compoundKey (int keyHi, int keyLo) {
7677
return ((long)keyHi << 32) | (keyLo & 0xFfFf_FfFfL);
7778
}
79+
80+
/**
81+
Is varargs empty?
82+
@see #safeVarArgs
83+
*/
84+
public static boolean blankVarargs (@Nullable Object @Nullable [] args) {
85+
return args == null
86+
|| args.length == 0
87+
|| (args.length == 1 && args[0] == null);
88+
}
89+
90+
/// Fix usual varargs mistakes (but type is lost)
91+
/// ~ Considers an Object array passed into a varargs parameter as collection of arguments rather than as single argument).
92+
/// @see #blankVarargs
93+
public static Object @Nullable[] safeVarArgs (@Nullable Object @Nullable[] varArgs) {
94+
if (varArgs == null)
95+
return ObjectArrays.EMPTY_ARRAY;// not some T[]!
96+
97+
if (varArgs.length == 1 && varArgs[0] instanceof Object[] a)// antT[] instanceof Object[]!
98+
return a;
99+
100+
return varArgs;// usual good varargs
101+
}
78102
}

src/test/java/com/trivago/fastutilconcurrentwrapper/intkey/PrimitiveConcurrentMapTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.trivago.fastutilconcurrentwrapper.PrimitiveConcurrentMap;
44
import com.trivago.fastutilconcurrentwrapper.util.CFUtil;
5+
import org.junit.jupiter.api.Disabled;
56
import org.junit.jupiter.api.Test;
67

78
import java.util.concurrent.ThreadLocalRandom;
@@ -43,7 +44,7 @@ void _hashLong () {
4344
assertEquals(74720, CFUtil.bucket(Long.MAX_VALUE, 100_000));
4445
}
4546

46-
@Test
47+
@Test @Disabled
4748
void longsAreSame () {
4849
long total = 0, t = System.nanoTime();
4950
for (long i = Integer.MIN_VALUE; i <= Integer.MAX_VALUE; i++){
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.trivago.fastutilconcurrentwrapper.util;
2+
3+
import it.unimi.dsi.fastutil.objects.ObjectArrays;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
/**
9+
@see CFUtil */
10+
class CFUtilTest {
11+
@Test
12+
void _hash () {
13+
assertEquals(1832674720, CFUtil.hash(Integer.MIN_VALUE));
14+
assertEquals(-2114883783, CFUtil.hash(-1));
15+
assertEquals(0, CFUtil.hash(0));
16+
assertEquals(1364076727, CFUtil.hash(1));
17+
assertEquals(-104067416, CFUtil.hash(Integer.MAX_VALUE));
18+
}
19+
20+
@Test
21+
void _blankVarargs () {
22+
assertTrue(CFUtil.blankVarargs(null));
23+
assertTrue(CFUtil.blankVarargs((Object[])null));
24+
assertTrue(CFUtil.blankVarargs(ObjectArrays.EMPTY_ARRAY));
25+
assertTrue(CFUtil.blankVarargs(new String[]{null}));
26+
assertTrue(CFUtil.blankVarargs(new Object[]{null}));
27+
28+
assertFalse(CFUtil.blankVarargs(new Object[]{null,null}));
29+
assertFalse(CFUtil.blankVarargs(new Integer[]{1}));
30+
}
31+
32+
@Test
33+
void _safeVarArgs () {
34+
Object[] a = null;
35+
36+
assertSame(ObjectArrays.EMPTY_ARRAY, CFUtil.safeVarArgs(null));
37+
assertSame(ObjectArrays.EMPTY_ARRAY, CFUtil.safeVarArgs(a));
38+
assertSame(ObjectArrays.EMPTY_ARRAY, CFUtil.safeVarArgs(ObjectArrays.EMPTY_ARRAY));
39+
40+
a = new Object[]{null};
41+
assertSame(a, CFUtil.safeVarArgs(a));
42+
a = new Integer[]{1};
43+
assertSame(a, CFUtil.safeVarArgs(a));
44+
45+
a = new Integer[0];
46+
assertSame(a, CFUtil.safeVarArgs(new Object[]{a}));
47+
}
48+
}

0 commit comments

Comments
 (0)