1 /* 2 * Copyright (c) 2004, 2018, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.tools.jstat; 27 28 /** 29 * A class that represents a mathematical expression as a tree structure 30 * containing operators as interior nodes and operands as leaves. The 31 * operands can be literals or lazily bound variables. 32 * 33 * @author Brian Doherty 34 * @since 1.5 35 */ 36 public class Expression { 37 private static int nextOrdinal; 38 private boolean debug = Boolean.getBoolean("Expression.debug"); 39 private Expression left; 40 private Expression right; 41 private Operator operator; 42 private int ordinal = nextOrdinal++; 43 private boolean required = false; 44 45 Expression() { 46 if (debug) { 47 System.out.println("Expression " + ordinal + " created"); 48 } 49 } 50 51 void setLeft(Expression left) { 52 if (debug) { 53 System.out.println("Setting left on " + ordinal + " to " + left); 54 } 55 this.left = left; 56 this.left.setRequired(required); 57 } 58 59 Expression getLeft() { 60 return left; 61 } 62 63 void setRight(Expression right) { 64 if (debug) { 65 System.out.println("Setting right on " + ordinal + " to " + right); 66 } 67 this.right = right; 68 this.right.setRequired(required); 69 } 70 71 Expression getRight() { 72 return right; 73 } 74 75 void setOperator(Operator o) { 76 if (debug) { 77 System.out.println("Setting operator on " + ordinal + " to " + o); 78 } 79 this.operator = o; 80 } 81 82 Operator getOperator() { 83 return operator; 84 } 85 86 void setRequired(boolean r) { 87 this.required = r; 88 if (left != null) { 89 left.setRequired(required); 90 } 91 if (right != null) { 92 right.setRequired(required); 93 } 94 } 95 96 boolean isRequired() { 97 return required; 98 } 99 100 public String toString() { 101 StringBuilder b = new StringBuilder(); 102 b.append('('); 103 if (left != null) { 104 b.append(left.toString()); 105 } 106 if (operator != null) { 107 b.append(operator.toString()); 108 if (right != null) { 109 b.append(right.toString()); 110 } 111 } 112 b.append(')'); 113 return b.toString(); 114 } 115 }