You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TreeToken.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.calc;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. /**
  26. * Describes a tree of {@link Token}s.
  27. *
  28. * @author chris
  29. */
  30. public class TreeToken {
  31. /** The children of this node. */
  32. private final List<TreeToken> children = new ArrayList<TreeToken>();
  33. /** The token at the root of the tree. */
  34. private final Token token;
  35. /** Whether or not this tree has been processed. */
  36. private boolean processed = false;
  37. /**
  38. * Creates a new tree with the specified token at the root.
  39. *
  40. * @param token The root token
  41. */
  42. public TreeToken(final Token token) {
  43. this.token = token;
  44. }
  45. /**
  46. * Retrieves the (direct) children of this tree.
  47. *
  48. * @return This tree's children
  49. */
  50. public List<TreeToken> getChildren() {
  51. return children;
  52. }
  53. /**
  54. * Retrieves the root token of this tree.
  55. *
  56. * @return This tree's token
  57. */
  58. public Token getToken() {
  59. return token;
  60. }
  61. /**
  62. * Adds the specified child to this tree.
  63. *
  64. * @param token The child to be added
  65. */
  66. public void addChild(final TreeToken token) {
  67. children.add(token);
  68. }
  69. /**
  70. * Determines if this tree has been processed.
  71. *
  72. * @return True if the tree has been processed, false otherwise
  73. */
  74. public boolean isProcessed() {
  75. return processed;
  76. }
  77. /**
  78. * Sets the processed flag of this tree to true.
  79. */
  80. public void setProcessed() {
  81. processed = true;
  82. }
  83. /**
  84. * Evaluates this tree to return a number.
  85. *
  86. * @return A numerical evaluation of this tree.
  87. */
  88. public Number evaluate() {
  89. return token.getType().evaluate(this);
  90. }
  91. /** {@inheritDoc} */
  92. @Override
  93. public String toString() {
  94. return "[token: " + token + "; children: " + children + "; processed: "
  95. + processed + "]";
  96. }
  97. }