1 /* 2 * Copyright (c) 2020, 2025, 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.NullRestricted; 32 import jdk.internal.vm.annotation.Strict; 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 static value class Point { 54 public int x; 55 public int y; 56 Point(int x, int y) { 57 this.x = x; 58 this.y = y; 59 } 60 } 61 62 static value class Value { 63 int i; 64 @NullRestricted @Strict 65 Point p; 66 Point nullable; 67 List<X> list; 68 Value(int i, Point/* Point! */ p, Point np, List<X> list) { 69 this.i = i; 70 this.p = p; 71 this.nullable = np; 72 this.list = list; 73 } 74 75 Point point() { 76 return p; 77 } 78 79 Point nullablePoint() { 80 return nullable; 81 } 82 83 int getI() { return i; } 84 85 List<X> list() { return list; } 86 } 87 88 final Value[] values = init(); 89 private Value[] init() { 90 Value[] values = new Value[10]; 91 for (int i = 0; i < 10; i++) { 92 values[i] = new Value(i, 93 new Point(i,i*2), 94 (i%2) == 0 ? null : new Point(i*10, i*20), 95 List.of(new X(i), new X(i*10))); 96 } 97 return values; 98 } 99 100 @Test 101 public void testValues() { 102 Arrays.stream(values) 103 .filter(v -> (v.i % 2) == 0) 104 .forEach(System.out::println); 105 } 106 107 @Test 108 public void testNullRestrictedType() { 109 Arrays.stream(values) 110 .map(Value::point) 111 .filter(p -> p.x >= 5) 112 .forEach(System.out::println); 113 114 } 115 116 @Test 117 public void testNullableValueType() { 118 Arrays.stream(values) 119 .map(Value::nullablePoint) 120 .filter(p -> p != null) 121 .forEach(System.out::println); 122 } 123 124 @Test 125 public void mapToInt() { 126 Stream<Point> stream = Arrays.stream(values) 127 .filter(v -> (v.getI() % 2) == 0) 128 .map(Value::point); 129 stream.forEach(p -> assertTrue((p.x % 2) == 0)); 130 } 131 132 @Test 133 public void testListOfValues() { 134 long count = Arrays.stream(values) 135 .map(Value::list) 136 .flatMap(List::stream) 137 .map(X::x) 138 .filter(x -> x >= 10) 139 .count(); 140 assertEquals(count, values.length-1); 141 } 142 }