< prev index next >

src/java.base/share/classes/java/lang/ScopedValue.java

Print this page

595             }
596         }
597         var value = findBinding();
598         boolean result = (value != Snapshot.NIL);
599         if (result)  Cache.put(this, value);
600         return result;
601     }
602 
603     /**
604      * Return the value of the scoped value or NIL if not bound.
605      */
606     private Object findBinding() {
607         Object value = scopedValueBindings().find(this);
608         return value;
609     }
610 
611     /**
612      * Returns the value of this scoped value if bound in the current thread, otherwise
613      * returns {@code other}.
614      *
615      * @param other the value to return if not bound, can be {@code null}
616      * @return the value of the scoped value if bound, otherwise {@code other}
617      */
618     public T orElse(T other) {

619         Object obj = findBinding();
620         if (obj != Snapshot.NIL) {
621             @SuppressWarnings("unchecked")
622             T value = (T) obj;
623             return value;
624         } else {
625             return other;
626         }
627     }
628 
629     /**
630      * Returns the value of this scoped value if bound in the current thread, otherwise
631      * throws an exception produced by the exception supplying function.
632      *
633      * @param <X> the type of the exception that may be thrown
634      * @param exceptionSupplier the supplying function that produces the exception to throw
635      * @return the value of the scoped value if bound in the current thread
636      * @throws X if the scoped value is not bound in the current thread
637      */
638     public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {

595             }
596         }
597         var value = findBinding();
598         boolean result = (value != Snapshot.NIL);
599         if (result)  Cache.put(this, value);
600         return result;
601     }
602 
603     /**
604      * Return the value of the scoped value or NIL if not bound.
605      */
606     private Object findBinding() {
607         Object value = scopedValueBindings().find(this);
608         return value;
609     }
610 
611     /**
612      * Returns the value of this scoped value if bound in the current thread, otherwise
613      * returns {@code other}.
614      *
615      * @param other the value to return if not bound
616      * @return the value of the scoped value if bound, otherwise {@code other}
617      */
618     public T orElse(T other) {
619         Objects.requireNonNull(other);
620         Object obj = findBinding();
621         if (obj != Snapshot.NIL) {
622             @SuppressWarnings("unchecked")
623             T value = (T) obj;
624             return value;
625         } else {
626             return other;
627         }
628     }
629 
630     /**
631      * Returns the value of this scoped value if bound in the current thread, otherwise
632      * throws an exception produced by the exception supplying function.
633      *
634      * @param <X> the type of the exception that may be thrown
635      * @param exceptionSupplier the supplying function that produces the exception to throw
636      * @return the value of the scoped value if bound in the current thread
637      * @throws X if the scoped value is not bound in the current thread
638      */
639     public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
< prev index next >