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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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 number of arguments.
  25. *
  26. * @since 0.6
  27. */
  28. public abstract class ConditionTreeFactory {
  29. /**
  30. * Retrieves a condition tree for the specified number of arguments.
  31. *
  32. * @param args The number of arguments in the {@link Action}
  33. *
  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 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 {@link ConditionTree} for different
  56. * 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. *
  61. * @return A {@link ConditionTreeFactory} that will create relevant {@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. @Override
  77. public ConditionTree getConditionTree(final int args) {
  78. return ConditionTree.createConjunction(args);
  79. }
  80. @Override
  81. public ConditionTreeFactoryType getType() {
  82. return ConditionTreeFactoryType.CONJUNCTION;
  83. }
  84. }
  85. /**
  86. * Creates condition trees where the arguments are disjointed together.
  87. */
  88. public static class DisjunctionFactory extends ConditionTreeFactory {
  89. @Override
  90. public ConditionTree getConditionTree(final int args) {
  91. return ConditionTree.createDisjunction(args);
  92. }
  93. @Override
  94. public ConditionTreeFactoryType getType() {
  95. return ConditionTreeFactoryType.DISJUNCTION;
  96. }
  97. }
  98. /**
  99. * Creates condition trees with a custom structure.
  100. */
  101. public static class CustomFactory extends ConditionTreeFactory {
  102. /** The condition tree to use. */
  103. protected final ConditionTree tree;
  104. /**
  105. * Creates a new CustomFactory for the specified tree.
  106. *
  107. * @param tree The tree to use
  108. */
  109. public CustomFactory(final ConditionTree tree) {
  110. this.tree = tree;
  111. }
  112. @Override
  113. public ConditionTree getConditionTree(final int args) {
  114. return tree;
  115. }
  116. @Override
  117. public ConditionTreeFactoryType getType() {
  118. return ConditionTreeFactoryType.CUSTOM;
  119. }
  120. }
  121. }