@@ -823,6 +823,41 @@ public interface MAP KEY_VALUE_GENERIC extends FUNCTION KEY_VALUE_GENERIC, Map<K
823
823
824
824
#endif
825
825
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
+
826
861
/** A type-specific {@link java.util.Map.Entry}; provides some additional methods
827
862
* that use polymorphism to avoid (un)boxing.
828
863
*
0 commit comments