Coverage Report - com.sdmetrics.metrics.Variables - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
Variables
100%
9/9
N/A
1
 
 1  
 /*
 2  
  * SDMetrics Open Core for UML design measurement
 3  
  * Copyright (c) Juergen Wuest
 4  
  * To contact the author, see <http://www.sdmetrics.com/Contact.html>.
 5  
  * 
 6  
  * This file is part of the SDMetrics Open Core.
 7  
  * 
 8  
  * SDMetrics Open Core is free software: you can redistribute it and/or modify
 9  
  * it under the terms of the GNU Affero General Public License as
 10  
  * published by the Free Software Foundation, either version 3 of the
 11  
  * License, or (at your option) any later version.
 12  
     
 13  
  * SDMetrics Open Core is distributed in the hope that it will be useful,
 14  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  
  * GNU Affero General Public License for more details.
 17  
  *
 18  
  * You should have received a copy of the GNU Affero General Public License
 19  
  * along with SDMetrics Open Core.  If not, see <http://www.gnu.org/licenses/>.
 20  
  *
 21  
  */
 22  
 package com.sdmetrics.metrics;
 23  
 
 24  
 import java.util.HashMap;
 25  
 
 26  
 import com.sdmetrics.model.ModelElement;
 27  
 
 28  
 /**
 29  
  * Contains the names and values of variables to use when evaluating metric,
 30  
  * set, or condition expressions.
 31  
  * <p>
 32  
  * A set of variables always defines a principal, that is, the model element for
 33  
  * which a metric/set/rule is calculated. The principal is not necessarily the
 34  
  * same element for which the expression is evaluated.
 35  
  */
 36  
 public class Variables {
 37  
 
 38  
         /** Name of the variable holding the principal. */
 39  
         final static String PRINCIPAL_NAME = "_principal";
 40  
 
 41  
         /** Stores the variables and their values. */
 42  454
         private final HashMap<String, Object> map = new HashMap<String, Object>(2);
 43  
 
 44  
         /**
 45  
          * Creates a new set of variables.
 46  
          * 
 47  
          * @param principal Principal for this set.
 48  
          */
 49  454
         public Variables(ModelElement principal) {
 50  454
                 map.put(PRINCIPAL_NAME, principal);
 51  454
         }
 52  
 
 53  
         /**
 54  
          * Sets the value of a variable. Adds the variable if it is not already
 55  
          * defined in the set. Otherwise, replaces the value of the existing
 56  
          * variable with the new value.
 57  
          * 
 58  
          * @param name Name of the variable.
 59  
          * @param value Value of the variable.
 60  
          */
 61  
         public void setVariable(String name, Object value) {
 62  460
                 map.put(name, value);
 63  460
         }
 64  
 
 65  
         /**
 66  
          * Gets the value of a variable.
 67  
          * 
 68  
          * @param name Name of the variable.
 69  
          * @return Value of the variable.
 70  
          */
 71  
         public Object getVariable(String name) {
 72  675
                 return map.get(name);
 73  
         }
 74  
 
 75  
         /**
 76  
          * Tests whether a variable of a specified name is defined in this set.
 77  
          * 
 78  
          * @param name Name of the variable to look up.
 79  
          * @return <code>true</code> if the variable is defined.
 80  
          */
 81  
         public boolean hasVariable(String name) {
 82  636
                 return map.containsKey(name);
 83  
         }
 84  
 
 85  
         /**
 86  
          * Gets the principal of this set.
 87  
          * 
 88  
          * @return The principal.
 89  
          */
 90  
         ModelElement getPrincipal() {
 91  270
                 return (ModelElement) map.get(PRINCIPAL_NAME);
 92  
         }
 93  
 }