Clover coverage report - Code Coverage for tapestry release 4.0-beta-12
Coverage timestamp: Sun Oct 30 2005 16:22:01 EST
file stats: LOC: 127   Methods: 7
NCLOC: 53   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
QueryParameterMap.java 100% 100% 100% 100%
coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.util;
 16   
 17    import java.util.Arrays;
 18    import java.util.HashMap;
 19    import java.util.Map;
 20   
 21    import org.apache.hivemind.util.Defense;
 22   
 23    /**
 24    * A wrapper around a Map that stores query parameter values. Map keys are strings. Map values can
 25    * be simple strings or array of string (or null).
 26    *
 27    * @author Howard M. Lewis Ship
 28    * @since 4.0
 29    */
 30    public class QueryParameterMap
 31    {
 32    private final Map _parameters;
 33   
 34  134 public QueryParameterMap()
 35    {
 36  134 this(new HashMap());
 37    }
 38   
 39    /**
 40    * Constructor around an existing Map whose keys and values are expected to conform expected use
 41    * (keys are strings, values are null, string or string array). The map passed in is retained (
 42    * not copied).
 43    */
 44   
 45  527 public QueryParameterMap(Map parameterMap)
 46    {
 47  527 Defense.notNull(parameterMap, "parameterMap");
 48   
 49  527 _parameters = parameterMap;
 50    }
 51   
 52    /**
 53    * Replaces the parameter value for the given name wit the new value (which may be null).
 54    */
 55   
 56  347 public void setParameterValue(String name, String value)
 57    {
 58  347 Defense.notNull(name, "name");
 59   
 60  347 _parameters.put(name, value);
 61    }
 62   
 63    /**
 64    * Replaces the parameter value for the given name wit the new list of values (which may be
 65    * empty or null).
 66    */
 67   
 68  26 public void setParameterValues(String name, String[] values)
 69    {
 70  26 Defense.notNull(name, "name");
 71   
 72  26 _parameters.put(name, values);
 73    }
 74   
 75    /**
 76    * Gets a query parameter value. If an array of values was stored, this returns the first value.
 77    * May return null.
 78    */
 79   
 80  494 public String getParameterValue(String name)
 81    {
 82  494 Defense.notNull(name, "name");
 83   
 84  494 Object values = _parameters.get(name);
 85   
 86  494 if (values == null || values instanceof String)
 87  493 return (String) values;
 88   
 89  1 String[] array = (String[]) values;
 90   
 91  1 return array[0];
 92    }
 93   
 94    /**
 95    * Returns the array of values for the specified parameter. If only a lone value was stored (via
 96    * {@link #setParameterValue(String, String)}, then the value is wrapped as a string array and
 97    * returned.
 98    */
 99  794 public String[] getParameterValues(String name)
 100    {
 101  794 Defense.notNull(name, "name");
 102   
 103  794 Object values = _parameters.get(name);
 104   
 105  794 if (values == null || values instanceof String[])
 106  251 return (String[]) values;
 107   
 108  543 String loneValue = (String) values;
 109   
 110  543 return new String[]
 111    { loneValue };
 112    }
 113   
 114    /**
 115    * Returns the names of all parameters, sorted alphabetically.
 116    */
 117  201 public String[] getParameterNames()
 118    {
 119  201 int count = _parameters.size();
 120   
 121  201 String[] result = (String[]) _parameters.keySet().toArray(new String[count]);
 122   
 123  201 Arrays.sort(result);
 124   
 125  201 return result;
 126    }
 127    }