File Source: ActionComponentChain.java
/*
P/P * Method: com.dmdirc.actions.ActionComponentChain__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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
23 package com.dmdirc.actions;
24
25 import com.dmdirc.actions.interfaces.ActionComponent;
26 import com.dmdirc.Precondition;
27 import com.dmdirc.logger.Logger;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 /**
33 * An action component chain supports chaining of multiple action components
34 * together.
35 *
36 * @author chris
37 */
38 public class ActionComponentChain implements ActionComponent {
39
40 /**
41 * A list of components in this chain.
42 */
43 private final List<ActionComponent> components = new ArrayList<ActionComponent>();
44
45 /**
46 * Creates a new component chain from the specified text representation.
47 * Chains are separated with full stops (.).
48 *
49 * @param source The class that this chain needs to start with
50 * @param chain The textual representation of the chain
51 */
/*
P/P * Method: void com.dmdirc.actions.ActionComponentChain(Class, String)
*
* Preconditions:
* chain != null
*
* Postconditions:
* this.components == &new ArrayList(ActionComponentChain#1)
* new ArrayList(ActionComponentChain#1) num objects == 1
*/
52 public ActionComponentChain(final Class source, final String chain) {
53 Class current = source;
54
55 for (String componentName : chain.split("\\.")) {
56 final ActionComponent component = ActionManager.getActionComponent(componentName);
57
58 if (component == null) {
59 throw new IllegalArgumentException("Component " + componentName
60 + " not found");
61 } else if (component.appliesTo() == current) {
62 components.add(component);
63 current = component.getType();
64 } else {
65 throw new IllegalArgumentException("Component " + componentName
66 + " cannot be applied to " + current.getName());
67 }
68 }
69
70 }
71
72 /** {@inheritDoc} */
73 @Override
74 public Object get(final Object argument) {
/*
P/P * Method: Object get(Object)
*
* Preconditions:
* this.components != null
*
* Presumptions:
* java.util.Iterator:next(...)@77 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@77: {0}, {1}
*/
75 Object res = argument;
76
77 for (ActionComponent component : components) {
78 res = component.get(res);
79 }
80
81 return res;
82 }
83
84 /** {@inheritDoc} */
85 @Precondition("This component chain has one or more components")
86 @Override
87 public Class appliesTo() {
/*
P/P * Method: Class appliesTo()
*
* Preconditions:
* this.components != null
*
* Presumptions:
* java.util.List:get(...)@90 != null
*
* Postconditions:
* init'ed(return_value)
*/
88 Logger.assertTrue(!components.isEmpty());
89
90 return components.get(0).appliesTo();
91 }
92
93 /** {@inheritDoc} */
94 @Precondition("This component chain has one or more components")
95 @Override
96 public Class getType() {
/*
P/P * Method: Class getType()
*
* Preconditions:
* this.components != null
*
* Presumptions:
* java.util.List:get(...)@99 != null
* java.util.List:size(...)@99 >= -231+1
*
* Postconditions:
* init'ed(return_value)
*/
97 Logger.assertTrue(!components.isEmpty());
98
99 return components.get(components.size() - 1).getType();
100 }
101
102 /** {@inheritDoc} */
103 @Precondition("This component chain has one or more components")
104 @Override
105 public String getName() {
/*
P/P * Method: String getName()
*
* Preconditions:
* this.components != null
*
* Presumptions:
* java.util.Iterator:next(...)@110 != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@110: {0}, {1}
*/
106 Logger.assertTrue(!components.isEmpty());
107
108 final StringBuilder name = new StringBuilder();
109
110 for (ActionComponent component : components) {
111 name.append("'s ");
112 name.append(component.getName());
113 }
114
115 return name.substring(3);
116 }
117
118 /** {@inheritDoc} */
119 @Override
120 @Precondition("This component chain has one or more components")
121 public String toString() {
/*
P/P * Method: String toString()
*
* Preconditions:
* this.components != null
*
* Presumptions:
* java.util.Iterator:next(...)@126 != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@126: {0}, {1}
*/
122 Logger.assertTrue(!components.isEmpty());
123
124 final StringBuilder name = new StringBuilder();
125
126 for (ActionComponent component : components) {
127 name.append('.');
128 name.append(component.toString());
129 }
130
131 return name.substring(1);
132 }
133
134 }
SofCheck Inspector Build Version : 2.17854
| ActionComponentChain.java |
2009-Jun-25 01:54:24 |
| ActionComponentChain.class |
2009-Sep-02 17:04:13 |