Clover coverage report - Ledge Components - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:13:20 CET
file stats: LOC: 1,005   Methods: 65
NCLOC: 675   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultParameters.java 80.6% 89% 92.3% 87.1%
coverage coverage
 1    //
 2    //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 3    //All rights reserved.
 4    //
 5    //Redistribution and use in source and binary forms, with or without modification,
 6    //are permitted provided that the following conditions are met:
 7    //
 8    //* Redistributions of source code must retain the above copyright notice,
 9    //this list of conditions and the following disclaimer.
 10    //* Redistributions in binary form must reproduce the above copyright notice,
 11    //this list of conditions and the following disclaimer in the documentation
 12    //and/or other materials provided with the distribution.
 13    //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 14    //nor the names of its contributors may be used to endorse or promote products
 15    //derived from this software without specific prior written permission.
 16    //
 17    //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 18    //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19    //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 20    //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 21    //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 22    //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 23    //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 24    //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25    //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26    //POSSIBILITY OF SUCH DAMAGE.
 27    //
 28   
 29    package org.objectledge.parameters;
 30   
 31    import java.io.IOException;
 32    import java.io.InputStream;
 33    import java.io.InputStreamReader;
 34    import java.io.LineNumberReader;
 35    import java.io.StringReader;
 36    import java.io.UnsupportedEncodingException;
 37    import java.util.ArrayList;
 38    import java.util.Arrays;
 39    import java.util.Collections;
 40    import java.util.Date;
 41    import java.util.HashMap;
 42    import java.util.Iterator;
 43    import java.util.List;
 44    import java.util.Map;
 45    import java.util.Set;
 46    import java.util.StringTokenizer;
 47   
 48    import org.objectledge.database.DatabaseUtils;
 49   
 50    /**
 51    * A simple implementation of parameters container.
 52    *
 53    * @author <a href="mailto:pablo@caltha.com">Pawel Potempski</a>
 54    * @version $Id: DefaultParameters.java,v 1.24 2005/12/20 09:09:32 pablo Exp $
 55    */
 56    public class DefaultParameters implements Parameters
 57    {
 58    /** string representation for boolean <code>true</code> value. */
 59    public static final String TRUE = "true";
 60   
 61    /**
 62    * Create a string represenation of value array.
 63    *
 64    * <p>Values will be emmited comma separated, with any contained commas backslash-escaped.</p>
 65    *
 66    * @param values value arrary.
 67    * @return the string representation of the values.
 68    */
 69  368 public static String toString(String[] values)
 70    {
 71  368 StringBuilder sb = new StringBuilder();
 72  368 for (int i = 0; i < values.length; i++)
 73    {
 74  644 String value = values[i];
 75  644 int index = value.indexOf(',');
 76  644 int start = 0;
 77  644 while (index >= 0)
 78    {
 79  92 sb.append(value.substring(start, index));
 80  92 sb.append("\\,");
 81  92 start = index + 1;
 82  92 index = value.indexOf(',', start);
 83    }
 84  644 if (start < value.length())
 85    {
 86  644 sb.append(value.substring(start));
 87    }
 88  644 if (i < (values.length - 1))
 89    {
 90  276 sb.append(",");
 91    }
 92    }
 93  368 return sb.toString();
 94    }
 95   
 96    /** The main parameters map. */
 97    protected Map<String, String[]> map;
 98   
 99    /**
 100    * Method used in constructors to choose backing <code>Map</code> implementation.
 101    * By default a <code>HashMap</code> is used.
 102    */
 103  14168 protected void setupMap()
 104    {
 105  14168 map = new HashMap<String, String[]>();
 106    }
 107   
 108    /**
 109    * Create the empty container.
 110    */
 111  8188 public DefaultParameters()
 112    {
 113  8188 setupMap();
 114    }
 115   
 116    /**
 117    * Create the container and feed it with configuration given as string.
 118    *
 119    * @param configuration the string representation of the container.
 120    */
 121  552 public DefaultParameters(String configuration)
 122    {
 123  552 setupMap();
 124  552 try
 125    {
 126  552 LineNumberReader reader = new LineNumberReader(new StringReader(configuration));
 127  506 loadParameters(reader);
 128    }
 129    catch (Exception e)
 130    {
 131  92 throw new IllegalArgumentException("Exception occurred" + e.getMessage());
 132    }
 133    }
 134   
 135    /**
 136    * Create the container and feed it with configuration given as string.
 137    *
 138    * @param is the stream with byte representation of the container.
 139    * @param encoding the encoding of the source.
 140    * @throws UnsupportedEncodingException if the specified encoding is not supported by the JVM.
 141    * @throws IOException if there is an error reading data from the stream.
 142    */
 143  46 public DefaultParameters(InputStream is, String encoding)
 144    throws IOException, UnsupportedEncodingException
 145    {
 146  46 setupMap();
 147  46 LineNumberReader reader = new LineNumberReader(new InputStreamReader(is, encoding));
 148  46 loadParameters(reader);
 149    }
 150   
 151    /**
 152    * Create the container as a copy of source container.
 153    *
 154    * @param source the source container.
 155    */
 156  5382 public DefaultParameters(Parameters source)
 157    {
 158  5382 setupMap();
 159  5382 add(source, true);
 160    }
 161   
 162    /**
 163    * {@inheritDoc}
 164    */
 165  2806 public String get(String name)
 166    {
 167  2806 String[] values = map.get(name);
 168  2806 if (values == null || values.length == 0)
 169    {
 170  552 throw new UndefinedParameterException("Parameter '" + name + "'is undefined");
 171    }
 172  2254 if (values.length > 1)
 173    {
 174  552 throw new AmbiguousParameterException("Parameter '" + name + "'has multiple values");
 175    }
 176  1702 return values[0];
 177    }
 178   
 179    /**
 180    * Returns the value of the parameter, or null if it is undefined.
 181    *
 182    * <p>This method will return <code>null</code> in any of the following situations:
 183    * <ul>
 184    * <li>the internal map contains no value array for the specified name</li>
 185    * <li>the internal map contains an empty value array for the specified name</li>
 186    * <li>the internal map contains a single value array for the specified name, and the
 187    * value in the array is an empty string</li>
 188    * </ul>
 189    * @param name the name of the parameter.
 190    * @return value of the parameter if undefined (see above).
 191    * @throws AmbiguousParameterException if the parameter has more than one value.
 192    */
 193  2208 protected String getSingleValue(String name)
 194    throws AmbiguousParameterException
 195    {
 196  2208 String[] values = map.get(name);
 197  2208 if (values == null || values.length == 0)
 198    {
 199  460 return null;
 200    }
 201  1748 if (values.length > 1)
 202    {
 203  0 throw new AmbiguousParameterException("Parameter '" + name + "'has multiple values");
 204    }
 205  1748 String value = values[0];
 206  1748 if(value.equals(""))
 207    {
 208  46 return null;
 209    }
 210  1702 return value;
 211    }
 212   
 213    /**
 214    * {@inheritDoc}
 215    */
 216  1104 public String get(String name, String defaultValue)
 217    {
 218  1104 String[] values = map.get(name);
 219  1104 if (values == null || values.length == 0)
 220    {
 221  184 return defaultValue;
 222    }
 223  920 if (values.length > 1)
 224    {
 225  92 throw new AmbiguousParameterException("Parameter '" + name + "'has multiple values");
 226    }
 227  828 return values[0];
 228    }
 229   
 230    /**
 231    * {@inheritDoc}
 232    */
 233  28612 public String[] getStrings(String name)
 234    {
 235  28612 String[] values = map.get(name);
 236  28612 if (values == null)
 237    {
 238  3864 return new String[0];
 239    }
 240  24748 String[] target = new String[values.length];
 241  24748 System.arraycopy(values, 0, target, 0, values.length);
 242  24748 return target;
 243    }
 244   
 245    /**
 246    * {@inheritDoc}
 247    */
 248  414 public boolean getBoolean(String name)
 249    {
 250  414 String value = get(name);
 251  322 return value.equals(TRUE);
 252    }
 253   
 254    /**
 255    * {@inheritDoc}
 256    */
 257  552 public boolean getBoolean(String name, boolean defaultValue)
 258    {
 259  552 String value = getSingleValue(name);
 260  552 return value != null ? value.equals(TRUE) : defaultValue;
 261    }
 262   
 263    /**
 264    * {@inheritDoc}
 265    */
 266  644 public boolean[] getBooleans(String name)
 267    {
 268  644 String[] values = getStrings(name);
 269  644 boolean[] target = new boolean[values.length];
 270  644 for (int i = 0; i < values.length; i++)
 271    {
 272  1288 target[i] = values[i].equals(TRUE);
 273    }
 274  644 return target;
 275    }
 276   
 277    /**
 278    * {@inheritDoc}
 279    */
 280  368 public Date getDate(String name)
 281    {
 282  368 String value = get(name);
 283  184 return new Date(Long.parseLong(value));
 284    }
 285   
 286    /**
 287    * {@inheritDoc}
 288    */
 289  276 public Date getDate(String name, Date defaultValue)
 290    {
 291  276 String value = getSingleValue(name);
 292  276 return value != null ? new Date(Long.parseLong(value)) : defaultValue;
 293    }
 294   
 295    /**
 296    * {@inheritDoc}
 297    */
 298  368 public Date[] getDates(String name)
 299    {
 300  368 String[] values = getStrings(name);
 301  368 Date[] target = new Date[values.length];
 302  368 for (int i = 0; i < values.length; i++)
 303    {
 304  552 target[i] = new Date(Long.parseLong(values[i]));
 305    }
 306  368 return target;
 307    }
 308   
 309    /**
 310    * {@inheritDoc}
 311    */
 312  368 public float getFloat(String name) throws NumberFormatException
 313    {
 314  368 return Float.parseFloat(get(name));
 315    }
 316   
 317    /**
 318    * {@inheritDoc}
 319    */
 320  644 public float getFloat(String name, float defaultValue)
 321    {
 322  644 String value = getSingleValue(name);
 323  644 return value != null ? Float.parseFloat(value) : defaultValue;
 324    }
 325   
 326    /**
 327    * {@inheritDoc}
 328    */
 329  460 public float[] getFloats(String name) throws NumberFormatException
 330    {
 331  460 String[] values = getStrings(name);
 332  460 float[] target = new float[values.length];
 333  460 for (int i = 0; i < values.length; i++)
 334    {
 335  1012 target[i] = Float.parseFloat(values[i]);
 336    }
 337  460 return target;
 338    }
 339   
 340    /**
 341    * {@inheritDoc}
 342    */
 343  644 public int getInt(String name) throws NumberFormatException
 344    {
 345  644 return Integer.parseInt(get(name));
 346    }
 347   
 348    /**
 349    * {@inheritDoc}
 350    */
 351  368 public int getInt(String name, int defaultValue)
 352    {
 353  368 String value = getSingleValue(name);
 354  368 return value != null ? Integer.parseInt(value) : defaultValue;
 355    }
 356   
 357    /**
 358    * {@inheritDoc}
 359    */
 360  1104 public int[] getInts(String name)
 361    {
 362  1104 String[] values = getStrings(name);
 363  1104 int[] target = new int[values.length];
 364  1104 for (int i = 0; i < values.length; i++)
 365    {
 366  2116 target[i] = Integer.parseInt(values[i]);
 367    }
 368  1104 return target;
 369    }
 370   
 371    /**
 372    * {@inheritDoc}
 373    */
 374  368 public long getLong(String name) throws NumberFormatException
 375    {
 376  368 return Long.parseLong(get(name));
 377    }
 378   
 379    /**
 380    * {@inheritDoc}
 381    */
 382  368 public long getLong(String name, long defaultValue)
 383    {
 384  368 String value = getSingleValue(name);
 385  368 return value != null ? Long.parseLong(value) : defaultValue;
 386    }
 387   
 388    /**
 389    * {@inheritDoc}
 390    */
 391  368 public long[] getLongs(String name) throws NumberFormatException
 392    {
 393  368 String[] values = getStrings(name);
 394  368 long[] target = new long[values.length];
 395  368 for (int i = 0; i < values.length; i++)
 396    {
 397  736 target[i] = Long.parseLong(values[i]);
 398    }
 399  368 return target;
 400    }
 401   
 402    /**
 403    * {@inheritDoc}
 404    */
 405  7038 public String[] getParameterNames()
 406    {
 407  7038 String[] names = new String[map.size()];
 408  7038 map.keySet().toArray(names);
 409  7038 return names;
 410    }
 411   
 412    /**
 413    * {@inheritDoc}
 414    */
 415  1196 public boolean isDefined(String name)
 416    {
 417  1196 String[] values = map.get(name);
 418  1196 if (values != null && values.length > 0)
 419    {
 420  644 return true;
 421    }
 422  552 return false;
 423    }
 424   
 425    /**
 426    * {@inheritDoc}
 427    */
 428  92 public void remove()
 429    {
 430  92 map.clear();
 431    }
 432   
 433    /**
 434    * {@inheritDoc}
 435    */
 436  92 public void remove(String name)
 437    {
 438  92 map.remove(name);
 439    }
 440   
 441    /**
 442    * {@inheritDoc}
 443    */
 444  506 public void remove(String name, String value)
 445    {
 446  506 String[] values = map.get(name);
 447  506 if (values == null || values.length == 0)
 448    {
 449  92 return;
 450    }
 451  414 ArrayList<String> list = new ArrayList<String>();
 452  414 for (int i = 0; i < values.length; i++)
 453    {
 454  460 if (!((values[i] == null && value == null) ||
 455    (value != null && value.equals(values[i]))))
 456    {
 457  414 list.add(values[i]);
 458    }
 459    }
 460  414 values = new String[list.size()];
 461  414 list.toArray(values);
 462  414 map.put(name, values);
 463    }
 464   
 465    /**
 466    * {@inheritDoc}
 467    */
 468  0 public void remove(String name, Date value)
 469    {
 470  0 remove(name, Long.toString(value.getTime()));
 471    }
 472   
 473    /**
 474    * {@inheritDoc}
 475    */
 476  92 public void remove(String name, float value)
 477    {
 478  92 remove(name, Float.toString(value));