< prev index next >

src/java.base/share/classes/java/util/OptionalLong.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


 123      * @return the value described by this {@code OptionalLong}
 124      * @throws NoSuchElementException if no value is present
 125      */
 126     public long getAsLong() {
 127         if (!isPresent) {
 128             throw new NoSuchElementException("No value present");
 129         }
 130         return value;
 131     }
 132 
 133     /**
 134      * If a value is present, returns {@code true}, otherwise {@code false}.
 135      *
 136      * @return {@code true} if a value is present, otherwise {@code false}
 137      */
 138     public boolean isPresent() {
 139         return isPresent;
 140     }
 141 
 142     /**











 143      * If a value is present, performs the given action with the value,
 144      * otherwise does nothing.
 145      *
 146      * @param action the action to be performed, if a value is present
 147      * @throws NullPointerException if value is present and the given action is
 148      *         {@code null}
 149      */
 150     public void ifPresent(LongConsumer action) {
 151         if (isPresent) {
 152             action.accept(value);
 153         }
 154     }
 155 
 156     /**
 157      * If a value is present, performs the given action with the value,
 158      * otherwise performs the given empty-based action.
 159      *
 160      * @param action the action to be performed, if a value is present
 161      * @param emptyAction the empty-based action to be performed, if no value is
 162      *        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


 123      * @return the value described by this {@code OptionalLong}
 124      * @throws NoSuchElementException if no value is present
 125      */
 126     public long getAsLong() {
 127         if (!isPresent) {
 128             throw new NoSuchElementException("No value present");
 129         }
 130         return value;
 131     }
 132 
 133     /**
 134      * If a value is present, returns {@code true}, otherwise {@code false}.
 135      *
 136      * @return {@code true} if a value is present, otherwise {@code false}
 137      */
 138     public boolean isPresent() {
 139         return isPresent;
 140     }
 141 
 142     /**
 143      * If a value is not present, returns {@code true}, otherwise
 144      * {@code false}.
 145      *
 146      * @return  {@code true} if a value is not present, otherwise {@code false}
 147      * @since   11
 148      */
 149     public boolean isEmpty() {
 150         return !isPresent;
 151     }
 152 
 153     /**
 154      * If a value is present, performs the given action with the value,
 155      * otherwise does nothing.
 156      *
 157      * @param action the action to be performed, if a value is present
 158      * @throws NullPointerException if value is present and the given action is
 159      *         {@code null}
 160      */
 161     public void ifPresent(LongConsumer action) {
 162         if (isPresent) {
 163             action.accept(value);
 164         }
 165     }
 166 
 167     /**
 168      * If a value is present, performs the given action with the value,
 169      * otherwise performs the given empty-based action.
 170      *
 171      * @param action the action to be performed, if a value is present
 172      * @param emptyAction the empty-based action to be performed, if no value is
 173      *        present


< prev index next >