Skip to content

Commit 8a80c92

Browse files
committed
implement #ofEntries and #entry
1 parent 5eaffbb commit 8a80c92

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

drv/Map.drv

+35
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,41 @@ public interface MAP KEY_VALUE_GENERIC extends FUNCTION KEY_VALUE_GENERIC, Map<K
823823

824824
#endif
825825

826+
/** Returns an unmodifiable, type-specific map with the specified entries.
827+
* See {@link java.util.Map#ofEntries(Map.Entry[])} */
828+
#if !KEYS_PRIMITIVE || !VALUES_PRIMITIVE
829+
@SafeVarargs
830+
@SuppressWarnings({"varargs", "unchecked"})
831+
#else
832+
@SuppressWarnings("varargs")
833+
#endif
834+
static KEY_VALUE_GENERIC MAP KEY_VALUE_GENERIC ofEntries(MAP.Entry KEY_VALUE_GENERIC ... entries) {
835+
if (entries.length == 0) return MAPS.EMPTY_MAP;
836+
if (entries.length == 1) return MAPS.singleton(entries[0].ENTRY_GET_KEY(), entries[0].ENTRY_GET_VALUE());
837+
838+
KEY_GENERIC_TYPE[] keys = KEY_GENERIC_ARRAY_CAST new KEY_TYPE[entries.length];
839+
VALUE_GENERIC_TYPE[] vals = VALUE_GENERIC_ARRAY_CAST new VALUE_TYPE[entries.length];
840+
841+
for (int i = 0; i < entries.length; ++i) {
842+
MAP.Entry KEY_VALUE_GENERIC entry = entries[i];
843+
keys[i] = entry.ENTRY_GET_KEY();
844+
// Prevent duplicate keys
845+
for (int j = 0; j < i; ++j) if (KEY_EQUALS(keys[j], keys[i])) throw new IllegalArgumentException("duplicate key: " + keys[i]);
846+
vals[i] = entry.ENTRY_GET_VALUE();
847+
}
848+
if (entries.length <= 8) {
849+
return MAPS.unmodifiable(new ARRAY_MAP KEY_VALUE_GENERIC_DIAMOND(keys, vals, entries.length));
850+
} else {
851+
return MAPS.unmodifiable(new OPEN_HASH_MAP KEY_VALUE_GENERIC_DIAMOND(keys, vals, 0.75f));
852+
}
853+
}
854+
855+
/** Returns an unmodifiable, type-specific entry.
856+
* See {@link java.util.Map#entry(Object, Object)} */
857+
static KEY_VALUE_GENERIC Entry KEY_VALUE_GENERIC entry(KEY_GENERIC_TYPE key, VALUE_GENERIC_TYPE value) {
858+
return new ABSTRACT_MAP.BasicEntry KEY_VALUE_GENERIC_DIAMOND(key, value);
859+
}
860+
826861
/** A type-specific {@link java.util.Map.Entry}; provides some additional methods
827862
* that use polymorphism to avoid (un)boxing.
828863
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package it.unimi.dsi.fastutil.objects;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertThrows;
6+
import static org.junit.Assert.assertTrue;
7+
8+
public class Object2ObjectMapTest {
9+
10+
@Test
11+
public void testSmallMaps() {
12+
assertTrue(Object2ObjectMap.ofEntries() instanceof Object2ObjectMaps.EmptyMap);
13+
assertTrue(Object2ObjectMap.ofEntries(Object2ObjectMap.entry(new Object(), new Object())) instanceof Object2ObjectMaps.Singleton);
14+
}
15+
16+
@Test
17+
public void testThrowOnDuplicate() {
18+
assertThrows(IllegalArgumentException.class, () -> Object2ObjectMap.ofEntries(
19+
Object2ObjectMap.entry("dupe", "foo"),
20+
Object2ObjectMap.entry("not a dupe", "bar"),
21+
Object2ObjectMap.entry("dupe", "exception")
22+
)
23+
);
24+
}
25+
26+
27+
}

0 commit comments

Comments
 (0)