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.7KB

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