Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 218   Methods: 15
NCLOC: 113   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PropertyMaps.java 30% 49.3% 60% 47.1%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * 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
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.drjava.config;
 38   
 39    import edu.rice.cs.drjava.DrJava;
 40    import edu.rice.cs.plt.lambda.Lambda2;
 41    import java.util.*;
 42   
 43    /** Class representing all the variables that
 44    * can be inserted as variables in external processes.
 45    * @version $Id: PropertyMaps.java 5175 2010-01-20 08:46:32Z mgricken $
 46    */
 47    public final class PropertyMaps implements Cloneable {
 48    /** Map of property sets. */
 49    protected Map<String,Map<String,DrJavaProperty>> _props = new TreeMap<String,Map<String,DrJavaProperty>>();
 50   
 51    /** Template instance. */
 52    public static final PropertyMaps TEMPLATE = new PropertyMaps();
 53   
 54    static {
 55  8 for(Map.Entry<Object,Object> es: System.getProperties().entrySet()) {
 56  436 TEMPLATE.setProperty("Java", new JavaSystemProperty(es.getKey().toString()));
 57    }
 58   
 59  8 OptionMap om = DrJava.getConfig().getOptionMap();
 60  8 for (OptionParser<?> op : om.keys()) {
 61  2684 String key = "config." + op.getName();
 62  2684 TEMPLATE.setProperty("Config", new ConfigProperty(key));
 63    }
 64    }
 65   
 66    /** Create the basic property maps.
 67    * One is named "Java" and contains the Java system properties.
 68    * A second one is named "Config" and contains the DrJava configuration items. */
 69  58 public PropertyMaps() { }
 70   
 71    /** Return the property requested, or null if not found.
 72    * @param category name of the category
 73    * @param name name of the property
 74    * @return property, or null if not found
 75    * @throws IllegalArgumentException if category is not known. */
 76  50748 public DrJavaProperty getProperty(String category, String name) {
 77  50748 Map<String,DrJavaProperty> m = _props.get(category);
 78  0 if (m == null) { throw new IllegalArgumentException("DrJavaProperty category unknown."); }
 79  50748 return m.get(name);
 80    }
 81   
 82    /** Search through all categories and return the property requested, or null if not found.
 83    * @param key key of the property
 84    * @return property, or null if not found */
 85  20541 public DrJavaProperty getProperty(String key) {
 86  20541 for(String category: _props.keySet()) {
 87  49779 DrJavaProperty p = getProperty(category, key);
 88  20528 if (p != null) { return p; }
 89    }
 90  13 return null;
 91    }
 92   
 93    /** Remove the specified property.
 94    * @param p property to remove */
 95  0 public void removeProperty(DrJavaProperty p) {
 96  0 for(String category: _props.keySet()) {
 97  0 _props.get(category).remove(p);
 98    }
 99    }
 100   
 101    /** Add a property. */
 102  26490 public DrJavaProperty setProperty(String category, DrJavaProperty p) {
 103  26490 Map<String,DrJavaProperty> m = _props.get(category);
 104  330 if (m == null) { m = new HashMap<String,DrJavaProperty>(); _props.put(category,m); }
 105  26490 m.put(p.getName(), p);
 106  26490 return p;
 107    }
 108   
 109    /** Clear the specified category. */
 110  0 public void clearCategory(String category) {
 111  0 _props.remove(category);
 112    }
 113   
 114    /** Return the set of categories. */
 115  114 public Set<String> getCategories() { return _props.keySet(); }
 116   
 117    /** Return the properties in a category.
 118    * @throws IllegalArgumentException if category is not known. */
 119  510 public Map<String, DrJavaProperty> getProperties(String category) {
 120  510 Map<String,DrJavaProperty> m = _props.get(category);
 121  0 if (m == null) { throw new IllegalArgumentException("DrJavaProperty category unknown."); }
 122  510 return m;
 123    }
 124   
 125    /** A lambda to use the getLazy() method, which does not force an update and might be stale. */
 126    public static final Lambda2<DrJavaProperty,PropertyMaps,String> GET_LAZY = new Lambda2<DrJavaProperty,PropertyMaps,String>() {
 127  78 public String value(DrJavaProperty p, PropertyMaps pm) { return p.getLazy(pm); /* might be stale */ }
 128    };
 129   
 130    /** A lambda to use the getCurrent() method, which forces an update. */
 131    public static final Lambda2<DrJavaProperty,PropertyMaps,String> GET_CURRENT = new Lambda2<DrJavaProperty,PropertyMaps,String>() {
 132  47 public String value(DrJavaProperty p, PropertyMaps pm) { return p.getCurrent(pm); }
 133    };
 134   
 135    protected HashMap<String,Stack<VariableProperty>> _variables = new HashMap<String,Stack<VariableProperty>>();
 136   
 137    // name of the category for variables
 138    protected static final String VARIABLES_CATEGORY = "$Variables$";
 139   
 140    /** Clear all user-defined variables. */
 141  0 public void clearVariables() {
 142  0 _props.remove(VARIABLES_CATEGORY);
 143    }
 144   
 145    /** Add a variable with the specified name and value, shadowing previous definitions of the variable.
 146    * @param name name of the variable
 147    * @param value value of the variable
 148    * @throws IllegalArgumentException if the name is already used for a built-in property */
 149  0 public void addVariable(String name, String value) {
 150  0 for(String category: _props.keySet()) {
 151  0 if (category.equals(VARIABLES_CATEGORY)) continue;
 152  0 if (getProperty(category, name) != null) {
 153  0 throw new IllegalArgumentException("Variable " + name + " already used for a built-in property");
 154    }
 155    }
 156    // name not used by built-in
 157  0 VariableProperty p = new VariableProperty(name, value);
 158  0 setProperty(VARIABLES_CATEGORY, p);
 159  0 Stack<VariableProperty> varStack = _variables.get(name);
 160  0 if (varStack == null) { varStack = new Stack<VariableProperty>(); _variables.put(name,varStack); }
 161  0 varStack.push(p);
 162    }
 163   
 164    /** Mutate the value of a variable with the specified name.
 165    * @param name name of the variable
 166    * @param value new value of the variable
 167    * @throws IllegalArgumentException if a variable with name does not exist */
 168  0 public void setVariable(String name, String value) {
 169  0 Stack<VariableProperty> varStack = _variables.get(name);
 170  0 if ((varStack == null) ||
 171  0 (varStack.empty())) { throw new IllegalArgumentException("Variable " + name + " does not exist."); }
 172  0 VariableProperty p = varStack.peek();
 173  0 p.setValue(value);
 174    }
 175   
 176    /** Remove the variable with the specified name, unshadowing previous definitions of the variable.
 177    * @param name of the variable
 178    * @throws IllegalArgumentException if no variable with that name exists */
 179  0 public void removeVariable(String name) {
 180  0 Stack<VariableProperty> varStack = _variables.get(name);
 181  0 if ((varStack == null) ||
 182  0 (varStack.empty())) { throw new IllegalArgumentException("Variable " + name + " does not exist."); }
 183  0 VariableProperty p = varStack.pop();
 184  0 if (varStack.empty()) {
 185    // no shadowed variables
 186    // remove the stack from the hash map of variables
 187  0 _variables.remove(name);
 188    // remove the property
 189  0 removeProperty(p);
 190    }
 191    else {
 192    // previously shadowed variable(s) exist
 193    // set unshadowed variable as new value of property
 194  0 setProperty(VARIABLES_CATEGORY, varStack.peek());
 195    }
 196    }
 197   
 198    /** Clone this PropertyMaps object.
 199    * @return cloned object */
 200  46 public PropertyMaps clone() throws CloneNotSupportedException {
 201  46 PropertyMaps clone = new PropertyMaps();
 202  46 clone._props.clear();
 203  46 for(String category: _props.keySet()) {
 204  276 for (String key: _props.get(category).keySet()) {
 205  20384 clone.setProperty(category, getProperty(key));
 206    }
 207    }
 208  46 clone._variables.clear();
 209  46 for(String name: _variables.keySet()) {
 210  0 Stack<VariableProperty> stack = new Stack<VariableProperty>();
 211  0 for (VariableProperty v: _variables.get(name)) {
 212  0 stack.add(new VariableProperty(v.getName(),v.getCurrent(this)));
 213    }
 214  0 clone._variables.put(name, stack);
 215    }
 216  46 return clone;
 217    }
 218    }