1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.charts.params

File AbstractChartParam.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

22
72
19
1
237
190
40
0.56
3.79
19
2.11

Classes

Class Line # Actions
AbstractChartParam 32 72 0% 40 113
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   
 
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   
 
44  0 toggle public AbstractChartParam(String name)
45    {
46  0 this(name, true);
47    }
48   
 
49  0 toggle public AbstractChartParam(String name, boolean optional)
50    {
51  0 this.name = name;
52  0 this.optional = optional;
53    }
54   
 
55  0 toggle @Override
56    public String getName()
57    {
58  0 return name;
59    }
60   
 
61  0 toggle @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   
 
73  0 toggle @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   
 
83  0 toggle @Override
84    public int hashCode()
85    {
86  0 return getName().hashCode();
87    }
88   
 
89  0 toggle @Override
90    public String toString()
91    {
92  0 return getName();
93    }
94   
 
95  0 toggle 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   
 
106  0 toggle protected String getStringOptionalArg(Map map, String name)
107    {
108  0 return (String) map.get(name);
109    }
110   
 
111  0 toggle 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   
 
122  0 toggle 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   
 
133  0 toggle 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   
 
144  0 toggle 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   
 
158  0 toggle protected List getListArg(Map map, String name) throws MissingArgumentException
159    {
160  0 return parseList(getStringArg(map, name));
161    }
162   
 
163  0 toggle 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   
 
181  0 toggle 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   
 
191  0 toggle 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   
 
204  0 toggle 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   
 
220  0 toggle 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    }