120 }
121
122 /**
123 * Returns an {@code Optional} describing the given value, if
124 * non-{@code null}, otherwise returns an empty {@code Optional}.
125 *
126 * @param value the possibly-{@code null} value to describe
127 * @param <T> the type of the value
128 * @return an {@code Optional} with a present value if the specified value
129 * is non-{@code null}, otherwise an empty {@code Optional}
130 */
131 public static <T> Optional<T> ofNullable(T value) {
132 return value == null ? empty() : of(value);
133 }
134
135 /**
136 * If a value is present, returns the value, otherwise throws
137 * {@code NoSuchElementException}.
138 *
139 * @apiNote
140 * The methods {@link #orElse(Object) orElse} and
141 * {@link #orElseGet(Supplier) orElseGet}
142 * are generally preferable to this method, as they return a substitute
143 * value if the value is absent, instead of throwing an exception.
144 *
145 * @return the non-{@code null} value described by this {@code Optional}
146 * @throws NoSuchElementException if no value is present
147 * @see Optional#isPresent()
148 */
149 public T get() {
150 if (value == null) {
151 throw new NoSuchElementException("No value present");
152 }
153 return value;
154 }
155
156 /**
157 * If a value is present, returns {@code true}, otherwise {@code false}.
158 *
159 * @return {@code true} if a value is present, otherwise {@code false}
160 */
161 public boolean isPresent() {
162 return value != null;
163 }
164
165 /**
166 * If a value is present, performs the given action with the value,
167 * otherwise does nothing.
345 */
346 public T orElse(T other) {
347 return value != null ? value : other;
348 }
349
350 /**
351 * If a value is present, returns the value, otherwise returns the result
352 * produced by the supplying function.
353 *
354 * @param supplier the supplying function that produces a value to be returned
355 * @return the value, if present, otherwise the result produced by the
356 * supplying function
357 * @throws NullPointerException if no value is present and the supplying
358 * function is {@code null}
359 */
360 public T orElseGet(Supplier<? extends T> supplier) {
361 return value != null ? value : supplier.get();
362 }
363
364 /**
365 * If a value is present, returns the value, otherwise throws an exception
366 * produced by the exception supplying function.
367 *
368 * @apiNote
369 * A method reference to the exception constructor with an empty argument
370 * list can be used as the supplier. For example,
371 * {@code IllegalStateException::new}
372 *
373 * @param <X> Type of the exception to be thrown
374 * @param exceptionSupplier the supplying function that produces an
375 * exception to be thrown
376 * @return the value, if present
377 * @throws X if no value is present
378 * @throws NullPointerException if no value is present and the exception
379 * supplying function is {@code null}
380 */
381 public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
382 if (value != null) {
383 return value;
384 } else {
|
120 }
121
122 /**
123 * Returns an {@code Optional} describing the given value, if
124 * non-{@code null}, otherwise returns an empty {@code Optional}.
125 *
126 * @param value the possibly-{@code null} value to describe
127 * @param <T> the type of the value
128 * @return an {@code Optional} with a present value if the specified value
129 * is non-{@code null}, otherwise an empty {@code Optional}
130 */
131 public static <T> Optional<T> ofNullable(T value) {
132 return value == null ? empty() : of(value);
133 }
134
135 /**
136 * If a value is present, returns the value, otherwise throws
137 * {@code NoSuchElementException}.
138 *
139 * @apiNote
140 * The preferred alternative to this method is {@link #orElseThrow()}.
141 *
142 * @return the non-{@code null} value described by this {@code Optional}
143 * @throws NoSuchElementException if no value is present
144 */
145 public T get() {
146 if (value == null) {
147 throw new NoSuchElementException("No value present");
148 }
149 return value;
150 }
151
152 /**
153 * If a value is present, returns {@code true}, otherwise {@code false}.
154 *
155 * @return {@code true} if a value is present, otherwise {@code false}
156 */
157 public boolean isPresent() {
158 return value != null;
159 }
160
161 /**
162 * If a value is present, performs the given action with the value,
163 * otherwise does nothing.
341 */
342 public T orElse(T other) {
343 return value != null ? value : other;
344 }
345
346 /**
347 * If a value is present, returns the value, otherwise returns the result
348 * produced by the supplying function.
349 *
350 * @param supplier the supplying function that produces a value to be returned
351 * @return the value, if present, otherwise the result produced by the
352 * supplying function
353 * @throws NullPointerException if no value is present and the supplying
354 * function is {@code null}
355 */
356 public T orElseGet(Supplier<? extends T> supplier) {
357 return value != null ? value : supplier.get();
358 }
359
360 /**
361 * If a value is present, returns the value, otherwise throws
362 * {@code NoSuchElementException}.
363 *
364 * @return the non-{@code null} value described by this {@code Optional}
365 * @throws NoSuchElementException if no value is present
366 * @since 10
367 */
368 public T orElseThrow() {
369 if (value == null) {
370 throw new NoSuchElementException("No value present");
371 }
372 return value;
373 }
374
375 /**
376 * If a value is present, returns the value, otherwise throws an exception
377 * produced by the exception supplying function.
378 *
379 * @apiNote
380 * A method reference to the exception constructor with an empty argument
381 * list can be used as the supplier. For example,
382 * {@code IllegalStateException::new}
383 *
384 * @param <X> Type of the exception to be thrown
385 * @param exceptionSupplier the supplying function that produces an
386 * exception to be thrown
387 * @return the value, if present
388 * @throws X if no value is present
389 * @throws NullPointerException if no value is present and the exception
390 * supplying function is {@code null}
391 */
392 public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
393 if (value != null) {
394 return value;
395 } else {
|