Coverage Report - com.sdmetrics.metrics.MetricTools - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
MetricTools
96%
27/28
100%
22/22
3
 
 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.Collection;
 25  
 import java.util.HashSet;
 26  
 import java.util.Iterator;
 27  
 
 28  
 import com.sdmetrics.math.HashMultiSet;
 29  
 
 30  
 /**
 31  
  * Collection of static helper methods that are useful for the implementation of
 32  
  * calculation procedures.
 33  
  */
 34  0
 public class MetricTools {
 35  
 
 36  
         /** Default return value 0. */
 37  27
         static final Integer ZERO = Integer.valueOf(0);
 38  
         /** Default return value 1. */
 39  27
         static final Integer ONE = Integer.valueOf(1);
 40  
 
 41  
         /**
 42  
          * Creates a new empty set or multiset.
 43  
          * 
 44  
          * @param multi <code>true</code> to create a multiset, <code>false</code>
 45  
          *        to create a regular set.
 46  
          * @return New empty HashSet or {@link HashMultiSet}
 47  
          */
 48  
         public static Collection<?> createHashSet(boolean multi) {
 49  161
                 if (multi) {
 50  47
                         return new HashMultiSet<Object>(1);
 51  
                 }
 52  114
                 return new HashSet<Object>(1);
 53  
         }
 54  
 
 55  
         /**
 56  
          * Creates a new set or multiset with initial contents.
 57  
          * 
 58  
          * @param multi <code>true</code> to create a multiset, <code>false</code>
 59  
          *        to create a regular set.
 60  
          * @param contents Initial contents of the (multi) set.
 61  
          * @return New HashSet or {@link HashMultiSet} with initial contents.
 62  
          * @since 2.3
 63  
          */
 64  
         public static Collection<?> createHashSet(boolean multi,
 65  
                         Collection<?> contents) {
 66  26
                 if (multi) {
 67  13
                         return new HashMultiSet<Object>(contents);
 68  
                 }
 69  13
                 return new HashSet<Object>(contents);
 70  
         }
 71  
 
 72  
         /**
 73  
          * Tests if a collection is a multiset.
 74  
          * 
 75  
          * @param c Collection to test.
 76  
          * @return <code>true</code> if c is an instance of {@link HashMultiSet}.
 77  
          */
 78  
         public static boolean isMultiSet(Collection<?> c) {
 79  83
                 return c instanceof HashMultiSet;
 80  
         }
 81  
 
 82  
         /**
 83  
          * Counts the number of occurrences of an element in a set or multiset.
 84  
          * 
 85  
          * @param c The set or multiset. This must be an instance of java.util.Set
 86  
          *        or {@link HashMultiSet}.
 87  
          * @param element The element to count.
 88  
          * @return If c is a multiset, the cardinality of the element in c.
 89  
          *         Otherwise 1 if c contains the element, 0 if c does not contain
 90  
          *         the element.
 91  
          */
 92  
         public static int elementCount(Collection<?> c, Object element) {
 93  42
                 if (isMultiSet(c)) {
 94  21
                         return ((HashMultiSet<?>) c).getElementCount(element);
 95  21
                 } else if (c.contains(element)) {
 96  15
                         return 1;
 97  
                 }
 98  6
                 return 0;
 99  
         }
 100  
 
 101  
         /**
 102  
          * Removes all occurrences of an element in a set or multiset.
 103  
          * 
 104  
          * @param c The set or multiset. This must be an instance of java.util.Set
 105  
          *        or {@link HashMultiSet}.
 106  
          * @param element The element to remove.
 107  
          */
 108  
         public static void removeAny(Collection<?> c, Object element) {
 109  4
                 if (isMultiSet(c)) {
 110  2
                         ((HashMultiSet<?>) c).removeAny(element);
 111  2
                 } else {
 112  2
                         c.remove(element);
 113  
                 }
 114  4
         }
 115  
 
 116  
         /**
 117  
          * Obtains an iterator over the unique elements of a set or multiset.
 118  
          * 
 119  
          * @param <T> Type of the elements in the set
 120  
          * @param c The set or multiset.
 121  
          * @return An iterator that, in the case of multisets, ignores the
 122  
          *         cardinality of elements and returns each element only once.
 123  
          */
 124  
         public static <T> Iterator<T> getFlatIterator(Collection<T> c) {
 125  13
                 if (isMultiSet(c)) {
 126  7
                         return ((HashMultiSet<T>) c).getFlatIterator();
 127  
                 }
 128  6
                 return c.iterator();
 129  
         }
 130  
 
 131  
         /**
 132  
          * Optimizes a float return value for a metric.
 133  
          * <p>
 134  
          * If the value has no fractional part and less than 6 significant digits,
 135  
          * its value is wrapped in an Integer.
 136  
          * 
 137  
          * @param f The return value of the metric
 138  
          * @return If possible, an Integer with the value of the float, otherwise a
 139  
          *         Float instance
 140  
          */
 141  
         public static Number getNumber(float f) {
 142  660104
                 if (f > -100000 && f < 100000) {
 143  600103
                         if (f == Math.floor(f)) {
 144  200101
                                 return Integer.valueOf((int) f);
 145  
                         }
 146  
                 }
 147  460003
                 return Float.valueOf(f);
 148  
         }
 149  
 
 150  
         /**
 151  
          * Checks if an object represents an empty model element reference. Empty
 152  
          * model element references are represented by empty strings.
 153  
          * 
 154  
          * @param o Object to test
 155  
          * @return <code>true</code> if <code>o</code> is an empty String
 156  
          * @since 2.3
 157  
          */
 158  
         public static boolean isEmptyElement(Object o) {
 159  12
                 return (o instanceof String) && "".equals(o);
 160  
         }
 161  
 }