View Javadoc
1   /*
2    * Copyright (c) 2004, 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  
44      Expression() {
45          if (debug) {
46              System.out.println("Expression " + ordinal + " created");
47          }
48      }
49  
50      void setLeft(Expression left) {
51          if (debug) {
52              System.out.println("Setting left on " + ordinal + " to " + left);
53          }
54          this.left = left;
55      }
56  
57      Expression getLeft() {
58          return left;
59      }
60  
61      void setRight(Expression right) {
62          if (debug) {
63              System.out.println("Setting right on " + ordinal + " to " + right);
64          }
65          this.right = right;
66      }
67  
68      Expression getRight() {
69          return right;
70      }
71  
72      void setOperator(Operator o) {
73          if (debug) {
74              System.out.println("Setting operator on " + ordinal + " to " + o);
75          }
76          this.operator = o;
77      }
78  
79      Operator getOperator() {
80          return operator;
81      }
82  
83      public String toString() {
84          StringBuilder b = new StringBuilder();
85          b.append('(');
86          if (left != null) {
87              b.append(left.toString());
88          }
89          if (operator != null) {
90              b.append(operator.toString());
91              if (right != null) {
92                  b.append(right.toString());
93              }
94          }
95          b.append(')');
96          return b.toString();
97      }
98  }