Coverage Report - com.sdmetrics.math.ExpressionNode - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionNode
100%
24/24
100%
18/18
1,462
 
 1  
 /*
 2  
  * SDMetrics Open Core for UML design measurement
 3  
  * Copyright (c) Juergen Wuest
 4  
  * To contact the author, see <http://www.sdmetrics.com/Contact.html>.
 5  
  * 
 6  
  * This file is part of the SDMetrics Open Core.
 7  
  * 
 8  
  * SDMetrics Open Core is free software: you can redistribute it and/or modify
 9  
  * it under the terms of the GNU Affero General Public License as
 10  
  * published by the Free Software Foundation, either version 3 of the
 11  
  * License, or (at your option) any later version.
 12  
     
 13  
  * SDMetrics Open Core is distributed in the hope that it will be useful,
 14  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  
  * GNU Affero General Public License for more details.
 17  
  *
 18  
  * You should have received a copy of the GNU Affero General Public License
 19  
  * along with SDMetrics Open Core.  If not, see <http://www.gnu.org/licenses/>.
 20  
  *
 21  
  */
 22  
 package com.sdmetrics.math;
 23  
 
 24  
 /** Represents a node in the operator tree for a math expression. */
 25  
 public class ExpressionNode {
 26  
 
 27  
         /** Type of the node: operator, identifier, constant etc. */
 28  
         private NodeType type;
 29  
         /** Value of the node. The operator, the identifier, the constant etc. */
 30  
         private String value;
 31  
         /** List of operands. */
 32  
         ExpressionNode[] operands;
 33  
 
 34  
         /**
 35  
          * Creates a new node representing an identifier.
 36  
          * 
 37  
          * @param identifier The identifier
 38  
          */
 39  
         public ExpressionNode(String identifier) {
 40  123
                 this(NodeType.IDENTIFIER, identifier);
 41  123
         }
 42  
 
 43  
         /**
 44  
          * Creates a new node of a given type and value, without operands.
 45  
          * 
 46  
          * @param type Type of the node
 47  
          * @param value Value of the node
 48  
          */
 49  33489
         ExpressionNode(NodeType type, String value) {
 50  33489
                 setValue(type, value, null);
 51  33489
         }
 52  
 
 53  
         /**
 54  
          * Creates a new operation node.
 55  
          * 
 56  
          * @param value The operation of the node
 57  
          * @param operands The operands of the node
 58  
          */
 59  4696
         ExpressionNode(String value, ExpressionNode[] operands) {
 60  4696
                 setValue(NodeType.OPERATION, value, operands);
 61  4696
         }
 62  
 
 63  
         /**
 64  
          * Retrieves the value of this node. The meaning of the value depends on the
 65  
          * node type:
 66  
          * <ul>
 67  
          * <li>For operations, the value is a string indicating the operation: +,-,
 68  
          * etc or a function or relation name.
 69  
          * <li>For numbers, the value is the string representation of the number.
 70  
          * <li>For string constants, the value is the constant string (without the
 71  
          * single quotes).
 72  
          * <li>For identifiers, the value is the identifier.
 73  
          * </ul>
 74  
          * 
 75  
          * @return value of the node
 76  
          */
 77  
         public String getValue() {
 78  78396
                 return value;
 79  
         }
 80  
 
 81  
         /**
 82  
          * Sets type, value, and operands of this node.
 83  
          * 
 84  
          * @param type New type of the node
 85  
          * @param value New value of the node
 86  
          * @param operands The new operands of the node (may be <code>null</code>
 87  
          *        for constants and identifiers)
 88  
          */
 89  
         final void setValue(NodeType type, String value, ExpressionNode[] operands) {
 90  39956
                 this.type = type;
 91  39956
                 this.value = value;
 92  39956
                 this.operands = operands;
 93  39956
         }
 94  
 
 95  
         /**
 96  
          * Gets the number of operands of this node.
 97  
          * 
 98  
          * @return The number of operands of this node
 99  
          * @since 2.3
 100  
          */
 101  
         public int getOperandCount() {
 102  779
                 return operands == null ? 0 : operands.length;
 103  
         }
 104  
 
 105  
         /***
 106  
          * Gets an operand of the node.
 107  
          * 
 108  
          * @param index Index of the operand. Valid indices run from 0 to
 109  
          *        {@link #getOperandCount()}.
 110  
          * @throws IllegalArgumentException Index out of range
 111  
          * @return The operand at the given index.
 112  
          * @since 2.3
 113  
          */
 114  
         public ExpressionNode getOperand(int index) {
 115  226
                 if (index >= getOperandCount())
 116  2
                         throw new IllegalArgumentException("No argument at index " + index
 117  1
                                         + " for operator '" + getValue() + "'.");
 118  225
                 return operands[index];
 119  
         }
 120  
 
 121  
         /**
 122  
          * Retrieves the node representing the left hand side of a binary operation,
 123  
          * or the operand of a unary operation. Convenience method for unary/binary
 124  
          * operations.
 125  
          * 
 126  
          * @return the left node, or <code>null</code> if there is no left operand
 127  
          */
 128  
         public ExpressionNode getLeftNode() {
 129  1784
                 return operands == null ? null : operands[0];
 130  
         }
 131  
 
 132  
         /**
 133  
          * Retrieves the node representing the right hand side of a binary
 134  
          * operation. Convenience method for binary operations.
 135  
          * 
 136  
          * @return the right node, or <code>null</code> if there is no right operand
 137  
          */
 138  
         public ExpressionNode getRightNode() {
 139  1332
                 return operands == null || operands.length < 2 ? null : operands[1];
 140  
         }
 141  
 
 142  
         /**
 143  
          * Tests whether this node represents an operation.
 144  
          * 
 145  
          * @return <code>true</code> if this node represents an operation.
 146  
          */
 147  
         public boolean isOperation() {
 148  240476
                 return type == NodeType.OPERATION;
 149  
         }
 150  
 
 151  
         /**
 152  
          * Tests whether this node represents a number constant.
 153  
          * 
 154  
          * @return <code>true</code> if this node represents a number constant.
 155  
          */
 156  
         public boolean isNumberConstant() {
 157  31195
                 return type == NodeType.NUMBER;
 158  
         }
 159  
 
 160  
         /**
 161  
          * Tests whether this node represents a string constant.
 162  
          * 
 163  
          * @return <code>true</code> if this node represents a string constant.
 164  
          */
 165  
         public boolean isStringConstant() {
 166  28346
                 return type == NodeType.STRING;
 167  
         }
 168  
 
 169  
         /**
 170  
          * Tests whether this node represents an identifier.
 171  
          * 
 172  
          * @return <code>true</code> if this node represents an identifier.
 173  
          */
 174  
         public boolean isIdentifier() {
 175  24843
                 return type == NodeType.IDENTIFIER;
 176  
         }
 177  
 
 178  
 }