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.

ActionComponentChain.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2006-2013 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. import com.dmdirc.Precondition;
  24. import com.dmdirc.interfaces.ActionController;
  25. import com.dmdirc.interfaces.actions.ActionComponent;
  26. import com.dmdirc.logger.Logger;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. /**
  30. * An action component chain supports chaining of multiple action components
  31. * together.
  32. */
  33. public class ActionComponentChain implements ActionComponent {
  34. /**
  35. * A list of components in this chain.
  36. */
  37. private final List<ActionComponent> components = new ArrayList<>();
  38. /**
  39. * Creates a new component chain from the specified text representation.
  40. * Chains are separated with full stops (.).
  41. *
  42. * @param source The class that this chain needs to start with
  43. * @param chain The textual representation of the chain
  44. * @deprecated Should specify an action controller.
  45. */
  46. @Deprecated
  47. public ActionComponentChain(final Class<?> source, final String chain) {
  48. this(source, chain, ActionManager.getActionManager());
  49. }
  50. /**
  51. * Creates a new component chain from the specified text representation.
  52. * Chains are separated with full stops (.).
  53. *
  54. * @param source The class that this chain needs to start with
  55. * @param chain The textual representation of the chain
  56. * @param manager The action manager to use to look up components
  57. */
  58. public ActionComponentChain(final Class<?> source, final String chain,
  59. final ActionController manager) {
  60. Class<?> current = source;
  61. for (String componentName : chain.split("\\.")) {
  62. final ActionComponent component = manager.getComponent(componentName);
  63. if (component == null) {
  64. throw new IllegalArgumentException("Component " + componentName
  65. + " not found");
  66. } else if (component.appliesTo() == current) {
  67. components.add(component);
  68. current = component.getType();
  69. } else {
  70. throw new IllegalArgumentException("Component " + componentName
  71. + " cannot be applied to " + current.getName());
  72. }
  73. }
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public Object get(final Object arg) {
  78. Object res = arg;
  79. for (ActionComponent component : components) {
  80. if (res == null) {
  81. return null;
  82. }
  83. res = component.get(res);
  84. }
  85. return res;
  86. }
  87. /** {@inheritDoc} */
  88. @Precondition("This component chain has one or more components")
  89. @Override
  90. public Class<?> appliesTo() {
  91. Logger.assertTrue(!components.isEmpty());
  92. return components.get(0).appliesTo();
  93. }
  94. /** {@inheritDoc} */
  95. @Precondition("This component chain has one or more components")
  96. @Override
  97. public Class<?> getType() {
  98. Logger.assertTrue(!components.isEmpty());
  99. return components.get(components.size() - 1).getType();
  100. }
  101. /** {@inheritDoc} */
  102. @Precondition("This component chain has one or more components")
  103. @Override
  104. public String getName() {
  105. Logger.assertTrue(!components.isEmpty());
  106. final StringBuilder name = new StringBuilder();
  107. for (ActionComponent component : components) {
  108. name.append("'s ");
  109. name.append(component.getName());
  110. }
  111. return name.substring(3);
  112. }
  113. /** {@inheritDoc} */
  114. @Override
  115. @Precondition("This component chain has one or more components")
  116. public String toString() {
  117. Logger.assertTrue(!components.isEmpty());
  118. final StringBuilder name = new StringBuilder();
  119. for (ActionComponent component : components) {
  120. name.append('.');
  121. name.append(component.toString());
  122. }
  123. return name.substring(1);
  124. }
  125. /** {@inheritDoc} */
  126. @Override
  127. public String name() {
  128. return toString();
  129. }
  130. /**
  131. * Determines if any components in this chain require a server to have
  132. * an established connection in order to function.
  133. *
  134. * @since 0.6.4
  135. * @return True iff at least one component requires a connection
  136. */
  137. public boolean requiresConnection() {
  138. boolean res = false;
  139. for (ActionComponent component : components) {
  140. try {
  141. final ComponentOptions options = component.getClass()
  142. .getMethod("get", Object.class).getAnnotation(ComponentOptions.class);
  143. if (options != null) {
  144. res |= options.requireConnected();
  145. }
  146. } catch (NoSuchMethodException ex) {
  147. // Do nothing
  148. }
  149. }
  150. return res;
  151. }
  152. }