Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ActionComponentChain.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2006-2008 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. import com.dmdirc.actions.interfaces.ActionComponent;
  24. import com.dmdirc.Precondition;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. /**
  28. * An action component chain supports chaining of multiple action components
  29. * together.
  30. *
  31. * @author chris
  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<ActionComponent>();
  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. */
  45. public ActionComponentChain(final Class source, final String chain) {
  46. Class current = source;
  47. for (String componentName : chain.split("\\.")) {
  48. final ActionComponent component = ActionManager.getActionComponent(componentName);
  49. if (component == null) {
  50. throw new IllegalArgumentException("Component " + componentName
  51. + " not found");
  52. } else if (component.appliesTo() == current) {
  53. components.add(component);
  54. current = component.getType();
  55. } else {
  56. throw new IllegalArgumentException("Component " + componentName
  57. + " cannot be applied to " + current.getName());
  58. }
  59. }
  60. }
  61. /** {@inheritDoc} */
  62. @Override
  63. public Object get(final Object argument) {
  64. Object res = argument;
  65. for (ActionComponent component : components) {
  66. res = component.get(res);
  67. }
  68. return res;
  69. }
  70. /** {@inheritDoc} */
  71. @Precondition("This component chain has one or more components")
  72. @Override
  73. public Class appliesTo() {
  74. assert(!components.isEmpty());
  75. return components.get(0).appliesTo();
  76. }
  77. /** {@inheritDoc} */
  78. @Precondition("This component chain has one or more components")
  79. @Override
  80. public Class getType() {
  81. assert(!components.isEmpty());
  82. return components.get(components.size() - 1).getType();
  83. }
  84. /** {@inheritDoc} */
  85. @Precondition("This component chain has one or more components")
  86. @Override
  87. public String getName() {
  88. assert(!components.isEmpty());
  89. final StringBuilder name = new StringBuilder();
  90. for (ActionComponent component : components) {
  91. name.append("'s ");
  92. name.append(component.getName());
  93. }
  94. return name.substring(3);
  95. }
  96. /** {@inheritDoc} */
  97. @Override
  98. @Precondition("This component chain has one or more components")
  99. public String toString() {
  100. assert(!components.isEmpty());
  101. final StringBuilder name = new StringBuilder();
  102. for (ActionComponent component : components) {
  103. name.append('.');
  104. name.append(component.toString());
  105. }
  106. return name.substring(1);
  107. }
  108. }