File Source: Alias.java
/*
P/P * Method: com.dmdirc.actions.wrappers.Alias__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.wrappers;
24
25 import com.dmdirc.actions.Action;
26 import com.dmdirc.actions.ActionCondition;
27 import com.dmdirc.actions.interfaces.ActionType;
28 import com.dmdirc.actions.CoreActionComparison;
29 import com.dmdirc.actions.CoreActionComponent;
30 import com.dmdirc.actions.CoreActionType;
31
32 import java.io.Serializable;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36
37 /**
38 * Actions alias wrapper.
39 */
40 public final class Alias implements Serializable {
41
42 /**
43 * A version number for this class. It should be changed whenever the class
44 * structure is changed (or anything else that would prevent serialized
45 * objects being unserialized with the new class).
46 */
47 private static final long serialVersionUID = 1;
48
49 /** Alias command. */
50 private String command;
51
52 /** Alias arguments. */
53 private List<ActionCondition> arguments;
54
55 /** Alias response. */
56 private String[] response;
57
58 /**
59 * Creates a new Alias wrapper.
60 *
61 * @param command Alias command
62 */
/*
P/P * Method: void com.dmdirc.actions.wrappers.Alias(String)
*
* Postconditions:
* this.arguments == &new ArrayList(Alias#1)
* this.command == command
* init'ed(this.command)
* this.response == &new String[](Alias#3)
* new ArrayList(Alias#1) num objects == 1
* new String[](Alias#3) num objects == 1
* this.response.length == 1
* this.response[0] == &""
*/
63 public Alias(final String command) {
64 this.command = command;
65 this.arguments = new ArrayList<ActionCondition>();
66 this.arguments.add(new ActionCondition(1, CoreActionComponent.STRING_STRING,
67 CoreActionComparison.STRING_EQUALS, command));
68 this.response = new String[]{"", };
69 }
70
71 /**
72 * Wraps an existing Action in an Alias.
73 *
74 * @param command Alias command
75 * @param arguments List of arguments for the alias
76 * @param response Response for the alias
77 */
78 public Alias(final String command, final List<ActionCondition> arguments,
/*
P/P * Method: void com.dmdirc.actions.wrappers.Alias(String, List, String[])
*
* Preconditions:
* response != null
* (soft) init'ed(response[...])
*
* Postconditions:
* this.arguments == &new ArrayList(Alias#1)
* this.command == command
* init'ed(this.command)
* this.response == &new String[](Alias#2)
* new ArrayList(Alias#1) num objects == 1
* new String[](Alias#2) num objects == 1
* this.response.length == response.length
* init'ed(this.response.length)
* this.response[...] == One-of{response[...], undefined}
*/
79 final String[] response) {
80 this.command = command;
81 this.arguments = new ArrayList<ActionCondition>(arguments);
82 this.response = response.clone();
83 }
84
85 /**
86 * Returns the aliases command.
87 *
88 * @return Aliases command
89 */
90 public String getCommand() {
/*
P/P * Method: String getCommand()
*
* Preconditions:
* init'ed(this.command)
*
* Postconditions:
* return_value == this.command
* init'ed(return_value)
*/
91 return command;
92 }
93
94 /**
95 * Sets the aliases command.
96 *
97 * @param command Command to give the alias
98 */
99 public void setCommand(final String command) {
/*
P/P * Method: void setCommand(String)
*
* Preconditions:
* this.command != null
* (soft) this.arguments != null
*
* Presumptions:
* java.util.List:get(...)@105 != null
* java.util.List:get(...)@108 != null
*
* Postconditions:
* this.command == One-of{old this.command, command}
* init'ed(this.command)
*
* Test Vectors:
* argument.comparison@105: Addr_Set{&com.dmdirc.actions.CoreActionComparison__static_init.new CoreActionComparison$2(CoreActionComparison__static_init#2)}, Inverse{&com.dmdirc.actions.CoreActionComparison__static_init.new CoreActionComparison$2(CoreActionComparison__static_init#2)}
* java.lang.String:equals(...)@100: {1}, {0}
*/
100 if (!this.command.equals(command)) {
101 this.command = command;
102
103 ActionCondition argument;
104
105 argument = arguments.get(0);
106
107 if (argument.getComparison() != CoreActionComparison.STRING_EQUALS) {
108 argument = arguments.get(1);
109 }
110
111 argument.setTarget(command);
112 }
113 }
114
115 /**
116 * Returns the aliases name.
117 *
118 * @return Aliases name
119 */
120 public String getName() {
/*
P/P * Method: String getName()
*
* Preconditions:
* this.arguments != null
* init'ed(this.command)
*
* Presumptions:
* condition.comparison != null
*
* Postconditions:
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* return_value in Addr_Set{&java.lang.StringBuilder:toString(...),&java.lang.StringBuilder:toString(...)}
*
* Test Vectors:
* java.lang.Object:equals(...)@126: {0}, {1}
* java.lang.Object:equals(...)@128: {0}, {1}
* java.lang.Object:equals(...)@130: {0}, {1}
*/
121 final ActionCondition condition = getArgsArgument();
122 if (condition == null) {
123 return command + "-Any";
124 } else {
125 final String comparison;
126 if (condition.getComparison().equals(CoreActionComparison.INT_EQUALS)) {
127 comparison = "equals";
128 } else if (condition.getComparison().equals(CoreActionComparison.INT_GREATER)) {
129 comparison = "greater";
130 } else if (condition.getComparison().equals(CoreActionComparison.INT_LESS)) {
131 comparison = "less";
132 } else {
133 comparison = condition.getComparison().toString();
134 }
135 return command + "-" + comparison + "-" + condition.getTarget();
136 }
137 }
138
139 /**
140 * Gets the aliases arguments.
141 *
142 * @return Argument list
143 */
144 public List<ActionCondition> getArguments() {
/*
P/P * Method: List getArguments()
*
* Preconditions:
* init'ed(this.arguments)
*
* Postconditions:
* return_value == &new ArrayList(getArguments#1)
* new ArrayList(getArguments#1) num objects == 1
*/
145 return new ArrayList<ActionCondition>(arguments);
146 }
147
148 /**
149 * Gets the aliases number of arguments argument.
150 *
151 * @return Number of arguments ActionCondition or null
152 */
153 public ActionCondition getArgsArgument() {
154 ActionCondition argument;
155
/*
P/P * Method: ActionCondition getArgsArgument()
*
* Preconditions:
* this.arguments != null
*
* Presumptions:
* java.util.List:get(...)@156 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* argument.comparison@156: Inverse{&com.dmdirc.actions.CoreActionComparison__static_init.new CoreActionComparison$2(CoreActionComparison__static_init#2)}, Addr_Set{&com.dmdirc.actions.CoreActionComparison__static_init.new CoreActionComparison$2(CoreActionComparison__static_init#2)}
* java.util.List:size(...)@159: {-231..1}, {2..232-1}
*/
156 argument = arguments.get(0);
157
158 if (argument.getComparison() == CoreActionComparison.STRING_EQUALS) {
159 if (arguments.size() > 1) {
160 argument = arguments.get(1);
161 } else {
162 argument = null;
163 }
164 }
165
166 return argument;
167 }
168
169 /**
170 * Sets the aliases arguments.
171 *
172 * @param arguments A new list of arguments to set
173 */
174 public void setArguments(final List<ActionCondition> arguments) {
/*
P/P * Method: void setArguments(List)
*
* Preconditions:
* this.arguments != null
*
* Postconditions:
* this.arguments == One-of{old this.arguments, &new ArrayList(setArguments#1)}
* this.arguments != null
* new ArrayList(setArguments#1) num objects <= 1
*
* Test Vectors:
* java.lang.Object:equals(...)@175: {1}, {0}
*/
175 if (!this.arguments.equals(arguments)) {
176 this.arguments = new ArrayList<ActionCondition>(arguments);
177 }
178 }
179
180 /**
181 * Gets the aliases response.
182 *
183 * @return Response
184 */
185 public String[] getResponse() {
/*
P/P * Method: String[] getResponse()
*
* Preconditions:
* this.response != null
* (soft) init'ed(this.response[...])
*
* Postconditions:
* return_value == &new String[](getResponse#1)
* new String[](getResponse#1) num objects == 1
* return_value.length == this.response.length
* init'ed(return_value.length)
* return_value[...] == One-of{this.response[...], undefined}
*/
186 return response.clone();
187 }
188
189 /**
190 * Sets the aliases response.
191 *
192 * @param response New Response
193 */
194 public void setResponse(final String[] response) {
/*
P/P * Method: void setResponse(String[])
*
* Preconditions:
* init'ed(this.response)
* (soft) response != null
* (soft) init'ed(response[...])
*
* Postconditions:
* this.response == One-of{old this.response, &new String[](setResponse#1)}
* init'ed(this.response)
* new String[](setResponse#1) num objects <= 1
* new String[](setResponse#1).length == response.length
* init'ed(new String[](setResponse#1).length)
* possibly_updated(new String[](setResponse#1)[...])
*
* Test Vectors:
* java.util.Arrays:equals(...)@195: {1}, {0}
*/
195 if (!Arrays.equals(this.response, response)) {
196 this.response = response.clone();
197 }
198 }
199
200 /**
201 * Updates this alias with the details of another alias.
202 *
203 * @param alias Alias to retrieve details from
204 */
205 public void update(final Alias alias) {
/*
P/P * Method: void update(Alias)
*
* Preconditions:
* alias != null
* init'ed(alias.arguments)
* init'ed(alias.command)
* alias.response != null
* this.arguments != null
* this.command != null
* init'ed(this.response)
* (soft) init'ed(alias.response[...])
*
* Postconditions:
* this.arguments == One-of{old this.arguments, &new ArrayList(setArguments#1)}
* this.arguments != null
* this.command == One-of{old this.command, alias.command}
* init'ed(this.command)
* this.response == One-of{old this.response, &new String[](setResponse#1)}
* init'ed(this.response)
* new ArrayList(setArguments#1) num objects <= 1
* new String[](setResponse#1) num objects <= 1
* new String[](setResponse#1).length == alias.response.length
* init'ed(new String[](setResponse#1).length)
* ...
*/
206 setArguments(alias.getArguments());
207 setCommand(alias.getCommand());
208 setResponse(alias.getResponse());
209 }
210
211 /**
212 * Checks if the specified alias matches this one
213 *
214 * @param alias Alias to check a match with
215 *
216 * @return true iif the alias matches this one
217 */
218 public boolean matches(final Alias alias) {
/*
P/P * Method: bool matches(Alias)
*
* Preconditions:
* alias != null
* alias.command != null
* init'ed(this.command)
* (soft) init'ed(alias.arguments)
* (soft) init'ed(this.arguments)
*
* Postconditions:
* init'ed(return_value)
*/
219 return alias.getCommand().equalsIgnoreCase(command)
220 && alias.getArguments().equals(arguments);
221 }
222
223 /**
224 * Creates an action corresponding to this alias.
225 *
226 * @return A new action for this alias.
227 */
228 public Action createAction() {
/*
P/P * Method: Action createAction()
*
* Preconditions:
* init'ed(com/dmdirc/actions/wrappers/AliasWrapper.me)
* this.arguments != null
* init'ed(this.command)
* this.response != null
* (soft) init'ed(this.response[...])
*
* Postconditions:
* com/dmdirc/actions/wrappers/AliasWrapper.me == One-of{old com/dmdirc/actions/wrappers/AliasWrapper.me, &new AliasWrapper(getAliasWrapper#1)}
* com/dmdirc/actions/wrappers/AliasWrapper.me != null
* return_value == &new Action(createAction#1)
* new Action(createAction#1) num objects == 1
* new AliasWrapper(getAliasWrapper#1) num objects <= 1
* new ArrayList(ActionGroup#1) num objects == new AliasWrapper(getAliasWrapper#1) num objects
* new ArrayList(AliasWrapper#1) num objects == new AliasWrapper(getAliasWrapper#1) num objects
* new HashMap(ActionGroup#2) num objects == new AliasWrapper(getAliasWrapper#1) num objects
* new AliasWrapper(getAliasWrapper#1).actions == &new ArrayList(ActionGroup#1)
* new AliasWrapper(getAliasWrapper#1).aliases == &new ArrayList(AliasWrapper#1)
* ...
*/
229 return new Action(
230 AliasWrapper.getAliasWrapper().getName(),
231 getName(),
232 new ActionType[] {CoreActionType.UNKNOWN_COMMAND, },
233 getResponse(),
234 getArguments(),
235 "");
236 }
237
238 /** {@inheritDoc} */
239 @Override
240 public String toString() {
/*
P/P * Method: String toString()
*
* Preconditions:
* this.arguments != null
* init'ed(this.command)
* init'ed(this.response)
*
* Postconditions:
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* return_value == &java.lang.StringBuilder:toString(...)
*/
241 return "[name=aliases/" + getName() + ", triggers="
242 + "[UNKNOWN_COMMAND], response="
243 + Arrays.toString(response) + ", "
244 + arguments + ", format='']";
245 }
246 }
SofCheck Inspector Build Version : 2.17854
| Alias.java |
2009-Jun-25 01:54:24 |
| Alias.class |
2009-Sep-02 17:04:14 |