1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.plugin.charts.params; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.Iterator; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
|
28 |
|
import com.xpn.xwiki.plugin.charts.exceptions.InvalidArgumentException; |
29 |
|
import com.xpn.xwiki.plugin.charts.exceptions.MissingArgumentException; |
30 |
|
import com.xpn.xwiki.plugin.charts.exceptions.ParamException; |
31 |
|
|
|
|
| 0% |
Uncovered Elements: 113 (113) |
Complexity: 40 |
Complexity Density: 0.56 |
|
32 |
|
public abstract class AbstractChartParam implements ChartParam |
33 |
|
{ |
34 |
|
protected String name; |
35 |
|
|
36 |
|
protected boolean optional; |
37 |
|
|
38 |
|
public static final String MAP_SEPARATOR = ";"; |
39 |
|
|
40 |
|
public static final String MAP_ASSIGNMENT = ":"; |
41 |
|
|
42 |
|
public static final String LIST_SEPARATOR = ","; |
43 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
44 |
0 |
public AbstractChartParam(String name)... |
45 |
|
{ |
46 |
0 |
this(name, true); |
47 |
|
} |
48 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
49 |
0 |
public AbstractChartParam(String name, boolean optional)... |
50 |
|
{ |
51 |
0 |
this.name = name; |
52 |
0 |
this.optional = optional; |
53 |
|
} |
54 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
55 |
0 |
@Override... |
56 |
|
public String getName() |
57 |
|
{ |
58 |
0 |
return name; |
59 |
|
} |
60 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
61 |
0 |
@Override... |
62 |
|
public boolean isOptional() |
63 |
|
{ |
64 |
0 |
return optional; |
65 |
|
} |
66 |
|
|
67 |
|
@Override |
68 |
|
public abstract Class getType(); |
69 |
|
|
70 |
|
@Override |
71 |
|
public abstract Object convert(String value) throws ParamException; |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
73 |
0 |
@Override... |
74 |
|
public boolean equals(Object obj) |
75 |
|
{ |
76 |
0 |
if (obj != null && obj instanceof ChartParam) { |
77 |
0 |
return getName().equals(((ChartParam) obj).getName()); |
78 |
|
} else { |
79 |
0 |
return false; |
80 |
|
} |
81 |
|
} |
82 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
0 |
@Override... |
84 |
|
public int hashCode() |
85 |
|
{ |
86 |
0 |
return getName().hashCode(); |
87 |
|
} |
88 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
89 |
0 |
@Override... |
90 |
|
public String toString() |
91 |
|
{ |
92 |
0 |
return getName(); |
93 |
|
} |
94 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
95 |
0 |
protected String getStringArg(Map map, String name) throws MissingArgumentException... |
96 |
|
{ |
97 |
0 |
String value = (String) map.get(name); |
98 |
0 |
if (value != null) { |
99 |
0 |
return value; |
100 |
|
} else { |
101 |
0 |
throw new MissingArgumentException("Invalid value for the parameter " + getName() + ": Argument " + name |
102 |
|
+ " is mandatory."); |
103 |
|
} |
104 |
|
} |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
0 |
protected String getStringOptionalArg(Map map, String name)... |
107 |
|
{ |
108 |
0 |
return (String) map.get(name); |
109 |
|
} |
110 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
111 |
0 |
protected int getIntArg(Map map, String name) throws MissingArgumentException, InvalidArgumentException... |
112 |
|
{ |
113 |
0 |
String value = getStringArg(map, name); |
114 |
0 |
try { |
115 |
0 |
return Integer.parseInt(value); |
116 |
|
} catch (NumberFormatException e) { |
117 |
0 |
throw new InvalidArgumentException("Invalid value for the parameter " + getName() |
118 |
|
+ ": Non-integer value for the " + name + " argument."); |
119 |
|
} |
120 |
|
} |
121 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
122 |
0 |
protected float getFloatArg(Map map, String name) throws MissingArgumentException, InvalidArgumentException... |
123 |
|
{ |
124 |
0 |
String value = getStringArg(map, name); |
125 |
0 |
try { |
126 |
0 |
return Float.parseFloat(value); |
127 |
|
} catch (NumberFormatException e) { |
128 |
0 |
throw new InvalidArgumentException("Invalid value for the parameter " + getName() |
129 |
|
+ ": Non-float value for the " + name + " argument."); |
130 |
|
} |
131 |
|
} |
132 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
133 |
0 |
protected double getDoubleArg(Map map, String name) throws MissingArgumentException, InvalidArgumentException... |
134 |
|
{ |
135 |
0 |
String value = getStringArg(map, name); |
136 |
0 |
try { |
137 |
0 |
return Double.parseDouble(value); |
138 |
|
} catch (NumberFormatException e) { |
139 |
0 |
throw new InvalidArgumentException("Invalid value for the parameter " + getName() |
140 |
|
+ ": Non-double value for the " + name + " argument."); |
141 |
|
} |
142 |
|
} |
143 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
144 |
0 |
protected Object getChoiceArg(Map map, String name, Map choices) throws MissingArgumentException,... |
145 |
|
InvalidArgumentException |
146 |
|
{ |
147 |
0 |
String value = getStringArg(map, name); |
148 |
0 |
Object obj = choices.get(value); |
149 |
0 |
if (obj != null) { |
150 |
0 |
return obj; |
151 |
|
} else { |
152 |
0 |
throw new InvalidArgumentException("Invalid value for the parameter " + getName() |
153 |
|
+ ": The accepted values for the " + name + "argument are " + choices.keySet() + "; encountered: " |
154 |
|
+ value); |
155 |
|
} |
156 |
|
} |
157 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
158 |
0 |
protected List getListArg(Map map, String name) throws MissingArgumentException... |
159 |
|
{ |
160 |
0 |
return parseList(getStringArg(map, name)); |
161 |
|
} |
162 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 6 |
Complexity Density: 0.6 |
|
163 |
0 |
protected Map parseMap(String value) throws InvalidArgumentException... |
164 |
|
{ |
165 |
0 |
String[] args = value.split(MAP_SEPARATOR); |
166 |
0 |
if (args.length == 0 || (args.length == 1 && args[0].length() == 0)) { |
167 |
0 |
return new HashMap(0); |
168 |
|
} |
169 |
0 |
Map result = new HashMap(args.length); |
170 |
0 |
for (int i = 0; i < args.length; i++) { |
171 |
0 |
String[] split = args[i].split(MAP_ASSIGNMENT); |
172 |
0 |
if (split.length != 2) { |
173 |
0 |
throw new InvalidArgumentException("Invalid value for the parameter " + getName() + ": " + "name" |
174 |
|
+ MAP_ASSIGNMENT + "value \"" + MAP_SEPARATOR + "\"-separated list expected"); |
175 |
|
} |
176 |
0 |
result.put(split[0].trim(), split[1].trim()); |
177 |
|
} |
178 |
0 |
return result; |
179 |
|
} |
180 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
181 |
0 |
protected Map parseMap(String value, int expectedTokenCount) throws InvalidArgumentException... |
182 |
|
{ |
183 |
0 |
Map result = parseMap(value); |
184 |
0 |
if (result.size() != expectedTokenCount) { |
185 |
0 |
throw new InvalidArgumentException("Invalid number of arguments given to the " + getName() |
186 |
|
+ " parameter; expected:" + expectedTokenCount); |
187 |
|
} |
188 |
0 |
return result; |
189 |
|
} |
190 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 5 |
Complexity Density: 0.71 |
|
191 |
0 |
protected List parseList(String value)... |
192 |
|
{ |
193 |
0 |
String[] args = value.split(LIST_SEPARATOR); |
194 |
0 |
if (args.length == 0 || (args.length == 1 && args[0].length() == 0)) { |
195 |
0 |
return new ArrayList(0); |
196 |
|
} |
197 |
0 |
List result = new ArrayList(args.length); |
198 |
0 |
for (int i = 0; i < args.length; i++) { |
199 |
0 |
result.add(args[i].trim()); |
200 |
|
} |
201 |
0 |
return result; |
202 |
|
} |
203 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
204 |
0 |
protected List toFloatList(List list) throws InvalidArgumentException... |
205 |
|
{ |
206 |
0 |
List result = new ArrayList(list.size()); |
207 |
0 |
Iterator it = list.iterator(); |
208 |
0 |
while (it.hasNext()) { |
209 |
0 |
String value = (String) it.next(); |
210 |
0 |
try { |
211 |
0 |
result.add(new Float(value)); |
212 |
|
} catch (NumberFormatException e) { |
213 |
0 |
throw new InvalidArgumentException("Invalid value for the parameter " + getName() |
214 |
|
+ ": Non-float value for " + name); |
215 |
|
} |
216 |
|
} |
217 |
0 |
return result; |
218 |
|
} |
219 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
220 |
0 |
protected float[] toFloatArray(List list) throws InvalidArgumentException... |
221 |
|
{ |
222 |
0 |
float[] result = new float[list.size()]; |
223 |
0 |
Iterator it = list.iterator(); |
224 |
0 |
int i = 0; |
225 |
0 |
while (it.hasNext()) { |
226 |
0 |
String value = (String) it.next(); |
227 |
0 |
try { |
228 |
0 |
result[i] = Float.parseFloat(value); |
229 |
|
} catch (NumberFormatException e) { |
230 |
0 |
throw new InvalidArgumentException("Invalid value for the parameter " + getName() |
231 |
|
+ ": Non-float value for " + name); |
232 |
|
} |
233 |
0 |
i++; |
234 |
|
} |
235 |
0 |
return result; |
236 |
|
} |
237 |
|
} |