001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jstat;
027:
028: /**
029: * A class that represents a mathematical expression as a tree structure
030: * containing operators as interior nodes and operands as leaves. The
031: * operands can be literals or lazily bound variables.
032: *
033: * @author Brian Doherty
034: * @version 1.8, 05/05/07
035: * @since 1.5
036: */
037: public class Expression {
038: private static int nextOrdinal;
039: private boolean debug = Boolean.getBoolean("Expression.debug");
040: private Expression left;
041: private Expression right;
042: private Operator operator;
043: private int ordinal = nextOrdinal++;
044:
045: Expression() {
046: if (debug) {
047: System.out.println("Expression " + ordinal + " created");
048: }
049: }
050:
051: void setLeft(Expression left) {
052: if (debug) {
053: System.out.println("Setting left on " + ordinal + " to "
054: + left);
055: }
056: this .left = left;
057: }
058:
059: Expression getLeft() {
060: return left;
061: }
062:
063: void setRight(Expression right) {
064: if (debug) {
065: System.out.println("Setting right on " + ordinal + " to "
066: + right);
067: }
068: this .right = right;
069: }
070:
071: Expression getRight() {
072: return right;
073: }
074:
075: void setOperator(Operator o) {
076: if (debug) {
077: System.out.println("Setting operator on " + ordinal
078: + " to " + o);
079: }
080: this .operator = o;
081: }
082:
083: Operator getOperator() {
084: return operator;
085: }
086:
087: public String toString() {
088: StringBuilder b = new StringBuilder();
089: b.append('(');
090: if (left != null) {
091: b.append(left.toString());
092: }
093: if (operator != null) {
094: b.append(operator.toString());
095: if (right != null) {
096: b.append(right.toString());
097: }
098: }
099: b.append(')');
100: return b.toString();
101: }
102: }
|