|
1 /* |
|
2 * Copyright (c) 2017, 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 8231827 |
|
27 * @summary All example code from "Pattern Matching for Java" document, released April 2017, adjusted to current state (no switches, etc) |
|
28 * @compile --enable-preview -source ${jdk.version} ExamplesFromProposal.java |
|
29 * @run main/othervm --enable-preview ExamplesFromProposal |
|
30 */ |
|
31 |
|
32 interface Node { |
|
33 } |
|
34 |
|
35 class IntNode implements Node { |
|
36 int value; |
|
37 |
|
38 IntNode(int value) { |
|
39 this.value = value; |
|
40 } |
|
41 } |
|
42 |
|
43 class NegNode implements Node { |
|
44 Node node; |
|
45 |
|
46 NegNode(Node node) { |
|
47 this.node = node; |
|
48 } |
|
49 } |
|
50 |
|
51 class MulNode implements Node { |
|
52 Node left, right; |
|
53 |
|
54 MulNode(Node left, Node right) { |
|
55 this.left = left; |
|
56 this.right = right; |
|
57 } |
|
58 } |
|
59 |
|
60 class AddNode implements Node { |
|
61 Node left, right; |
|
62 |
|
63 AddNode(Node left, Node right) { |
|
64 this.left = left; |
|
65 this.right = right; |
|
66 } |
|
67 } |
|
68 |
|
69 public class ExamplesFromProposal { |
|
70 |
|
71 public static Object getSomething() { |
|
72 return new Long(42); |
|
73 } |
|
74 |
|
75 public static int eval(Node n) { |
|
76 if (n instanceof IntNode in) return in.value; |
|
77 else if (n instanceof NegNode nn) return -eval(nn.node); |
|
78 else if (n instanceof AddNode an) return eval(an.left) + eval(an.right); |
|
79 else if (n instanceof MulNode mn) return eval(mn.left) * eval(mn.right); |
|
80 else { |
|
81 // should never happen |
|
82 throw new AssertionError("broken"); |
|
83 } |
|
84 } |
|
85 |
|
86 public static String toString(Node n) { |
|
87 if (n instanceof IntNode in) return String.valueOf(in.value); |
|
88 else if (n instanceof NegNode nn) return "-"+eval(nn.node); |
|
89 else if (n instanceof AddNode an) return eval(an.left) + " + " + eval(an.right); |
|
90 else if (n instanceof MulNode mn) return eval(mn.left) + " * " + eval(mn.right); |
|
91 else { |
|
92 // should never happen |
|
93 throw new AssertionError("broken"); |
|
94 } |
|
95 } |
|
96 |
|
97 public static Node simplify(Node n) { |
|
98 if (n instanceof IntNode in) { |
|
99 return n; |
|
100 } else if (n instanceof NegNode nn) { |
|
101 return new NegNode(simplify(nn.node)); |
|
102 } else if (n instanceof AddNode ad) { |
|
103 n = simplify(ad.left); |
|
104 if (n instanceof IntNode intn) { |
|
105 if (intn.value == 0) |
|
106 return simplify(ad.right); |
|
107 else |
|
108 return new AddNode(intn, simplify(ad.right)); |
|
109 } else { |
|
110 return new AddNode(simplify(ad.left), simplify(ad.right)); |
|
111 } |
|
112 } else if (n instanceof MulNode mn) { |
|
113 return new MulNode(simplify(mn.left), simplify(mn.right)); |
|
114 } else { |
|
115 //should never happen |
|
116 throw new AssertionError("broken"); |
|
117 } |
|
118 } |
|
119 |
|
120 public static void testNode(Node n, int expected) { |
|
121 if (eval(n) != expected) |
|
122 throw new AssertionError("broken"); |
|
123 } |
|
124 |
|
125 public static void main(String[] args) { |
|
126 Object x = new Integer(42); |
|
127 |
|
128 if (x instanceof Integer i) { |
|
129 // can use i here |
|
130 System.out.println(i.intValue()); |
|
131 } |
|
132 |
|
133 Object obj = getSomething(); |
|
134 |
|
135 String formatted = "unknown"; |
|
136 if (obj instanceof Integer i) { |
|
137 formatted = String.format("int %d", i); |
|
138 } |
|
139 else if (obj instanceof Byte b) { |
|
140 formatted = String.format("byte %d", b); |
|
141 } |
|
142 else if (obj instanceof Long l) { |
|
143 formatted = String.format("long %d", l); |
|
144 } |
|
145 else if (obj instanceof Double d) { |
|
146 formatted = String.format("double %f", d); |
|
147 } |
|
148 else if (obj instanceof String s) { |
|
149 formatted = String.format("String %s", s); |
|
150 } |
|
151 System.out.println(formatted); |
|
152 |
|
153 if (obj instanceof Integer i) formatted = String.format("int %d", i); |
|
154 else if (obj instanceof Byte b) formatted = String.format("byte %d", b); |
|
155 else if (obj instanceof Long l) formatted = String.format("long %d", l); |
|
156 else if (obj instanceof Double d) formatted = String.format("double %f", d); |
|
157 else if (obj instanceof String s) formatted = String.format("String %s", s); |
|
158 else formatted = String.format("Something else "+ obj.toString()); |
|
159 System.out.println(formatted); |
|
160 |
|
161 Node zero = new IntNode(0); |
|
162 Node one = new IntNode(1); |
|
163 Node ft = new IntNode(42); |
|
164 |
|
165 Node temp = new AddNode(zero,ft); |
|
166 |
|
167 testNode(temp,42); |
|
168 |
|
169 |
|
170 |
|
171 if (toString(simplify(temp)).equals(toString(ft))) |
|
172 System.out.println("Simplify worked!"); |
|
173 else |
|
174 throw new AssertionError("broken"); |
|
175 |
|
176 |
|
177 if (toString(simplify(new AddNode(zero,temp))).equals(toString(ft))) |
|
178 System.out.println("Simplify worked!"); |
|
179 else |
|
180 throw new AssertionError("broken"); |
|
181 |
|
182 |
|
183 temp = new AddNode(zero,ft); |
|
184 temp = new AddNode(one,temp); |
|
185 temp = new AddNode(zero,temp); |
|
186 |
|
187 Node fortythree = new AddNode(one,ft); |
|
188 |
|
189 if (toString(simplify(temp)).equals(toString(fortythree))) |
|
190 System.out.println("Simplify worked!"); |
|
191 else |
|
192 throw new AssertionError("broken"); |
|
193 |
|
194 |
|
195 x = "Hello"; |
|
196 |
|
197 if (x instanceof String s1) { |
|
198 System.out.println(s1); |
|
199 } |
|
200 if (x instanceof String s1 && s1.length() > 0) { |
|
201 System.out.println(s1); |
|
202 } |
|
203 if (x instanceof String s1) { |
|
204 System.out.println(s1 + " is a string"); |
|
205 } else { |
|
206 System.out.println("not a string"); |
|
207 } |
|
208 |
|
209 if (!(x instanceof String s1)) { |
|
210 System.out.println("not a string"); |
|
211 } else { |
|
212 System.out.println(s1 + " is a string"); |
|
213 } |
|
214 } |
|
215 } |