Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstat/Expression.java
41159 views
/*1* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jstat;2627/**28* A class that represents a mathematical expression as a tree structure29* containing operators as interior nodes and operands as leaves. The30* operands can be literals or lazily bound variables.31*32* @author Brian Doherty33* @since 1.534*/35public class Expression {36private static int nextOrdinal;37private boolean debug = Boolean.getBoolean("Expression.debug");38private Expression left;39private Expression right;40private Operator operator;41private int ordinal = nextOrdinal++;42private boolean required = false;4344Expression() {45if (debug) {46System.out.println("Expression " + ordinal + " created");47}48}4950void setLeft(Expression left) {51if (debug) {52System.out.println("Setting left on " + ordinal + " to " + left);53}54this.left = left;55this.left.setRequired(required);56}5758Expression getLeft() {59return left;60}6162void setRight(Expression right) {63if (debug) {64System.out.println("Setting right on " + ordinal + " to " + right);65}66this.right = right;67this.right.setRequired(required);68}6970Expression getRight() {71return right;72}7374void setOperator(Operator o) {75if (debug) {76System.out.println("Setting operator on " + ordinal + " to " + o);77}78this.operator = o;79}8081Operator getOperator() {82return operator;83}8485void setRequired(boolean r) {86this.required = r;87if (left != null) {88left.setRequired(required);89}90if (right != null) {91right.setRequired(required);92}93}9495boolean isRequired() {96return required;97}9899public String toString() {100StringBuilder b = new StringBuilder();101b.append('(');102if (left != null) {103b.append(left.toString());104}105if (operator != null) {106b.append(operator.toString());107if (right != null) {108b.append(right.toString());109}110}111b.append(')');112return b.toString();113}114}115116117