1 /* 2 * Copyright (c) 2020, 2024, 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 * @summary Basic stream tests to iterate on nullable and null-restricted values 27 * @enablePreview 28 * @run junit/othervm StreamTest 29 */ 30 31 import jdk.internal.vm.annotation.ImplicitlyConstructible; 32 import jdk.internal.vm.annotation.NullRestricted; 33 34 import java.util.Arrays; 35 import java.util.List; 36 import java.util.stream.Stream; 37 38 import org.junit.jupiter.api.Test; 39 import static org.junit.jupiter.api.Assertions.*; 40 41 public class StreamTest { 42 43 static value class X { 44 int x; 45 X(int x) { 46 this.x = x; 47 } 48 int x() { 49 return x; 50 } 51 } 52 53 @ImplicitlyConstructible 54 static value class Point { 55 public int x; 56 public int y; 57 Point(int x, int y) { 58 this.x = x; 59 this.y = y; 60 } 61 } 62 63 @ImplicitlyConstructible 64 static value class Value { 65 int i; 66 @NullRestricted 67 Point p; 68 Point nullable; 69 List<X> list; 70 Value(int i, Point/* Point! */ p, Point np, List<X> list) { 71 this.i = i; 72 this.p = p; 73 this.nullable = np; 74 this.list = list; 75 } 76 77 Point point() { 78 return p; 79 } 80 81 Point nullablePoint() { 82 return nullable; 83 } 84 85 int getI() { return i; } 86 87 List<X> list() { return list; } 88 } 89 90 final Value[] values = init(); 91 private Value[] init() { 92 Value[] values = new Value[10]; 93 for (int i = 0; i < 10; i++) { 94 values[i] = new Value(i, 95 new Point(i,i*2), 96 (i%2) == 0 ? null : new Point(i*10, i*20), 97 List.of(new X(i), new X(i*10))); 98 } 99 return values; 100 } 101 102 @Test 103 public void testValues() { 104 Arrays.stream(values) 105 .filter(v -> (v.i % 2) == 0) 106 .forEach(System.out::println); 107 } 108 109 @Test 110 public void testNullRestrictedType() { 111 Arrays.stream(values) 112 .map(Value::point) 113 .filter(p -> p.x >= 5) 114 .forEach(System.out::println); 115 116 } 117 118 @Test 119 public void testNullableValueType() { 120 Arrays.stream(values) 121 .map(Value::nullablePoint) 122 .filter(p -> p != null) 123 .forEach(System.out::println); 124 } 125 126 @Test 127 public void mapToInt() { 128 Stream<Point> stream = Arrays.stream(values) 129 .filter(v -> (v.getI() % 2) == 0) 130 .map(Value::point); 131 stream.forEach(p -> assertTrue((p.x % 2) == 0)); 132 } 133 134 @Test 135 public void testListOfValues() { 136 long count = Arrays.stream(values) 137 .map(Value::list) 138 .flatMap(List::stream) 139 .map(X::x) 140 .filter(x -> x >= 10) 141 .count(); 142 assertEquals(count, values.length-1); 143 } 144 }