Returns a new dict with the specified mutability containing the entries of m.
(@Nullable Mutability mu, Map<? extends K, ? extends V> m)
| 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) { |