76 * @return an empty {@code Optional}
77 */
78 public static<T> Optional<T> empty() {
79 @SuppressWarnings("unchecked")
80 Optional<T> t = (Optional<T>) EMPTY;
81 return t;
82 }
83
84 /**
85 * Constructs an instance with the value present.
86 *
87 * @param value the non-null value to be present
88 */
89 private Optional(T value) {
90 this.value = Objects.requireNonNull(value);
91 }
92
93 /**
94 * Returns an {@code Optional} with the specified present non-null value.
95 *
96 * @param value the value to be present, which must be non-null
97 * @return an {@code Optional} with the value present
98 */
99 public static <T> Optional<T> of(T value) {
100 return new Optional<>(value);
101 }
102
103 /**
104 * Returns an {@code Optional} describing the specified value, if non-null,
105 * otherwise returns an empty {@code Optional}.
106 *
107 * @param value the possibly-null value to describe
108 * @return an {@code Optional} with a present value if the specified value
109 * is non-null, otherwise an empty {@code Optional}
110 */
111 public static <T> Optional<T> ofNullable(T value) {
112 return value == null ? empty() : of(value);
113 }
114
115 /**
116 * If a value is present in this {@code Optional}, returns the value,
117 * otherwise throws {@code NoSuchElementException}.
118 *
119 * @return the non-null value held by this {@code Optional}
120 * @throws NoSuchElementException if there is no value present
121 *
122 * @see Optional#isPresent()
123 */
124 public T get() {
125 if (value == null) {
126 throw new NoSuchElementException("No value present");
|
76 * @return an empty {@code Optional}
77 */
78 public static<T> Optional<T> empty() {
79 @SuppressWarnings("unchecked")
80 Optional<T> t = (Optional<T>) EMPTY;
81 return t;
82 }
83
84 /**
85 * Constructs an instance with the value present.
86 *
87 * @param value the non-null value to be present
88 */
89 private Optional(T value) {
90 this.value = Objects.requireNonNull(value);
91 }
92
93 /**
94 * Returns an {@code Optional} with the specified present non-null value.
95 *
96 * @param <T> the class of the value
97 * @param value the value to be present, which must be non-null
98 * @return an {@code Optional} with the value present
99 */
100 public static <T> Optional<T> of(T value) {
101 return new Optional<>(value);
102 }
103
104 /**
105 * Returns an {@code Optional} describing the specified value, if non-null,
106 * otherwise returns an empty {@code Optional}.
107 *
108 * @param <T> the class of the value
109 * @param value the possibly-null value to describe
110 * @return an {@code Optional} with a present value if the specified value
111 * is non-null, otherwise an empty {@code Optional}
112 */
113 public static <T> Optional<T> ofNullable(T value) {
114 return value == null ? empty() : of(value);
115 }
116
117 /**
118 * If a value is present in this {@code Optional}, returns the value,
119 * otherwise throws {@code NoSuchElementException}.
120 *
121 * @return the non-null value held by this {@code Optional}
122 * @throws NoSuchElementException if there is no value present
123 *
124 * @see Optional#isPresent()
125 */
126 public T get() {
127 if (value == null) {
128 throw new NoSuchElementException("No value present");
|