src/share/classes/java/util/OptionalInt.java

Print this page
rev 8872 : 8028816: Add value-type notice to Optional* classes
Reviewed-by: mduigou
Contributed-by: brian.goetz@oracle.com, bitterfoxc@gmail.com


  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
  23  * questions.
  24  */
  25 package java.util;
  26 
  27 import java.util.function.IntConsumer;
  28 import java.util.function.IntSupplier;
  29 import java.util.function.Supplier;
  30 
  31 /**
  32  * A container object which may or may not contain a {@code int} value.
  33  * If a value is present, {@code isPresent()} will return {@code true} and
  34  * {@code get()} will return the value.
  35  *
  36  * <p>Additional methods that depend on the presence or absence of a contained
  37  * value are provided, such as {@link #orElse(int) orElse()}
  38  * (return a default value if value not present) and
  39  * {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (execute a block
  40  * of code if the value is present).





  41  *
  42  * @since 1.8
  43  */
  44 public final class OptionalInt {
  45     /**
  46      * Common instance for {@code empty()}.
  47      */
  48     private static final OptionalInt EMPTY = new OptionalInt();
  49 
  50     /**
  51      * If true then the value is present, otherwise indicates no value is present
  52      */
  53     private final boolean isPresent;
  54     private final int value;
  55 
  56     /**
  57      * Construct an empty instance.
  58      *
  59      * @implNote Generally only one empty instance, {@link OptionalInt#EMPTY},
  60      * should exist per VM.




  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
  23  * questions.
  24  */
  25 package java.util;
  26 
  27 import java.util.function.IntConsumer;
  28 import java.util.function.IntSupplier;
  29 import java.util.function.Supplier;
  30 
  31 /**
  32  * A container object which may or may not contain a {@code int} value.
  33  * If a value is present, {@code isPresent()} will return {@code true} and
  34  * {@code getAsInt()} will return the value.
  35  *
  36  * <p>Additional methods that depend on the presence or absence of a contained
  37  * value are provided, such as {@link #orElse(int) orElse()}
  38  * (return a default value if value not present) and
  39  * {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (execute a block
  40  * of code if the value is present).
  41  *
  42  * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
  43  * class; use of identity-sensitive operations (including reference equality
  44  * ({@code ==}), identity hash code, or synchronization) on instances of
  45  * {@code OptionalInt} may have unpredictable effects and should be avoided.
  46  *
  47  * @since 1.8
  48  */
  49 public final class OptionalInt {
  50     /**
  51      * Common instance for {@code empty()}.
  52      */
  53     private static final OptionalInt EMPTY = new OptionalInt();
  54 
  55     /**
  56      * If true then the value is present, otherwise indicates no value is present
  57      */
  58     private final boolean isPresent;
  59     private final int value;
  60 
  61     /**
  62      * Construct an empty instance.
  63      *
  64      * @implNote Generally only one empty instance, {@link OptionalInt#EMPTY},
  65      * should exist per VM.