1 /*
2 * Copyright (c) 2012, 2020, 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
25 package java.util;
26
27 import java.util.function.IntConsumer;
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.
34 * If a value is present, {@code isPresent()} returns {@code true}. If no
35 * value is present, the object is considered <i>empty</i> and
36 * {@code isPresent()} returns {@code false}.
37 *
38 * <p>Additional methods that depend on the presence or absence of a contained
39 * value are provided, such as {@link #orElse(int) orElse()}
40 * (returns a default value if no value is present) and
41 * {@link #ifPresent(IntConsumer) ifPresent()} (performs an
42 * action if a value is present).
43 *
44 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
45 * class; programmers should treat instances that are
46 * {@linkplain #equals(Object) equal} as interchangeable and should not
47 * use instances for synchronization, or unpredictable behavior may
48 * occur. For example, in a future release, synchronization may fail.
49 *
50 * @apiNote
51 * {@code OptionalInt} is primarily intended for use as a method return type where
52 * there is a clear need to represent "no result." A variable whose type is
53 * {@code OptionalInt} should never itself be {@code null}; it should always point
54 * to an {@code OptionalInt} instance.
55 *
56 * @since 1.8
57 */
58 @jdk.internal.ValueBased
59 public final class OptionalInt {
60 /**
61 * Common instance for {@code empty()}.
62 */
63 private static final OptionalInt EMPTY = new OptionalInt();
64
65 /**
66 * If true then the value is present, otherwise indicates no value is present
67 */
68 private final boolean isPresent;
69 private final int value;
70
71 /**
72 * Construct an empty instance.
73 *
74 * @implNote Generally only one empty instance, {@link OptionalInt#EMPTY},
75 * should exist per VM.
76 */
77 private OptionalInt() {
78 this.isPresent = false;
79 this.value = 0;
|
1 /*
2 * Copyright (c) 2012, 2026, 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
25 package java.util;
26
27 import java.util.function.IntConsumer;
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.
34 * If a value is present, {@code isPresent()} returns {@code true}. If no
35 * value is present, the object is considered <i>empty</i> and
36 * {@code isPresent()} returns {@code false}.
37 *
38 * <p>Additional methods that depend on the presence or absence of a contained
39 * value are provided, such as {@link #orElse(int) orElse()}
40 * (returns a default value if no value is present) and
41 * {@link #ifPresent(IntConsumer) ifPresent()} (performs an
42 * action if a value is present).
43 *
44 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
45 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
46 * as interchangeable and should not use instances for synchronization or
47 * with {@linkplain java.lang.ref.Reference object references}.
48 *
49 * <div class="preview-block">
50 * <div class="preview-comment">
51 * When preview features are enabled, {@code OptionalInt} is a {@linkplain Class#isValue value class}.
52 * Use of value class instances for synchronization or with
53 * {@linkplain java.lang.ref.Reference object references} result in
54 * {@link IdentityException}.
55 * </div>
56 * </div>
57 *
58 * @apiNote
59 * {@code OptionalInt} is primarily intended for use as a method return type where
60 * there is a clear need to represent "no result." A variable whose type is
61 * {@code OptionalInt} should never itself be {@code null}; it should always point
62 * to an {@code OptionalInt} instance.
63 *
64 * @since 1.8
65 */
66 @jdk.internal.ValueBased
67 // See doc/value-class-preview.md for an overview of value class generation
68 public final /*value*/ class OptionalInt {
69 /**
70 * Common instance for {@code empty()}.
71 */
72 private static final OptionalInt EMPTY = new OptionalInt();
73
74 /**
75 * If true then the value is present, otherwise indicates no value is present
76 */
77 private final boolean isPresent;
78 private final int value;
79
80 /**
81 * Construct an empty instance.
82 *
83 * @implNote Generally only one empty instance, {@link OptionalInt#EMPTY},
84 * should exist per VM.
85 */
86 private OptionalInt() {
87 this.isPresent = false;
88 this.value = 0;
|