View Javadoc

1   /*
2    * $Id: $
3    * Copyright (C) 2007 - 2010 Roland Krueger
4    * Created on 02.03.2010
5    *
6    * Author: Roland Krueger (www.rolandkrueger.info)
7    *
8    * This file is part of RoKlib.
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public License
12   * as published by the Free Software Foundation; either version 2.1 of
13   * the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful, but
16   * WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23   * USA
24   */
25  package info.rolandkrueger.roklib.webapps.urldispatching.urlparameters;
26  
27  import info.rolandkrueger.roklib.webapps.urldispatching.AbstractURLActionCommand;
28  
29  import java.util.List;
30  import java.util.Map;
31  
32  public class SingleDoubleURLParameter extends AbstractSingleURLParameter<Double>
33  {
34    private static final long serialVersionUID = - 8782412809369726453L;
35  
36    public SingleDoubleURLParameter (String parameterName)
37    {
38      super (parameterName);
39    }
40  
41    public SingleDoubleURLParameter (String parameterName, Double defaultValue)
42    {
43      super (parameterName);
44      setDefaultValue (defaultValue);
45    }
46  
47    protected boolean consumeImpl (Map<String, List<String>> parameters)
48    {
49      List<String> valueList = parameters.remove (getParameterName ());
50      if (valueList == null || valueList.isEmpty ())
51      {
52        return false;
53      }
54      return consumeValue (valueList.get (0));
55    }
56  
57    @Override
58    protected boolean consumeListImpl (String[] values)
59    {
60      if (values == null || values.length == 0)
61        return false;
62      return consumeValue (values[0]);
63    }
64  
65    private boolean consumeValue (String stringValue)
66    {
67      try
68      {
69        setValue (Double.valueOf (stringValue));
70        return true;
71      } catch (NumberFormatException nfExc)
72      {
73        mError = EnumURLParameterErrors.CONVERSION_ERROR;
74        return false;
75      }
76    }
77  
78    public AbstractURLActionCommand getErrorCommandIfInvalid ()
79    {
80      return null;
81    }
82  }