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.

ConditionTreeFactory.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.actions;
  23. /**
  24. * Provides methods to automatically generated condition tree for a specified
  25. * number of arguments.
  26. *
  27. * @since 0.6
  28. * @author chris
  29. */
  30. public abstract class ConditionTreeFactory {
  31. /**
  32. * Retrieves a condition tree for the specified number of arguments.
  33. *
  34. * @param args The number of arguments in the {@link Action}
  35. * @return A ConditionTree for the specified number of args
  36. */
  37. public abstract ConditionTree getConditionTree(final int args);
  38. /**
  39. * Retrieves the type this of factory.
  40. *
  41. * @return This factory's type
  42. */
  43. public abstract ConditionTreeFactoryType getType();
  44. /**
  45. * The possible types of ConditionTreeFactories
  46. */
  47. public static enum ConditionTreeFactoryType {
  48. /** Factories that produce disjunction (OR) trees. */
  49. DISJUNCTION,
  50. /** Factories that produce conjunction (AND) trees. */
  51. CONJUNCTION,
  52. /** Factories that produce custom trees. */
  53. CUSTOM,
  54. }
  55. /**
  56. * Creates condition trees where the arguments are conjoined together.
  57. */
  58. public static class ConjunctionFactory extends ConditionTreeFactory {
  59. /** {@inheritDoc} */
  60. @Override
  61. public ConditionTree getConditionTree(final int args) {
  62. return ConditionTree.createConjunction(args);
  63. }
  64. /** {@inheritDoc} */
  65. @Override
  66. public ConditionTreeFactoryType getType() {
  67. return ConditionTreeFactoryType.CONJUNCTION;
  68. }
  69. }
  70. /**
  71. * Creates condition trees where the arguments are disjoined together.
  72. */
  73. public static class DisjunctionFactory extends ConditionTreeFactory {
  74. /** {@inheritDoc} */
  75. @Override
  76. public ConditionTree getConditionTree(final int args) {
  77. return ConditionTree.createDisjunction(args);
  78. }
  79. /** {@inheritDoc} */
  80. @Override
  81. public ConditionTreeFactoryType getType() {
  82. return ConditionTreeFactoryType.DISJUNCTION;
  83. }
  84. }
  85. /**
  86. * Creates condition trees with a custom structure.
  87. */
  88. public static class CustomFactory extends ConditionTreeFactory {
  89. /** The condition tree to use. */
  90. protected final ConditionTree tree;
  91. /**
  92. * Creates a new CustomFactory for the specified tree.
  93. *
  94. * @param tree The tree to use
  95. */
  96. public CustomFactory(final ConditionTree tree) {
  97. this.tree = tree;
  98. }
  99. /** {@inheritDoc} */
  100. @Override
  101. public ConditionTree getConditionTree(final int args) {
  102. return tree;
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public ConditionTreeFactoryType getType() {
  107. return ConditionTreeFactoryType.CUSTOM;
  108. }
  109. }
  110. /**
  111. * Retrieves a factory that will extrapolate the specified
  112. * {@link ConditionTree} for different number of arguments, if applicable.
  113. *
  114. * @param tree The {@link ConditionTree} that's in use
  115. * @param args The number of conditions currently in use
  116. * @return A {@link ConditionTreeFactory} that will create relevant
  117. * {@link ConditionTree}s
  118. */
  119. public static ConditionTreeFactory getFactory(final ConditionTree tree, final int args) {
  120. if (tree.equals(ConditionTree.createConjunction(args))) {
  121. return new ConjunctionFactory();
  122. } else if (tree.equals(ConditionTree.createDisjunction(args))) {
  123. return new DisjunctionFactory();
  124. } else {
  125. return new CustomFactory(tree);
  126. }
  127. }
  128. }