< prev index next >

src/java.base/share/classes/java/util/Optional.java

Print this page


   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


 143      * @return the non-{@code null} value described by this {@code Optional}
 144      * @throws NoSuchElementException if no value is present
 145      */
 146     public T get() {
 147         if (value == null) {
 148             throw new NoSuchElementException("No value present");
 149         }
 150         return value;
 151     }
 152 
 153     /**
 154      * If a value is present, returns {@code true}, otherwise {@code false}.
 155      *
 156      * @return {@code true} if a value is present, otherwise {@code false}
 157      */
 158     public boolean isPresent() {
 159         return value != null;
 160     }
 161 
 162     /**










 163      * If a value is present, performs the given action with the value,
 164      * otherwise does nothing.
 165      *
 166      * @param action the action to be performed, if a value is present
 167      * @throws NullPointerException if value is present and the given action is
 168      *         {@code null}
 169      */
 170     public void ifPresent(Consumer<? super T> action) {
 171         if (value != null) {
 172             action.accept(value);
 173         }
 174     }
 175 
 176     /**
 177      * If a value is present, performs the given action with the value,
 178      * otherwise performs the given empty-based action.
 179      *
 180      * @param action the action to be performed, if a value is present
 181      * @param emptyAction the empty-based action to be performed, if no value is
 182      *        present


   1 /*
   2  * Copyright (c) 2012, 2018, 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


 143      * @return the non-{@code null} value described by this {@code Optional}
 144      * @throws NoSuchElementException if no value is present
 145      */
 146     public T get() {
 147         if (value == null) {
 148             throw new NoSuchElementException("No value present");
 149         }
 150         return value;
 151     }
 152 
 153     /**
 154      * If a value is present, returns {@code true}, otherwise {@code false}.
 155      *
 156      * @return {@code true} if a value is present, otherwise {@code false}
 157      */
 158     public boolean isPresent() {
 159         return value != null;
 160     }
 161 
 162     /**
 163      * If a value is  not present, returns {@code true}, otherwise
 164      * {@code false}.
 165      *
 166      * @return {@code true} if a value is not present, otherwise {@code false}
 167      */
 168     public boolean isEmpty() {
 169         return value == null;
 170     }
 171 
 172     /**
 173      * If a value is present, performs the given action with the value,
 174      * otherwise does nothing.
 175      *
 176      * @param action the action to be performed, if a value is present
 177      * @throws NullPointerException if value is present and the given action is
 178      *         {@code null}
 179      */
 180     public void ifPresent(Consumer<? super T> action) {
 181         if (value != null) {
 182             action.accept(value);
 183         }
 184     }
 185 
 186     /**
 187      * If a value is present, performs the given action with the value,
 188      * otherwise performs the given empty-based action.
 189      *
 190      * @param action the action to be performed, if a value is present
 191      * @param emptyAction the empty-based action to be performed, if no value is
 192      *        present


< prev index next >