1 /*
  2  * Copyright (c) 2019, 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
 23  * questions.
 24  */
 25 
 26 /*
 27  * @test
 28  * @bug 8230121
 29  * @summary Javac does not properly parse nullable projection types of parameterized inline types
 30  * @compile GenericsAndValues5.java
 31  * @run main/othervm GenericsAndValues5
 32  */
 33 
 34 import java.util.NoSuchElementException;
 35 import java.util.Objects;
 36 import java.util.function.Function;
 37 import java.util.List;
 38 import java.util.ArrayList;
 39 
 40 primitive class Optional<T> {
 41     private T value;
 42 
 43     @SuppressWarnings("unchecked")
 44     public static <T> Optional<T> empty() {
 45         return (Optional<T>) Optional.default;
 46     }
 47 
 48     private Optional(T value) {
 49         this.value = value;
 50     }
 51 
 52     public static <T> Optional<T> of(T value) {
 53         if (value == null)
 54             return empty();
 55         return new Optional<T>(value);
 56     }
 57 
 58     public T get() {
 59         if (value == null)
 60             throw new NoSuchElementException("No value present");
 61         return value;
 62     }
 63 
 64     public boolean isPresent() {
 65         return value != null;
 66     }
 67 
 68     public T orElse(T other) {
 69         return value != null ? value : other;
 70     }
 71 
 72     public <U> Optional<U> map(Function<? super T, ? extends U> mapper) {
 73         Objects.requireNonNull(mapper);
 74         if (!isPresent())
 75             return empty();
 76         else
 77             return Optional.of(mapper.apply(value));
 78     }
 79 
 80     @Override
 81     public String toString() {
 82         return value != null ? String.format("Optional[%s]", value) : "Optional.empty";
 83     }
 84 }
 85 
 86 public final class GenericsAndValues5 {
 87 
 88    public static void main(String[] args) {
 89 
 90        List<Optional.ref<Integer>> opts = new ArrayList<>();
 91        for (int i=0; i < 6; i++) {
 92            Optional<Integer> oi = Optional.of(i);
 93            opts.add((Optional.ref<Integer>)oi);
 94            Optional<Integer> oe = Optional.empty();
 95            opts.add((Optional.ref<Integer>)oe);
 96        }
 97 
 98        Integer total = opts.stream()
 99            .map((Optional.ref<Integer> o) -> {
100                Optional<Integer> op = (Optional<Integer>)o;
101                return op.orElse(0);
102            })
103            .reduce(0, (x, y) -> x + y);
104 
105         if (total != 15) {
106             throw new AssertionError("Incorrect output: " + total);
107         }
108    }
109 }