1 /*
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import java.util.function.IntSupplier;
29 import java.util.function.Supplier;
30 import java.util.stream.IntStream;
31
32 /**
33 * A container object which may or may not contain an {@code int} value. If a
34 * value is present, {@code isPresent()} returns {@code true} and
35 * {@code getAsInt()} returns the value.
36 *
37 * <p>Additional methods that depend on the presence or absence of a contained
38 * value are provided, such as {@link #orElse(int) orElse()}
39 * (returns a default value if no value is present) and
40 * {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (performs an
41 * action if a value is present).
42 *
43 * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
44 * class; use of identity-sensitive operations (including reference equality
45 * ({@code ==}), identity hash code, or synchronization) on instances of
46 * {@code OptionalInt} may have unpredictable results and should be avoided.
47 *
48 * @since 1.8
49 */
50 public final class OptionalInt {
51 /**
52 * Common instance for {@code empty()}.
53 */
54 private static final OptionalInt EMPTY = new OptionalInt();
55
56 /**
57 * If true then the value is present, otherwise indicates no value is present
58 */
59 private final boolean isPresent;
60 private final int value;
61
62 /**
63 * Construct an empty instance.
64 *
65 * @implNote Generally only one empty instance, {@link OptionalInt#EMPTY},
66 * should exist per VM.
67 */
93 */
94 private OptionalInt(int value) {
95 this.isPresent = true;
96 this.value = value;
97 }
98
99 /**
100 * Returns an {@code OptionalInt} describing the given value.
101 *
102 * @param value the value to describe
103 * @return an {@code OptionalInt} with the value present
104 */
105 public static OptionalInt of(int value) {
106 return new OptionalInt(value);
107 }
108
109 /**
110 * If a value is present, returns the value, otherwise throws
111 * {@code NoSuchElementException}.
112 *
113 * @return the value described by this {@code OptionalInt}
114 * @throws NoSuchElementException if no value is present
115 * @see OptionalInt#isPresent()
116 */
117 public int getAsInt() {
118 if (!isPresent) {
119 throw new NoSuchElementException("No value present");
120 }
121 return value;
122 }
123
124 /**
125 * If a value is present, returns {@code true}, otherwise {@code false}.
126 *
127 * @return {@code true} if a value is present, otherwise {@code false}
128 */
129 public boolean isPresent() {
130 return isPresent;
131 }
132
|
1 /*
2 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import java.util.function.IntSupplier;
29 import java.util.function.Supplier;
30 import java.util.stream.IntStream;
31
32 /**
33 * A container object which may or may not contain an {@code int} value. If a
34 * value is present, {@code isPresent()} returns {@code true} and
35 * {@code getAsInt()} returns the value.
36 *
37 * <p>Additional methods that depend on the presence or absence of a contained
38 * value are provided, such as {@link #orElse(int) orElse()}
39 * (returns a default value if no value is present) and
40 * {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (performs an
41 * action if a value is present).
42 *
43 * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
44 * class; use of identity-sensitive operations (including reference equality
45 * ({@code ==}), identity hash code, or synchronization) on instances of
46 * {@code OptionalInt} may have unpredictable results and should be avoided.
47 *
48 * @apiNote
49 * {@code OptionalInt} is primarily intended for use as a method return type where
50 * there is a clear need to represent "no result," and where using {@code null}
51 * is likely to cause errors. A variable whose type is {@code OptionalInt} should
52 * never itself be {@code null}; it should always point to an {@code OptionalInt}
53 * instance.
54 *
55 * @since 1.8
56 */
57 public final class OptionalInt {
58 /**
59 * Common instance for {@code empty()}.
60 */
61 private static final OptionalInt EMPTY = new OptionalInt();
62
63 /**
64 * If true then the value is present, otherwise indicates no value is present
65 */
66 private final boolean isPresent;
67 private final int value;
68
69 /**
70 * Construct an empty instance.
71 *
72 * @implNote Generally only one empty instance, {@link OptionalInt#EMPTY},
73 * should exist per VM.
74 */
100 */
101 private OptionalInt(int value) {
102 this.isPresent = true;
103 this.value = value;
104 }
105
106 /**
107 * Returns an {@code OptionalInt} describing the given value.
108 *
109 * @param value the value to describe
110 * @return an {@code OptionalInt} with the value present
111 */
112 public static OptionalInt of(int value) {
113 return new OptionalInt(value);
114 }
115
116 /**
117 * If a value is present, returns the value, otherwise throws
118 * {@code NoSuchElementException}.
119 *
120 * @apiNote
121 * The methods {@link #orElse(int) orElse()} and
122 * {@link #orElseGet(java.util.function.IntSupplier) orElseGet()}
123 * are generally preferable to this method, as they return a substitute
124 * value if the value is absent, instead of throwing an exception.
125 *
126 * @return the value described by this {@code OptionalInt}
127 * @throws NoSuchElementException if no value is present
128 * @see OptionalInt#isPresent()
129 */
130 public int getAsInt() {
131 if (!isPresent) {
132 throw new NoSuchElementException("No value present");
133 }
134 return value;
135 }
136
137 /**
138 * If a value is present, returns {@code true}, otherwise {@code false}.
139 *
140 * @return {@code true} if a value is present, otherwise {@code false}
141 */
142 public boolean isPresent() {
143 return isPresent;
144 }
145
|