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.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /**
 25  * @test
 26  * @bug 8229537
 27  * @summary [lworld] Poor interaction between explicit lambda parameters and nullable projection types.
 28  * @compile -XDenablePrimitiveClasses ExplicitLambdaWithNullableTypes3.java
 29  * @run main/othervm -XX:+EnableValhalla -XX:+EnablePrimitiveClasses ExplicitLambdaWithNullableTypes3
 30  */
 31 
 32 import java.util.List;
 33 import java.util.ArrayList;
 34 import java.util.function.*;
 35 import java.util.NoSuchElementException;
 36 import java.util.stream.*;
 37 
 38 primitive class OptionalInt {
 39     // private static final OptionalInt EMPTY = OptionalInt.default;
 40 
 41     private boolean isPresent;
 42     private int v;
 43 
 44     public static OptionalInt empty() {
 45         return OptionalInt.default;
 46     }
 47 
 48     public OptionalInt(int val) {
 49         this.v = val;
 50         this.isPresent = true;
 51     }
 52 
 53     public static OptionalInt of(int val) {
 54         return new OptionalInt(val);
 55     }
 56 
 57     public int getAsInt() {
 58         if (!isPresent)
 59             throw new NoSuchElementException("No value present");
 60 
 61         return v;
 62     }
 63 
 64     public boolean isPresent() {
 65         return isPresent;
 66     }
 67 
 68     public void ifPresent(IntConsumer consumer) {
 69         if (isPresent)
 70             consumer.accept(v);
 71     }
 72 
 73     public int orElse(int other) {
 74         return isPresent ? v : other;
 75     }
 76 }
 77 
 78 public final class ExplicitLambdaWithNullableTypes3 {
 79 
 80    public static void main(String[] args) {
 81        List<OptionalInt.ref> opts = new ArrayList<>();
 82        for (int i=0; i < 5; i++) {
 83            opts.add(OptionalInt.of(i));
 84            opts.add(OptionalInt.empty());
 85            opts.add(null);
 86        }
 87 
 88        Stream<OptionalInt.ref> soi = opts.stream();
 89        ToIntFunction<OptionalInt.ref> f = o -> {
 90             if (o == null) return 0;
 91             OptionalInt op = (OptionalInt)o;
 92             return op.orElse(0);
 93         };
 94 
 95        IntStream sint = soi.mapToInt(f);
 96        int total = sint.reduce(0, (x, y) -> x + y);
 97 
 98        if (total != 10) {
 99             throw new AssertionError("Incorrect output: " + total);
100        }
101    }
102 }