109 * @throws NullPointerException if value is {@code null}
110 */
111 public static <T> Optional<T> of(T value) {
112 return new Optional<>(value);
113 }
114
115 /**
116 * Returns an {@code Optional} describing the given value, if
117 * non-{@code null}, otherwise returns an empty {@code Optional}.
118 *
119 * @param value the possibly-{@code null} value to describe
120 * @param <T> the type of the value
121 * @return an {@code Optional} with a present value if the specified value
122 * is non-{@code null}, otherwise an empty {@code Optional}
123 */
124 public static <T> Optional<T> ofNullable(T value) {
125 return value == null ? empty() : of(value);
126 }
127
128 /**
129 * If a value is present, returns the value, otherwise throws
130 * {@code NoSuchElementException}.
131 *
132 * @return the non-{@code null} value described by this {@code Optional}
133 * @throws NoSuchElementException if no value is present
134 * @see Optional#isPresent()
135 */
136 public T get() {
137 if (value == null) {
138 throw new NoSuchElementException("No value present");
139 }
140 return value;
141 }
142
143 /**
144 * If a value is present, returns {@code true}, otherwise {@code false}.
145 *
146 * @return {@code true} if a value is present, otherwise {@code false}
147 */
148 public boolean isPresent() {
149 return value != null;
150 }
151
152 /**
153 * If a value is present, performs the given action with the value,
154 * otherwise does nothing.
155 *
156 * @param action the action to be performed, if a value is present
|
109 * @throws NullPointerException if value is {@code null}
110 */
111 public static <T> Optional<T> of(T value) {
112 return new Optional<>(value);
113 }
114
115 /**
116 * Returns an {@code Optional} describing the given value, if
117 * non-{@code null}, otherwise returns an empty {@code Optional}.
118 *
119 * @param value the possibly-{@code null} value to describe
120 * @param <T> the type of the value
121 * @return an {@code Optional} with a present value if the specified value
122 * is non-{@code null}, otherwise an empty {@code Optional}
123 */
124 public static <T> Optional<T> ofNullable(T value) {
125 return value == null ? empty() : of(value);
126 }
127
128 /**
129 * Equivalent to {@link #getWhenPresent()}.
130 * If a value is present, returns the value, otherwise throws
131 * {@code NoSuchElementException}.
132 *
133 * @deprecated
134 * This method's simple name {@code get} makes it the obvious method to
135 * call to retrieve the value from this {@code Optional}. However, it has
136 * no mechanism to avoid an exception if this {@code Optional} is empty.
137 * This tends to lead to code that mishandles empty {@code Optional}
138 * values. Consider using other methods that handle the case where
139 * the {@code Optional} might be empty, such as
140 * {@link #filter(java.util.function.Predicate) filter()},
141 * {@link #map(java.util.function.Function) map()},
142 * {@link #ifPresent(java.util.function.Consumer) ifPresent()}
143 * and related methods, and
144 * {@link #orElse(java.lang.Object) orElse()} and related methods.
145 * Use {@link getWhenPresent()} when it is known that a value is
146 * always present.
147 *
148 * @return the non-{@code null} value described by this {@code Optional}
149 * @throws NoSuchElementException if no value is present
150 * @see Optional#isPresent()
151 */
152 @Deprecated(since="9")
153 public T get() {
154 return getWhenPresent();
155 }
156
157 /**
158 * If a value is present, returns the value, otherwise throws
159 * {@code NoSuchElementException}.
160 *
161 * @apiNote
162 * Use this method only when it is known that a value is always present.
163 *
164 * @return the non-{@code null} value described by this {@code Optional}
165 * @throws NoSuchElementException if no value is present
166 * @see Optional#isPresent()
167 */
168 public T getWhenPresent() {
169 if (value == null) {
170 throw new NoSuchElementException("No value present");
171 }
172 return value;
173 }
174
175 /**
176 * If a value is present, returns {@code true}, otherwise {@code false}.
177 *
178 * @return {@code true} if a value is present, otherwise {@code false}
179 */
180 public boolean isPresent() {
181 return value != null;
182 }
183
184 /**
185 * If a value is present, performs the given action with the value,
186 * otherwise does nothing.
187 *
188 * @param action the action to be performed, if a value is present
|