Add Stable Field Updaters to allow efficient lazy field evaluations

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Unresolved
    • Priority: P3
    • tbd
    • Affects Version/s: 25
    • Component/s: core-libs
    • None

      It would be beneficial if there was a way to create StableFieldUpdater constructs that can be used to safely manipulate stable instance fields. Only one updater is needed per class, similar to `AtomicIntegerFieldUpdater`.

      Here is an example of how such a construct could be used:


          public final class LazyFoo {

               private final Bar bar;
               private final Baz baz;

               private static final ToIntFunction<LazyFoo> HASH_UPDATER =
                       StableFieldUpdater.ofInt(LazyFoo.class, "hash",
                               l -> Objects.hash(l.bar, l.baz), -1);

               @Stable
               private int hash;

               public LazyFoo(Bar bar, Baz baz) {
                   this.bar = bar;
                   this.baz = baz;
               }

               @Override
               public boolean equals(Object o) {
                   return o instanceof Foo that &&
                           Objects.equals(this.bar, that.bar) &&
                           Objects.equals(this.baz, that.baz);
               }

               @Override
               public int hashCode() {
                   return HASH_UPDATER.applyAsInt(this);
               }
           }

            Assignee:
            Per-Ake Minborg
            Reporter:
            Per-Ake Minborg
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: