1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8231827
* @summary All example code from "Pattern Matching for Java" document, released April 2017, adjusted to current state (no switches, etc)
* @compile ExamplesFromProposal.java
* @run main ExamplesFromProposal
*/
interface Node {
}
class IntNode implements Node {
int value;
IntNode(int value) {
this.value = value;
}
}
class NegNode implements Node {
Node node;
NegNode(Node node) {
this.node = node;
}
}
class MulNode implements Node {
Node left, right;
MulNode(Node left, Node right) {
this.left = left;
this.right = right;
}
}
class AddNode implements Node {
Node left, right;
AddNode(Node left, Node right) {
this.left = left;
this.right = right;
}
}
public class ExamplesFromProposal {
public static Object getSomething() {
return new Long(42);
}
public static int eval(Node n) {
if (n instanceof IntNode in) return in.value;
else if (n instanceof NegNode nn) return -eval(nn.node);
else if (n instanceof AddNode an) return eval(an.left) + eval(an.right);
else if (n instanceof MulNode mn) return eval(mn.left) * eval(mn.right);
else {
// should never happen
throw new AssertionError("broken");
}
}
public static String toString(Node n) {
if (n instanceof IntNode in) return String.valueOf(in.value);
else if (n instanceof NegNode nn) return "-"+eval(nn.node);
else if (n instanceof AddNode an) return eval(an.left) + " + " + eval(an.right);
else if (n instanceof MulNode mn) return eval(mn.left) + " * " + eval(mn.right);
else {
// should never happen
throw new AssertionError("broken");
}
}
public static Node simplify(Node n) {
if (n instanceof IntNode in) {
return n;
} else if (n instanceof NegNode nn) {
return new NegNode(simplify(nn.node));
} else if (n instanceof AddNode ad) {
n = simplify(ad.left);
if (n instanceof IntNode intn) {
if (intn.value == 0)
return simplify(ad.right);
else
return new AddNode(intn, simplify(ad.right));
} else {
return new AddNode(simplify(ad.left), simplify(ad.right));
}
} else if (n instanceof MulNode mn) {
return new MulNode(simplify(mn.left), simplify(mn.right));
} else {
//should never happen
throw new AssertionError("broken");
}
}
public static void testNode(Node n, int expected) {
if (eval(n) != expected)
throw new AssertionError("broken");
}
public static void main(String[] args) {
Object x = new Integer(42);
if (x instanceof Integer i) {
// can use i here
System.out.println(i.intValue());
}
Object obj = getSomething();
String formatted = "unknown";
if (obj instanceof Integer i) {
formatted = String.format("int %d", i);
}
else if (obj instanceof Byte b) {
formatted = String.format("byte %d", b);
}
else if (obj instanceof Long l) {
formatted = String.format("long %d", l);
}
else if (obj instanceof Double d) {
formatted = String.format("double %f", d);
}
else if (obj instanceof String s) {
formatted = String.format("String %s", s);
}
System.out.println(formatted);
if (obj instanceof Integer i) formatted = String.format("int %d", i);
else if (obj instanceof Byte b) formatted = String.format("byte %d", b);
else if (obj instanceof Long l) formatted = String.format("long %d", l);
else if (obj instanceof Double d) formatted = String.format("double %f", d);
else if (obj instanceof String s) formatted = String.format("String %s", s);
else formatted = String.format("Something else "+ obj.toString());
System.out.println(formatted);
Node zero = new IntNode(0);
Node one = new IntNode(1);
Node ft = new IntNode(42);
Node temp = new AddNode(zero,ft);
testNode(temp,42);
if (toString(simplify(temp)).equals(toString(ft)))
System.out.println("Simplify worked!");
else
throw new AssertionError("broken");
if (toString(simplify(new AddNode(zero,temp))).equals(toString(ft)))
System.out.println("Simplify worked!");
else
throw new AssertionError("broken");
temp = new AddNode(zero,ft);
temp = new AddNode(one,temp);
temp = new AddNode(zero,temp);
Node fortythree = new AddNode(one,ft);
if (toString(simplify(temp)).equals(toString(fortythree)))
System.out.println("Simplify worked!");
else
throw new AssertionError("broken");
x = "Hello";
if (x instanceof String s1) {
System.out.println(s1);
}
if (x instanceof String s1 && s1.length() > 0) {
System.out.println(s1);
}
if (x instanceof String s1) {
System.out.println(s1 + " is a string");
} else {
System.out.println("not a string");
}
if (!(x instanceof String s1)) {
System.out.println("not a string");
} else {
System.out.println(s1 + " is a string");
}
}
}
|