您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ActionComponentChain.java 5.7KB

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