< prev index next >

src/jdk.jcmd/share/classes/sun/tools/jstat/Expression.java

Print this page


   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 }
   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 import java.util.Optional;
  29 
  30 /**
  31  * A class that represents a mathematical expression as a tree structure
  32  * containing operators as interior nodes and operands as leaves. The
  33  * operands can be literals or lazily bound variables.
  34  *
  35  * @author Brian Doherty
  36  * @since 1.5
  37  */
  38 public class Expression {
  39     private static int nextOrdinal;
  40     private boolean debug = Boolean.getBoolean("Expression.debug");
  41     private Expression left;
  42     private Expression right;
  43     private Operator operator;
  44     private int ordinal = nextOrdinal++;
  45     private boolean required = false;
  46 
  47     Expression() {
  48         if (debug) {
  49             System.out.println("Expression " + ordinal + " created");
  50         }
  51     }
  52 
  53     void setLeft(Expression left) {
  54         if (debug) {
  55             System.out.println("Setting left on " + ordinal + " to " + left);
  56         }
  57         this.left = left;
  58         this.left.setRequired(required);
  59     }
  60 
  61     Expression getLeft() {
  62         return left;
  63     }
  64 
  65     void setRight(Expression right) {
  66         if (debug) {
  67             System.out.println("Setting right on " + ordinal + " to " + right);
  68         }
  69         this.right = right;
  70         this.right.setRequired(required);
  71     }
  72 
  73     Expression getRight() {
  74         return right;
  75     }
  76 
  77     void setOperator(Operator o) {
  78         if (debug) {
  79             System.out.println("Setting operator on " + ordinal + " to " + o);
  80         }
  81         this.operator = o;
  82     }
  83 
  84     Operator getOperator() {
  85         return operator;
  86     }
  87 
  88     void setRequired(boolean req) {
  89         this.required = req;
  90         Optional.ofNullable(left).ifPresent(l -> l.setRequired(required));
  91         Optional.ofNullable(right).ifPresent(r -> r.setRequired(required));
  92     }
  93 
  94     boolean isRequired() {
  95         return required;
  96     }
  97 
  98     public String toString() {
  99         StringBuilder b = new StringBuilder();
 100         b.append('(');
 101         if (left != null) {
 102             b.append(left.toString());
 103         }
 104         if (operator != null) {
 105             b.append(operator.toString());
 106             if (right != null) {
 107                 b.append(right.toString());
 108             }
 109         }
 110         b.append(')');
 111         return b.toString();
 112     }
 113 }
< prev index next >