equal
deleted
inserted
replaced
28 import java.util.function.LongSupplier; |
28 import java.util.function.LongSupplier; |
29 import java.util.function.Supplier; |
29 import java.util.function.Supplier; |
30 import java.util.stream.LongStream; |
30 import java.util.stream.LongStream; |
31 |
31 |
32 /** |
32 /** |
33 * A container object which may or may not contain a {@code long} value. If a |
33 * A container object which may or may not contain a {@code long} value. |
34 * value is present, {@code isPresent()} returns {@code true} and |
34 * If a value is present, {@code isPresent()} returns {@code true}. If no |
35 * {@code getAsLong()} returns the value. |
35 * value is present, the object is considered <i>empty</i> and |
|
36 * {@code isPresent()} returns {@code false}. |
36 * |
37 * |
37 * <p>Additional methods that depend on the presence or absence of a contained |
38 * <p>Additional methods that depend on the presence or absence of a contained |
38 * value are provided, such as {@link #orElse(long) orElse()} |
39 * value are provided, such as {@link #orElse(long) orElse()} |
39 * (returns a default value if no value is present) and |
40 * (returns a default value if no value is present) and |
40 * {@link #ifPresent(LongConsumer) ifPresent()} (performs an |
41 * {@link #ifPresent(LongConsumer) ifPresent()} (performs an |
115 /** |
116 /** |
116 * If a value is present, returns the value, otherwise throws |
117 * If a value is present, returns the value, otherwise throws |
117 * {@code NoSuchElementException}. |
118 * {@code NoSuchElementException}. |
118 * |
119 * |
119 * @apiNote |
120 * @apiNote |
120 * The methods {@link #orElse(long) orElse} and |
121 * The preferred alternative to this method is {@link #orElseThrow()}. |
121 * {@link #orElseGet(LongSupplier) orElseGet} |
|
122 * are generally preferable to this method, as they return a substitute |
|
123 * value if the value is absent, instead of throwing an exception. |
|
124 * |
122 * |
125 * @return the value described by this {@code OptionalLong} |
123 * @return the value described by this {@code OptionalLong} |
126 * @throws NoSuchElementException if no value is present |
124 * @throws NoSuchElementException if no value is present |
127 * @see OptionalLong#isPresent() |
|
128 */ |
125 */ |
129 public long getAsLong() { |
126 public long getAsLong() { |
130 if (!isPresent) { |
127 if (!isPresent) { |
131 throw new NoSuchElementException("No value present"); |
128 throw new NoSuchElementException("No value present"); |
132 } |
129 } |
223 public long orElseGet(LongSupplier supplier) { |
220 public long orElseGet(LongSupplier supplier) { |
224 return isPresent ? value : supplier.getAsLong(); |
221 return isPresent ? value : supplier.getAsLong(); |
225 } |
222 } |
226 |
223 |
227 /** |
224 /** |
|
225 * If a value is present, returns the value, otherwise throws |
|
226 * {@code NoSuchElementException}. |
|
227 * |
|
228 * @return the value described by this {@code OptionalLong} |
|
229 * @throws NoSuchElementException if no value is present |
|
230 * @since 10 |
|
231 */ |
|
232 public long orElseThrow() { |
|
233 if (!isPresent) { |
|
234 throw new NoSuchElementException("No value present"); |
|
235 } |
|
236 return value; |
|
237 } |
|
238 |
|
239 /** |
228 * If a value is present, returns the value, otherwise throws an exception |
240 * If a value is present, returns the value, otherwise throws an exception |
229 * produced by the exception supplying function. |
241 * produced by the exception supplying function. |
230 * |
242 * |
231 * @apiNote |
243 * @apiNote |
232 * A method reference to the exception constructor with an empty argument |
244 * A method reference to the exception constructor with an empty argument |