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