|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package libcore.internal; |
| 18 | + |
| 19 | +import java.lang.reflect.Field; |
| 20 | +import java.lang.reflect.InvocationTargetException; |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.util.List; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +/** |
| 26 | + * Proof of concept / fake code whose only purpose is to demonstrate that Java 21 |
| 27 | + * language features are supported in libcore. |
| 28 | + */ |
| 29 | +public class Java21LanguageFeatures { |
| 30 | + |
| 31 | + public static int calculateApproximateArea(Shape s) { |
| 32 | + // Language feature (Java 21): JEP-441 - Pattern Matching for switch |
| 33 | + // Matching by type |
| 34 | + return switch (s) { |
| 35 | + case Triangle t -> (t.base * t.height / 2); |
| 36 | + case Rectangle r -> (r.length * r.width); |
| 37 | + case Circle c -> (c.radius * c.radius * 3); |
| 38 | + default -> 0; |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + public static abstract class Shape { |
| 43 | + } |
| 44 | + |
| 45 | + public static class Triangle extends Shape { |
| 46 | + public final int base; |
| 47 | + public final int height; |
| 48 | + |
| 49 | + public Triangle(int base, int height) { |
| 50 | + this.base = base; |
| 51 | + this.height = height; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static class Rectangle extends Shape { |
| 56 | + public final int length; |
| 57 | + public final int width; |
| 58 | + |
| 59 | + public Rectangle(int length, int width) { |
| 60 | + this.length = length; |
| 61 | + this.width = width; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static class Circle extends Shape { |
| 66 | + public final int radius; |
| 67 | + |
| 68 | + public Circle(int radius) { |
| 69 | + this.radius = radius; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public static String isDroid(String s) { |
| 74 | + // Language feature (Java 21): JEP-441 - Pattern Matching for switch |
| 75 | + // Matching with null check |
| 76 | + return switch (s) { |
| 77 | + case null -> "404"; |
| 78 | + case "Android", "Marvin" -> "Yes"; |
| 79 | + default -> "No"; |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + public static String isDroidIgnoreCase(String s) { |
| 84 | + // Language feature (Java 21): JEP-441 - Pattern Matching for switch |
| 85 | + // Matching with case refinement |
| 86 | + return switch (s) { |
| 87 | + case null -> "404"; |
| 88 | + case String str when str.equalsIgnoreCase("android") -> "Yes"; |
| 89 | + case String str when str.equalsIgnoreCase("marvin") -> "Yes"; |
| 90 | + default -> "No"; |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + public static boolean hasManySides(ShapeType t) { |
| 95 | + // Language feature (Java 21): JEP-441 - Pattern Matching for switch |
| 96 | + // Matching enum constants |
| 97 | + return switch (t) { |
| 98 | + case PointyShapeType.TRIANGLE -> false; |
| 99 | + case PointyShapeType.RECTANGLE -> true; |
| 100 | + case RoundedShapeType.CIRCLE -> false; |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + public sealed interface ShapeType permits PointyShapeType, RoundedShapeType {} |
| 105 | + public enum PointyShapeType implements ShapeType { TRIANGLE, RECTANGLE } |
| 106 | + public enum RoundedShapeType implements ShapeType { CIRCLE } |
| 107 | + |
| 108 | + public static int getX(Object obj) { |
| 109 | + // Language feature (Java 21): JEP-440 - Record Patterns |
| 110 | + if (obj instanceof Point(int x, int y)) { |
| 111 | + return x; |
| 112 | + } else { |
| 113 | + return 0; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public static int getY(Object obj) { |
| 118 | + // Language feature (Java 21): JEP-440 - Record Patterns |
| 119 | + // Pattern match with type inference for components |
| 120 | + if (obj instanceof Point(var x, var y)) { |
| 121 | + return y; |
| 122 | + } else { |
| 123 | + return 0; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public static boolean isLineVertical(Object obj) { |
| 128 | + // Language feature (Java 21): JEP-440 - Record Patterns |
| 129 | + // Nested record patterns |
| 130 | + if (obj instanceof Line(Point(int xa, int ya), |
| 131 | + Point(int xb, int yb))) { |
| 132 | + return xa == xb; |
| 133 | + } else { |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public record Point(int x, int y) { } |
| 139 | + |
| 140 | + public record Line(Point a, Point b) { } |
| 141 | + |
| 142 | + public static boolean isFirstItemLarger(Pair pair) { |
| 143 | + // Language feature (Java 21): JEP-440 - Record Patterns |
| 144 | + // Switch and record patterns |
| 145 | + return switch (pair) { |
| 146 | + case Pair(PairableInt(int xa), PairableInt(int xb)) -> xa > xb; |
| 147 | + case Pair(PairableString a, Pairable b) -> false; |
| 148 | + case Pair(PairableInt a, PairableString b) -> false; |
| 149 | + }; |
| 150 | + } |
| 151 | + |
| 152 | + public sealed interface Pairable permits PairableInt, PairableString { } |
| 153 | + |
| 154 | + public record PairableInt(int x) implements Pairable { } |
| 155 | + |
| 156 | + public record PairableString(String s) implements Pairable { } |
| 157 | + |
| 158 | + public record Pair(Pairable a, Pairable b) { } |
| 159 | + |
| 160 | + public static int sumOfMembers(AnyPair<Integer, Integer> pair) { |
| 161 | + // Language feature (Java 21): JEP-440 - Record Patterns |
| 162 | + // Pattern match with type inference type arguments |
| 163 | + return switch (pair) { |
| 164 | + case AnyPair(var a, var b) -> a.intValue() + b.intValue(); |
| 165 | + }; |
| 166 | + } |
| 167 | + |
| 168 | + public record AnyPair<S, T>(S a, T b) { } |
| 169 | + |
| 170 | + |
| 171 | + public sealed interface Blob permits SquishyBlob, ComplexBlob { } |
| 172 | + |
| 173 | + public enum SquishyBlob implements Blob { SMALL, MEDIUM, LARGE } |
| 174 | + |
| 175 | + public sealed interface ComplexBlob extends Blob permits MultiBlob, GeometricBlob { } |
| 176 | + |
| 177 | + public enum Color { RED, GREEN, BLUE } |
| 178 | + |
| 179 | + public record MultiBlob(ShapeType type, Color color, Integer count) implements ComplexBlob { } |
| 180 | + |
| 181 | + public record GeometricBlob(int sides) implements ComplexBlob { } |
| 182 | + |
| 183 | + public static Object getMainCharacteristic(Blob blob) { |
| 184 | + return switch (blob) { |
| 185 | + case SquishyBlob squishy -> squishy; |
| 186 | + case MultiBlob (RoundedShapeType type, var color, var count) |
| 187 | + when type == RoundedShapeType.CIRCLE -> RoundedShapeType.CIRCLE; |
| 188 | + case MultiBlob (var type, Color color, var count) |
| 189 | + when color == Color.RED -> Color.RED; |
| 190 | + case MultiBlob (var type, var color, var count) -> count; |
| 191 | + case GeometricBlob (int sides) when sides > 1000 -> Integer.valueOf(1000); |
| 192 | + case GeometricBlob (int sides) -> Integer.valueOf(sides); |
| 193 | + }; |
| 194 | + } |
| 195 | +} |
0 commit comments