MCPcopy Index your code
hub / github.com/bazelbuild/bazel / copyOf

Method copyOf

src/main/java/net/starlark/java/eval/Dict.java:456–492  ·  view source on GitHub ↗

Returns a new dict with the specified mutability containing the entries of m.

(@Nullable Mutability mu, Map<? extends K, ? extends V> m)

Source from the content-addressed store, hash-verified

454
455 /** Returns a new dict with the specified mutability containing the entries of {@code m}. */
456 public static <K, V> Dict<K, V> copyOf(@Nullable Mutability mu, Map<? extends K, ? extends V> m) {
457 if (mu == null) {
458 mu = Mutability.IMMUTABLE;
459 }
460
461 if (mu == Mutability.IMMUTABLE) {
462 if (m.isEmpty()) {
463 return empty();
464 }
465
466 if (m instanceof ImmutableMap) {
467 m.forEach(
468 (k, v) -> {
469 Starlark.checkValid(k);
470 Starlark.checkValid(v);
471 });
472 @SuppressWarnings("unchecked")
473 ImmutableMap<K, V> immutableMap = (ImmutableMap<K, V>) m;
474 return new Dict<>(immutableMap);
475 }
476
477 if (m instanceof Dict && ((Dict<?, ?>) m).isImmutable()) {
478 @SuppressWarnings("unchecked")
479 Dict<K, V> dict = (Dict<K, V>) m;
480 return dict;
481 }
482
483 ImmutableMap.Builder<K, V> immutableMapBuilder =
484 ImmutableMap.builderWithExpectedSize(m.size());
485 m.forEach((k, v) -> immutableMapBuilder.put(Starlark.checkValid(k), Starlark.checkValid(v)));
486 return new Dict<>(immutableMapBuilder.buildOrThrow());
487 } else {
488 LinkedHashMap<K, V> linkedHashMap = Maps.newLinkedHashMapWithExpectedSize(m.size());
489 m.forEach((k, v) -> linkedHashMap.put(Starlark.checkValid(k), Starlark.checkValid(v)));
490 return new Dict<>(mu, linkedHashMap);
491 }
492 }
493
494 /** Returns an immutable dict containing the entries of {@code m}. */
495 public static <K, V> Dict<K, V> immutableCopyOf(Map<? extends K, ? extends V> m) {

Callers 15

fromJavaMethod · 0.95
immutableCopyOfMethod · 0.95
copyMethod · 0.45
copyMethod · 0.45
ensureLineCapacityMethod · 0.45
DocstringInfoMethod · 0.45
ParameterDocMethod · 0.45
DocstringParserMethod · 0.45
structMethod · 0.45
getFieldNamesMethod · 0.45

Calls 6

emptyMethod · 0.95
checkValidMethod · 0.95
isImmutableMethod · 0.65
isEmptyMethod · 0.45
sizeMethod · 0.45
putMethod · 0.45

Tested by 8

copyMethod · 0.36
copyMethod · 0.36
ensureLineCapacityMethod · 0.36
structMethod · 0.36
getFieldNamesMethod · 0.36
addEqualityGroupMethod · 0.36