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  
         static final String ID = "id";
 38  
         /** Name of the attribute with the name of a model element. */
 39  
         static final String NAME = "name";
 40  
         /** Name of the attribute with the owner of a model element. */
 41  
         static final 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  2509
         private final Map<String, MetaModelElementAttribute> attributes = 
 50  2509
                         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  
                  * Constructor.
 71  
                  * @param name Name of the attribute
 72  
                  * @param isRef Indicates if the attribute references other model
 73  
                  *        elements
 74  
                  * @param isSet Indicates if this is a multi-valued attribute
 75  
                  * @param index Index of the attribute
 76  
                  */
 77  2800
                 MetaModelElementAttribute(String name, boolean isRef, boolean isSet,
 78  
                                 int index) {
 79  2800
                         this.attrName = name;
 80  2800
                         this.isReference = isRef;
 81  2800
                         this.isSet = isSet;
 82  2800
                         this.description = "";
 83  2800
                         this.index = index;
 84  2800
                 }
 85  
         }
 86  
 
 87  
         /**
 88  
          * Creates a element type.
 89  
          * 
 90  
          * @param name Name of the type.
 91  
          * @param parent The parent of the type.
 92  
          */
 93  2509
         MetaModelElement(String name, MetaModelElement parent) {
 94  2509
                 this.typeName = name;
 95  2509
                 if (parent != null) {
 96  
                         // inherit all of the parent's attributes
 97  2283
                         this.parent = parent;
 98  13719
                         for (MetaModelElementAttribute parentAttribute : parent.attributes
 99  2283
                                         .values()) {
 100  9153
                                 attributes.put(parentAttribute.attrName, parentAttribute);
 101  
                         }
 102  2283
                         this.extensionReference = parent.extensionReference;
 103  
                 }
 104  2509
         }
 105  
 
 106  
         /**
 107  
          * Gets the name of this element type.
 108  
          * 
 109  
          * @return Name of the element type.
 110  
          */
 111  
         public String getName() {
 112  31
                 return typeName;
 113  
         }
 114  
 
 115  
         /**
 116  
          * Gets the parent of this element type.
 117  
          * 
 118  
          * @return Parent of the metamodel element, <code>null</code> if this is the
 119  
          *         metamodel base element type.
 120  
          * @since 2.3
 121  
          */
 122  
         public MetaModelElement getParent() {
 123  3923
                 return parent;
 124  
         }
 125  
 
 126  
         /**
 127  
          * Checks if this type specializes another type.
 128  
          * 
 129  
          * @param base Base type against which to check
 130  
          * @return <code>true</code> if the <code>base</code> is the same type as
 131  
          *         this type, or a direct or indirect parent of this type.
 132  
          * @since 2.3
 133  
          */
 134  
         public boolean specializes(MetaModelElement base) {
 135  119
                 MetaModelElement type = this;
 136  465
                 while (type != null) {
 137  243
                         if (type == base) {
 138  16
                                 return true;
 139  
                         }
 140  227
                         type = type.getParent();
 141  
                 }
 142  103
                 return false;
 143  
         }
 144  
 
 145  
         /**
 146  
          * Gets the attribute names of the metamodel element. This includes
 147  
          * inherited attributes. The collection maintains the attributes in the
 148  
          * order in which they were defined in the metamodel definition file;
 149  
          * inherited elements are listed first.
 150  
          * 
 151  
          * @return Collection of attribute names
 152  
          */
 153  
         public Collection<String> getAttributeNames() {
 154  15618
                 return attributes.keySet();
 155  
         }
 156  
 
 157  
         /**
 158  
          * Tests if this element type has an attribute of a given name.
 159  
          * 
 160  
          * @param name Name of the candidate attribute
 161  
          * @return <code>true</code> if this element type has an attribute of that
 162  
          *         name.
 163  
          */
 164  
         public boolean hasAttribute(String name) {
 165  10102
                 return attributes.containsKey(name);
 166  
         }
 167  
 
 168  
         /**
 169  
          * Tests if an attribute is a cross-reference attribute.
 170  
          * 
 171  
          * @param name Name of the attribute to test.
 172  
          * @return <code>true</code> if the attribute is a cross-reference
 173  
          *         attribute, <code>false</code> if it is a data attribute.
 174  
          * @throws IllegalArgumentException Element type has no such attribute.
 175  
          */
 176  
         public boolean isRefAttribute(String name) {
 177  9880
                 return getAttribute(name).isReference;
 178  
         }
 179  
 
 180  
         /**
 181  
          * Gets the name of the extension reference attribute.
 182  
          * 
 183  
          * @return Name of the extension reference attribute, or <code>null</code>
 184  
          *         if none is defined.
 185  
          * @since 2.3
 186  
          */
 187  
         public String getExtensionReference() {
 188  2000
                 return extensionReference;
 189  
         }
 190  
 
 191  
         /**
 192  
          * Tests if an attribute is multi-valued.
 193  
          * 
 194  
          * @param name Name of the attribute to test.
 195  
          * @return <code>true</code> if the attribute is multi-valued,
 196  
          *         <code>false</code> if it only stores a single value.
 197  
          * @throws IllegalArgumentException Element type has no such attribute.
 198  
          */
 199  
         public boolean isSetAttribute(String name) {
 200  161364
                 return getAttribute(name).isSet;
 201  
         }
 202  
 
 203  
         /**
 204  
          * Gets the description of an attribute.
 205  
          * 
 206  
          * @param name Name of the attribute.
 207  
          * @return Informal description of the attribute.
 208  
          * @throws IllegalArgumentException Element type has no such attribute.
 209  
          */
 210  
         public String getAttributeDescription(String name) {
 211  5
                 return getAttribute(name).description;
 212  
         }
 213  
 
 214  
         /**
 215  
          * Adds an attribute to this element type.
 216  
          * 
 217  
          * @param attrName Name of the attribute.
 218  
          * @param isRef <code>true</code> if this is to be a cross-reference
 219  
          *        attribute, <code>false</code> if it is data-valued.
 220  
          * @param isSet <code>true</code> if this is to be a multi-valued attribute,
 221  
          *        <code>false</code> if it is single-valued.
 222  
          */
 223  
         void addAttribute(String attrName, boolean isRef, boolean isSet) {
 224  5600
                 MetaModelElementAttribute attrib = new MetaModelElementAttribute(
 225  2800
                                 attrName, isRef, isSet, attributes.size());
 226  2800
                 attributes.put(attrName, attrib);
 227  2800
         }
 228  
 
 229  
         /**
 230  
          * Gets the index of an attribute. Each attribute of an element type has a
 231  
          * unique index. Attribute indices go from 0 to N-1, where N is the total
 232  
          * number of attributes of this element type.
 233  
          * 
 234  
          * @param name Name of the attribute
 235  
          * @return Index of the attribute in this element type
 236  
          * @throws IllegalArgumentException Element type has no such attribute.
 237  
          */
 238  
         int getAttributeIndex(String name) {
 239  231466
                 return getAttribute(name).index;
 240  
         }
 241  
 
 242  
         /**
 243  
          * Adds text to the description of an attribute.
 244  
          * 
 245  
          * @param name Name of the attribute.
 246  
          * @param description Text to add to the attribute's description.
 247  
          * @throws IllegalArgumentException Element type has no such attribute.
 248  
          */
 249  
         void addAttributeDescription(String name, String description) {
 250  2327
                 MetaModelElementAttribute attr = getAttribute(name);
 251  2327
                 attr.description = attr.description + description;
 252  2327
         }
 253  
 
 254  
         /**
 255  
          * Gets the attribute definition for an attribute.
 256  
          * 
 257  
          * @param name Name of the attribute
 258  
          * @return The definition of the attribute
 259  
          * @throws IllegalArgumentException Element type has no such attribute.
 260  
          */
 261  
         private MetaModelElementAttribute getAttribute(String name) {
 262  405042
                 MetaModelElementAttribute attr = attributes.get(name);
 263  405042
                 if (attr == null) {
 264  2
                         throw new IllegalArgumentException("No attribute \"" + name
 265  1
                                         + "\" defined for elements of type \"" + typeName + "\".");
 266  
                 }
 267  405041
                 return attr;
 268  
         }
 269  
 
 270  
         /**
 271  
          * Sets the name of the extension reference attribute.
 272  
          * 
 273  
          * @param retAttrName Name of the extension reference attribute
 274  
          */
 275  
         void setExtensionReference(String retAttrName) {
 276  102
                 this.extensionReference = retAttrName;
 277  102
         }
 278  
 }