Coverage Report - com.sdmetrics.model.MetaModelElement - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
MetaModelElement
100%
40/40
100%
10/10
1,438
MetaModelElement$MetaModelElementAttribute
100%
7/7
N/A
1,438
 
 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.model;
 23  
 
 24  
 import java.util.Collection;
 25  
 import java.util.LinkedHashMap;
 26  
 import java.util.Map;
 27  
 
 28  
 /**
 29  
  * Represents an element type of the SDMetrics metamodel.
 30  
  * <p>
 31  
  * An element type has a name, a parent element type, and a list of attributes.
 32  
  * Attributes can contain data or references to model elements, and may be
 33  
  * single-valued or multi-valued.
 34  
  */
 35  
 public class MetaModelElement {
 36  
         /** Name of the attribute with the XMI ID of a model element. */
 37  
         final static String ID = "id";
 38  
         /** Name of the attribute with the name of a model element. */
 39  
         final static String NAME = "name";
 40  
         /** Name of the attribute with the owner of a model element. */
 41  
         final static String CONTEXT = "context";
 42  
 
 43  
         /** Name of this element type. */
 44  
         private final String typeName;
 45  
         /**
 46  
          * The attributes of this element type. Allows lookup of the attributes by
 47  
          * their name.
 48  
          */
 49  2471
         private final Map<String, MetaModelElementAttribute> attributes = 
 50  2471
                         new LinkedHashMap<String, MetaModelElementAttribute>(4);
 51  
         /** The parent type of this element type. */
 52  
         private MetaModelElement parent;
 53  
         /** Name of the extension reference attribute, if any. */
 54  
         private String extensionReference;
 55  
 
 56  
         /** Stores the definition of a metamodel element attribute. */
 57  
         static class MetaModelElementAttribute {
 58  
                 /** Name of the attribute. */
 59  
                 String attrName;
 60  
                 /** Indicates if this attribute references other model elements. */
 61  
                 boolean isReference;
 62  
                 /** Indicates if this is a multi-valued attribute. */
 63  
                 boolean isSet;
 64  
                 /** Description text of the attribute. */
 65  
                 String description;
 66  
                 /** Index of the attribute, for efficient array storage. */
 67  
                 int index;
 68  
 
 69  
                 /**
 70  
                  * @param name Name of the attribute
 71  
                  * @param isRef Indicates if the attribute references other model
 72  
                  *        elements
 73  
                  * @param isSet Indicates if this is a multi-valued attribute
 74  
                  * @param index Index of the attribute
 75  
                  */
 76  2755
                 MetaModelElementAttribute(String name, boolean isRef, boolean isSet,
 77  
                                 int index) {
 78  2755
                         this.attrName = name;
 79  2755
                         this.isReference = isRef;
 80  2755
                         this.isSet = isSet;
 81  2755
                         this.description = "";
 82  2755
                         this.index = index;
 83  2755
                 }
 84  
         }
 85  
 
 86  
         /**
 87  
          * Creates a element type.
 88  
          * 
 89  
          * @param name Name of the type.
 90  
          * @param parent The parent of the type.
 91  
          */
 92  2471
         MetaModelElement(String name, MetaModelElement parent) {
 93  2471
                 this.typeName = name;
 94  2471
                 if (parent != null) {
 95  
                         // inherit all of the parent's attributes
 96  2251
                         this.parent = parent;
 97  13540
                         for (MetaModelElementAttribute parentAttribute : parent.attributes
 98  2251
                                         .values())
 99  9038
                                 attributes.put(parentAttribute.attrName, parentAttribute);
 100  2251
                         this.extensionReference = parent.extensionReference;
 101  
                 }
 102  2471
         }
 103  
 
 104  
         /**
 105  
          * Gets the name of this element type.
 106  
          * 
 107  
          * @return Name of the element type.
 108  
          */
 109  
         public String getName() {
 110  31
                 return typeName;
 111  
         }
 112  
 
 113  
         /**
 114  
          * Gets the parent of this element type.
 115  
          * 
 116  
          * @return Parent of the metamodel element, <code>null</code> if this is the
 117  
          *         metamodel base element type.
 118  
          * @since 2.3
 119  
          */
 120  
         public MetaModelElement getParent() {
 121  3903
                 return parent;
 122  
         }
 123  
 
 124  
         /**
 125  
          * Checks if this type specializes another type.
 126  
          * 
 127  
          * @param base Base type against which to check
 128  
          * @return <code>true</code> if the <code>base</code> is the same type as
 129  
          *         this type, or a direct or indirect parent of this type.
 130  
          * @since 2.3
 131  
          */
 132  
         public boolean specializes(MetaModelElement base) {
 133  119
                 MetaModelElement type = this;
 134  465
                 while (type != null) {
 135  243
                         if (type == base)
 136  16
                                 return true;
 137  227
                         type = type.getParent();
 138  
                 }
 139  103
                 return false;
 140  
         }
 141  
 
 142  
         /**
 143  
          * Gets the attribute names of the metamodel element. This includes
 144  
          * inherited attributes. The collection maintains the attributes in the
 145  
          * order in which they were defined in the metamodel definition file;
 146  
          * inherited elements are listed first.
 147  
          * 
 148  
          * @return Collection of attribute names
 149  
          */
 150  
         public Collection<String> getAttributeNames() {
 151  15576
                 return attributes.keySet();
 152  
         }
 153  
 
 154  
         /**
 155  
          * Tests if this element type has an attribute of a given name.
 156  
          * 
 157  
          * @param name Name of the candidate attribute
 158  
          * @return <code>true</code> if this element type has an attribute of that
 159  
          *         name.
 160  
          */
 161  
         public boolean hasAttribute(String name) {
 162  10041
                 return attributes.containsKey(name);
 163  
         }
 164  
 
 165  
         /**
 166  
          * Tests if an attribute is a cross-reference attribute.
 167  
          * 
 168  
          * @param name Name of the attribute to test.
 169  
          * @return <code>true</code> if the attribute is a cross-reference
 170  
          *         attribute, <code>false</code> if it is a data attribute.
 171  
          * @throws IllegalArgumentException Element type has no such attribute.
 172  
          */
 173  
         public boolean isRefAttribute(String name) {
 174  9787
                 return getAttribute(name).isReference;
 175  
         }
 176  
 
 177  
         /**
 178  
          * Gets the name of the extension reference attribute.
 179  
          * 
 180  
          * @return Name of the extension reference attribute, or <code>null</code>
 181  
          *         if none is defined.
 182  
          * @since 2.3
 183  
          */
 184  
         public String getExtensionReference() {
 185  1979
                 return extensionReference;
 186  
         }
 187  
 
 188  
         /**
 189  
          * Tests if an attribute is multi-valued.
 190  
          * 
 191  
          * @param name Name of the attribute to test.
 192  
          * @return <code>true</code> if the attribute is multi-valued,
 193  
          *         <code>false</code> if it only stores a single value.
 194  
          * @throws IllegalArgumentException Element type has no such attribute.
 195  
          */
 196  
         public boolean isSetAttribute(String name) {
 197  161092
                 return getAttribute(name).isSet;
 198  
         }
 199  
 
 200  
         /**
 201  
          * Gets the description of an attribute.
 202  
          * 
 203  
          * @param name Name of the attribute.
 204  
          * @return Informal description of the attribute.
 205  
          * @throws IllegalArgumentException Element type has no such attribute.
 206  
          */
 207  
         public String getAttributeDescription(String name) {
 208  5
                 return getAttribute(name).description;
 209  
         }
 210  
 
 211  
         /**
 212  
          * Adds an attribute to this element type.
 213  
          * 
 214  
          * @param attrName Name of the attribute.
 215  
          * @param isRef <code>true</code> if this is to be a cross-reference
 216  
          *        attribute, <code>false</code> if it is data-valued.
 217  
          * @param isSet <code>true</code> if this is to be a multi-valued attribute,
 218  
          *        <code>false</code> if it is single-valued.
 219  
          */
 220  
         void addAttribute(String attrName, boolean isRef, boolean isSet) {
 221  5510
                 MetaModelElementAttribute attrib = new MetaModelElementAttribute(
 222  2755
                                 attrName, isRef, isSet, attributes.size());
 223  2755
                 attributes.put(attrName, attrib);
 224  2755
         }
 225  
 
 226  
         /**
 227  
          * Gets the index of an attribute. Each attribute of an element type has a
 228  
          * unique index. Attribute indices go from 0 to N-1, where N is the total
 229  
          * number of attributes of this element type.
 230  
          * 
 231  
          * @param name Name of the attribute
 232  
          * @return Index of the attribute in this element type
 233  
          * @throws IllegalArgumentException Element type has no such attribute.
 234  
          */
 235  
         int getAttributeIndex(String name) {
 236  231114
                 return getAttribute(name).index;
 237  
         }
 238  
 
 239  
         /**
 240  
          * Adds text to the description of an attribute.
 241  
          * 
 242  
          * @param name Name of the attribute.
 243  
          * @param description Text to add to the attribute's description.
 244  
          * @throws IllegalArgumentException Element type has no such attribute.
 245  
          */
 246  
         void addAttributeDescription(String name, String description) {
 247  2307
                 MetaModelElementAttribute attr = getAttribute(name);
 248  2307
                 attr.description = attr.description + description;
 249  2307
         }
 250  
 
 251  
         /**
 252  
          * Gets the attribute definition for an attribute.
 253  
          * 
 254  
          * @param name Name of the attribute
 255  
          * @return The definition of the attribute
 256  
          * @throws IllegalArgumentException Element type has no such attribute.
 257  
          */
 258  
         private MetaModelElementAttribute getAttribute(String name) {
 259  404305
                 MetaModelElementAttribute attr = attributes.get(name);
 260  404305
                 if (attr == null)
 261  2
                         throw new IllegalArgumentException("No attribute \"" + name
 262  1
                                         + "\" defined for elements of type \"" + typeName + "\".");
 263  404304
                 return attr;
 264  
         }
 265  
 
 266  
         /**
 267  
          * Sets the name of the extension reference attribute.
 268  
          * 
 269  
          * @param retAttrName Name of the extension reference attribute
 270  
          */
 271  
         void setExtensionReference(String retAttrName) {
 272  100
                 this.extensionReference = retAttrName;
 273  100
         }
 274  
 }