← Back to Pull requests

Remove Map.computeIfAbsent and computeIfPresent #278

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 04, 2026 01:29 UTC
Author

Ticket

Closes #273
Tracker: #247

What changed and why

Before, Map, SortedMap, HashMap, LinkedHashMap, TreeMap, NonEmptyMap and NonEmptySortedMap had
computeIfAbsent(K, Function) and computeIfPresent(K, BiFunction), returning a Tuple2 of the value and the map.
updateWith (#274) covers both, so, as the maintainer decided on 2026-10-04 in the issue, they are removed, not
deprecated: Zazr makes no compatibility promise before 1.0, and Scala 2.13's immutable maps have neither.

After:

  • The 14 declarations are gone, with Maps.computeIfAbsent and Maps.computeIfPresent, which nothing else called.

  • The class javadoc of the two non-empty maps no longer lists them.
  • The java.util.Map computeIfAbsent/computeIfPresent of the asJava() views (MapViews) stay: java.util.Map

  • declares them, and they throw UnsupportedOperationException as every mutator of the views.
  • Call sites: there were none in main code. zazr-test, zazr-jackson, zazr-avaje-jsonb, the benchmarks, the docs

  • examples and the skill examples had none either (git grep computeIf). The only callers were the tests of the
    removed methods.

    Release notes (0.4.0)

    Map.computeIfAbsent and Map.computeIfPresent are removed from every Zazr map; use updateWith:

  • m.computeIfAbsent(k, f) → m.get(k) / m.updateWith(k, o -> o.orElse(() -> Option.some(f.apply(k))));

  • for the value alone, m.getOrElse(k, () -> f.apply(k))
  • m.computeIfPresent(k, f) → m.updateWith(k, o -> o.map(v -> f.apply(k, v)))

  • On NonEmptyMap and NonEmptySortedMap, updateWith returns the plain map; to keep the non-empty type:

  • m.containsKey(k) ? m : m.put(k, f.apply(k)) and m.get(k).fold(() -> m, v -> m.put(k, f.apply(k, v)))

    Each line was compiled against zazr-core/target/classes in a generic method taking the old parameter types
    (Function<? super K, ? extends V>, BiFunction<? super K, ? super V, ? extends V>) on Map, NonEmptyMap and
    NonEmptySortedMap, and run:

    m = HashMap.of("a", 1), f = String::length, g = (k, v) -> v + 10
    m.updateWith("bb", o -> o.orElse(() -> Option.some(f.apply("bb"))))  // HashMap((a, 1), (bb, 2))
    m.updateWith("a",  o -> o.orElse(() -> Option.some(f.apply("a"))))   // HashMap((a, 1))
    m.getOrElse("bb", () -> f.apply("bb"))                               // 2
    m.updateWith("a", o -> o.map(v -> g.apply("a", v)))                  // HashMap((a, 11))
    m.updateWith("z", o -> o.map(v -> g.apply("z", v))) == m             // true
    n = NonEmptyMap.of(Tuple.of("a", 1))
    n.get("a").fold(() -> n, v -> n.put("a", g.apply("a", v)))           // NonEmptyMap((a, 11))
    n.containsKey("bb") ? n : n.put("bb", f.apply("bb"))                 // NonEmptyMap((a, 1), (bb, 2))
    TreeMap.of("a", 1).updateWith("a", o -> o.map(v -> g.apply("a", v))) // TreeMap((a, 11))
    

    Decisions beyond the ticket

    1. The non-empty release-note line. The issue's replacements return HashMap/TreeMap on the non-empty maps,
    where the removed methods returned the non-empty type, so a migrating caller gets a type error. I added a
    replacement that keeps the type (above, and in the design row). Alternative: leave it out; rejected, since the
    notes would then not compile for those callers.
    2. euler/Memoize.java is unchanged. Its computeIfAbsent is java.util.concurrent.ConcurrentHashMap's, not
    Zazr's, so the removal does not touch it, and on a hit it already does one lookup. The second memoizer in the file
    uses a JDK HashMap with get/put. Rewriting it with a Zazr map would change a test helper for no reason.
    3. The ABSENT sentinel tests are rewritten, not dropped. HashMapTest, LinkedHashMapTest and TreeMapTest
    had computeIfAbsentOnAbsentKeyComputes and computeIfPresentOnAbsentKeyIsNoop in AbsentSentinelTests, the
    guard that the sentinel never leaks. They become updateWithOnAbsentKeyHandsNoneToF,
    updateWithRemovingAnAbsentKeyGivesTheMapItself and getOrElseWithASupplierOnAbsentKeyCallsIt, the same guard
    over the methods that now read the sentinel.
    4. docs/design.md 3.6, 3.7 and 3.9 listed the methods (the non-empty "returns the non-empty type" list, what
    Map keeps, and the null rules). The first two now say they were removed on 2026-10-04; in 3.9 the null-argument
    example is now updateWith, which also rejects a null f on a missing key.
    5. Two test methods were renamed now that they test no compute: NonEmptyMapTest.shouldMergeAndCompute →
    shouldMerge, NonEmptySortedMapTest.shouldPutMergeAndComputeKeepingTheComparator →
    shouldPutAndMergeKeepingTheComparator.

    Tests

  • Removed: the ComputeifabsentTests/ComputeIfPresentTests nested classes and the two null-result tests in

  • GetWithNullsTests of HashMapTest, LinkedHashMapTest and TreeMapTest; the two compute
    null-argument tests
    of MapFunctionArgumentTest; the compute assertions and NonEmptyGuarantee rows of NonEmptyMapTest and
    NonEmptySortedMapTest (the reflective guard there checks the rows match the public methods, so it confirms the
    removal).
  • The behaviour those tests covered that updateWith keeps is tested by MapUpdateWithTest (#274): put on an absent

  • key, the map itself when nothing changes, a null result rejected by name (shouldRejectANullResultNamingTheType),
    on all five maps; and by MapFunctionArgumentTest.shouldRejectANullFunctionInUpdateWith for a null function,
    key present or absent, map empty or not.
  • Rewritten: the AbsentSentinelTests above, on the three maps.
  • Mutation check

    One at a time, on the committed tree, each reverted with git checkout -- <file>:

    <details><summary>(a) <code>Maps.updateWith</code> hands <code>Some(ABSENT)</code> to <code>f</code> for an absent key</summary>

    -        return updateWith(map, key, current == ABSENT ? Option.none() : Option.some(current), current, f, nullResult);
    +        return updateWith(map, key, Option.some(current), current, f, nullResult);
    

    </details>

    Failed: HashMapTest$AbsentSentinelTests.updateWithOnAbsentKeyHandsNoneToF,
    LinkedHashMapTest$AbsentSentinelTests.updateWithOnAbsentKeyHandsNoneToF.

    <details><summary>(b) <code>Maps.getOrElse(map, key, supplier, …)</code> skips the <code>ABSENT</code> check</summary>

    -        return value != ABSENT ? value : Objects.requireNonNull(supplier.get(), nullResult);
    +        return value;
    

    </details>

    Failed: HashMapTest$AbsentSentinelTests.getOrElseWithASupplierOnAbsentKeyCallsIt,
    LinkedHashMapTest$AbsentSentinelTests.getOrElseWithASupplierOnAbsentKeyCallsIt (ClassCastException).

    <details><summary>(c) <code>TreeMap.updateWith</code> hands <code>Some(ABSENT)</code> to <code>f</code></summary>

    -        return Maps.updateWith(this, key, previous, current, f, nullResult);
    +        return Maps.updateWith(this, key, Option.some(current), current, f, nullResult);
    

    </details>

    Failed: TreeMapTest$AbsentSentinelTests.updateWithOnAbsentKeyHandsNoneToF.

    Checks run

  • make verify on 8f9c28b1a: green (zazr-core 13896 tests, 0 failures). 1b1015542 only changes docs/design.md.

  • make site on 8f9c28b1a: built, no warnings.

  • make docs-complexity regenerated docs/collections/complexity.md (the six compute rows go).
  • Docs

  • docs/design.md: row 3.3 of updateWith closes the open question; a new 3.3 row records the removal (decided by

  • the maintainer 2026-10-04, #273) with the replacements, the reasons and the two-lookup trade-off; 3.6, 3.7 and 3.9
    updated (above).
  • docs/vavr.md (the migration guide): a "Removed" row, computeIfAbsent, computeIfPresent → updateWith, the

  • lazy getOrElse.
  • skills/zazr/references/from-vavr.md: the same row with the replacement lambdas. The other skill references and

  • docs/collections/maps.md never mentioned the methods; they already teach updateWith and the lazy getOrElse.
  • docs/collections/complexity.md: regenerated.

  • What git grep -n computeIf still finds, and why each stays:

  • - docs/design.md: the decision log recording the removal.
    - docs/vavr.md, skills/zazr/references/from-vavr.md: the migration rows naming the Vavr method.
    - internal/MapViews.java, JavaViewMutatorTest: the java.util.Map methods of the asJava() views, kept.
    - internal/Collections.java: java.util.LinkedHashMap.computeIfAbsent on a JDK map, in groupBy/groupMap.
    - euler/Memoize.java: ConcurrentHashMap.computeIfAbsent and a comment about it (decision 2).

    Found but not fixed

    None.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: copilot-pull-request-reviewer[bot] Review duration: 1m
    All Vouch review confidence checks passed
    Residual risk 0.46 = Change Risk (0.52) × (1 − Review Confidence (0.12)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 10% · Reviewer familiarity: 35%
    11%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #278"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 11.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 11.7% · Lower = rushed or shallow review
    11.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 45.9% · Moderate risk
    45.9%