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