Coverage Report - net.sourceforge.cobertura.coveragedata.SwitchData
 
Classes in this File Line Coverage Branch Coverage Complexity
SwitchData
N/A
N/A
2.214
 
 1  
 /*
 2  
  * Cobertura - http://cobertura.sourceforge.net/
 3  
  *
 4  
  * Copyright (C) 2006 Jiri Mares
 5  
  *
 6  
  * Cobertura is free software; you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published
 8  
  * by the Free Software Foundation; either version 2 of the License,
 9  
  * or (at your option) any later version.
 10  
  *
 11  
  * Cobertura is distributed in the hope that it will be useful, but
 12  
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 14  
  * General Public License for more details.
 15  
  *
 16  
  * You should have received a copy of the GNU General Public License
 17  
  * along with Cobertura; if not, write to the Free Software
 18  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 19  
  * USA
 20  
  */
 21  
 
 22  
 package net.sourceforge.cobertura.coveragedata;
 23  
 
 24  
 import java.io.Serializable;
 25  
 import java.util.Arrays;
 26  
 
 27  
 /**
 28  
  * <p>
 29  
  * This class implements HasBeenInstrumented so that when cobertura instruments
 30  
  * itself, it will omit this class. It does this to avoid an infinite recursion
 31  
  * problem because instrumented classes make use of this class.
 32  
  * </p>
 33  
  */
 34  
 public class SwitchData implements BranchCoverageData, Comparable, Serializable,
 35  
                 HasBeenInstrumented
 36  
 {
 37  
         private static final long serialVersionUID = 9;
 38  
 
 39  
         private int switchNumber;
 40  
         
 41  
         private long defaultHits;
 42  
 
 43  
         private long[] hits;
 44  
         
 45  
         private int[] keys;
 46  
 
 47  
         public SwitchData(int switchNumber, int[] keys)
 48  
         {
 49  
                 super();
 50  
                 this.switchNumber = switchNumber;
 51  
                 defaultHits = 0;
 52  
                 hits = new long[keys.length];
 53  
                 Arrays.fill(hits, 0);
 54  
                 this.keys = new int[keys.length];
 55  
                 System.arraycopy(keys, 0, this.keys, 0, keys.length);
 56  
         }
 57  
 
 58  
         public SwitchData(int switchNumber, int min, int max)
 59  
         {
 60  
                 super();
 61  
                 this.switchNumber = switchNumber;
 62  
                 defaultHits = 0;
 63  
                 hits = new long[max - min + 1];
 64  
                 Arrays.fill(hits, 0);
 65  
                 this.keys = new int[max - min + 1];
 66  
                 for (int i = 0; min <= max; keys[i++] = min++);
 67  
         }
 68  
 
 69  
         public SwitchData(int switchNumber)
 70  
         {
 71  
                 this(switchNumber, new int[0]);
 72  
         }
 73  
 
 74  
         public int compareTo(Object o)
 75  
         {
 76  
                 if (!o.getClass().equals(SwitchData.class))
 77  
                         return Integer.MAX_VALUE;
 78  
                 return this.switchNumber - ((SwitchData) o).switchNumber;
 79  
         }
 80  
         
 81  
         void touchBranch(int branch) 
 82  
         {
 83  
                 if (branch == -1)
 84  
                         defaultHits++;
 85  
                 else 
 86  
                 {
 87  
                         if (hits.length <= branch)
 88  
                         {
 89  
                                 long[] old = hits;
 90  
                                 hits = new long[branch + 1];
 91  
                                 System.arraycopy(old, 0, hits, 0, old.length);
 92  
                                 Arrays.fill(hits, old.length, hits.length - 1, 0);
 93  
                         }
 94  
                         hits[branch]++;
 95  
                 }
 96  
         }
 97  
         
 98  
         public int getSwitchNumber()
 99  
         {
 100  
                 return this.switchNumber;
 101  
         }
 102  
 
 103  
         public long getHits(int branch)
 104  
         {
 105  
                 if (hits.length > branch)
 106  
                         return hits[branch];
 107  
                 return -1;
 108  
         }
 109  
 
 110  
         public long getDefaultHits()
 111  
         {
 112  
                 return defaultHits;
 113  
         }
 114  
 
 115  
         public double getBranchCoverageRate()
 116  
         {
 117  
                 int branches = hits.length + 1;
 118  
                 int hit = (defaultHits > 0) ? 1 : 0;
 119  
                 for (int i = hits.length - 1; i >= 0; hit += ((hits[i--] > 0) ? 1 : 0));
 120  
                 return ((double) hit) / branches;
 121  
         }
 122  
 
 123  
         public boolean equals(Object obj)
 124  
         {
 125  
                 if (this == obj)
 126  
                         return true;
 127  
                 if ((obj == null) || !(obj.getClass().equals(this.getClass())))
 128  
                         return false;
 129  
 
 130  
                 SwitchData switchData = (SwitchData) obj;
 131  
                 return (this.defaultHits == switchData.defaultHits)
 132  
                                 && (Arrays.equals(this.hits, switchData.hits))
 133  
                                 && (this.switchNumber == switchData.switchNumber);
 134  
         }
 135  
 
 136  
         public int hashCode()
 137  
         {
 138  
                 return this.switchNumber;
 139  
         }
 140  
 
 141  
         public int getNumberOfCoveredBranches()
 142  
         {
 143  
                 int ret = (defaultHits > 0) ? 1 : 0;
 144  
                 for (int i = hits.length -1; i >= 0;i--) 
 145  
                 {
 146  
                         if (hits[i] > 0) ret++;
 147  
                 }
 148  
                 return ret;
 149  
         }
 150  
 
 151  
         public int getNumberOfValidBranches()
 152  
         {
 153  
                 return hits.length + 1;
 154  
         }
 155  
 
 156  
         public void merge(BranchCoverageData coverageData)
 157  
         {
 158  
                 SwitchData switchData = (SwitchData) coverageData;
 159  
                 defaultHits += switchData.defaultHits;
 160  
                 for (int i = Math.min(hits.length, switchData.hits.length) - 1; i >= 0; i--)
 161  
                         hits[i] += switchData.hits[i];
 162  
                 if (switchData.hits.length > hits.length)
 163  
                 {
 164  
                         long[] old = hits;
 165  
                         hits = new long[switchData.hits.length];
 166  
                         System.arraycopy(old, 0, hits, 0, old.length);
 167  
                         System.arraycopy(switchData.hits, old.length, hits, old.length, hits.length - old.length);
 168  
                 }
 169  
                 if ((this.keys.length == 0) && (switchData.keys.length > 0))
 170  
                         this.keys = switchData.keys;
 171  
         }
 172  
 
 173  
 }