jsedding commented on PR #2457:
URL: https://github.com/apache/jackrabbit-oak/pull/2457#issuecomment-3204673616

   Just for fun, this implementation would be `null`-safe. The trick is to use 
the `ref` object as a `null`-marker.
   
   However, it might lead to `computeOnce` getting called more than once when 
called concurrently. It may be possible to add a guard against this, but I 
don't think it's worthwhile.
   
   ```
   
       public static <T> Supplier<@Nullable T> memoize(final Supplier<@Nullable 
T> computeOnce) {
           return new Supplier<>() {
               final AtomicReference<Object> ref = new 
AtomicReference<>(computeOnce);
   
               @Override
               @SuppressWarnings( "unchecked")
               public @Nullable T get() {
                   Object o = ref.updateAndGet(prev -> 
Objects.requireNonNullElseGet(
                           prev,
                           () -> 
Optional.<Object>ofNullable(computeOnce.get()).orElse(ref)
                   ));
                   return o == ref ? null : (T) o;
               }
           };
       }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: oak-dev-unsubscr...@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to