123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- /*
- * Copyright (c) 2006-2013 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.actions;
-
- import com.dmdirc.Precondition;
- import com.dmdirc.interfaces.ActionController;
- import com.dmdirc.interfaces.actions.ActionComponent;
- import com.dmdirc.logger.Logger;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * An action component chain supports chaining of multiple action components
- * together.
- */
- public class ActionComponentChain implements ActionComponent {
-
- /**
- * A list of components in this chain.
- */
- private final List<ActionComponent> components = new ArrayList<>();
-
- /**
- * Creates a new component chain from the specified text representation.
- * Chains are separated with full stops (.).
- *
- * @param source The class that this chain needs to start with
- * @param chain The textual representation of the chain
- * @deprecated Should specify an action controller.
- */
- @Deprecated
- public ActionComponentChain(final Class<?> source, final String chain) {
- this(source, chain, ActionManager.getActionManager());
- }
-
- /**
- * Creates a new component chain from the specified text representation.
- * Chains are separated with full stops (.).
- *
- * @param source The class that this chain needs to start with
- * @param chain The textual representation of the chain
- * @param manager The action manager to use to look up components
- */
- public ActionComponentChain(final Class<?> source, final String chain,
- final ActionController manager) {
- Class<?> current = source;
-
- for (String componentName : chain.split("\\.")) {
- final ActionComponent component = manager.getComponent(componentName);
-
- if (component == null) {
- throw new IllegalArgumentException("Component " + componentName
- + " not found");
- } else if (component.appliesTo() == current) {
- components.add(component);
- current = component.getType();
- } else {
- throw new IllegalArgumentException("Component " + componentName
- + " cannot be applied to " + current.getName());
- }
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public Object get(final Object arg) {
- Object res = arg;
-
- for (ActionComponent component : components) {
- if (res == null) {
- return null;
- }
-
- res = component.get(res);
- }
-
- return res;
- }
-
- /** {@inheritDoc} */
- @Precondition("This component chain has one or more components")
- @Override
- public Class<?> appliesTo() {
- Logger.assertTrue(!components.isEmpty());
-
- return components.get(0).appliesTo();
- }
-
- /** {@inheritDoc} */
- @Precondition("This component chain has one or more components")
- @Override
- public Class<?> getType() {
- Logger.assertTrue(!components.isEmpty());
-
- return components.get(components.size() - 1).getType();
- }
-
- /** {@inheritDoc} */
- @Precondition("This component chain has one or more components")
- @Override
- public String getName() {
- Logger.assertTrue(!components.isEmpty());
-
- final StringBuilder name = new StringBuilder();
-
- for (ActionComponent component : components) {
- name.append("'s ");
- name.append(component.getName());
- }
-
- return name.substring(3);
- }
-
- /** {@inheritDoc} */
- @Override
- @Precondition("This component chain has one or more components")
- public String toString() {
- Logger.assertTrue(!components.isEmpty());
-
- final StringBuilder name = new StringBuilder();
-
- for (ActionComponent component : components) {
- name.append('.');
- name.append(component.toString());
- }
-
- return name.substring(1);
- }
-
- /** {@inheritDoc} */
- @Override
- public String name() {
- return toString();
- }
-
- /**
- * Determines if any components in this chain require a server to have
- * an established connection in order to function.
- *
- * @since 0.6.4
- * @return True iff at least one component requires a connection
- */
- public boolean requiresConnection() {
- boolean res = false;
-
- for (ActionComponent component : components) {
- try {
- final ComponentOptions options = component.getClass()
- .getMethod("get", Object.class).getAnnotation(ComponentOptions.class);
- if (options != null) {
- res |= options.requireConnected();
- }
- } catch (NoSuchMethodException ex) {
- // Do nothing
- }
- }
-
- return res;
- }
-
- }
|