001    /*
002     * Cobertura - http://cobertura.sourceforge.net/
003     *
004     * Copyright (C) 2006 Jiri Mares
005     *
006     * Cobertura is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published
008     * by the Free Software Foundation; either version 2 of the License,
009     * or (at your option) any later version.
010     *
011     * Cobertura is distributed in the hope that it will be useful, but
012     * WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014     * General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with Cobertura; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019     * USA
020     */
021    
022    package net.sourceforge.cobertura.coveragedata;
023    
024    import java.io.Serializable;
025    
026    /**
027     * <p>
028     * This class implements HasBeenInstrumented so that when cobertura instruments
029     * itself, it will omit this class. It does this to avoid an infinite recursion
030     * problem because instrumented classes make use of this class.
031     * </p>
032     */
033    public class JumpData implements BranchCoverageData, Comparable, Serializable,
034                    HasBeenInstrumented
035    {
036            private static final long serialVersionUID = 8;
037    
038            private int conditionNumber;
039    
040            private long trueHits;
041    
042            private long falseHits;
043    
044            JumpData(int conditionNumber)
045            {
046                    super();
047                    this.conditionNumber = conditionNumber;
048                    this.trueHits = 0L;
049                    this.falseHits = 0L;
050            }
051    
052            public int compareTo(Object o)
053            {
054                    if (!o.getClass().equals(JumpData.class))
055                            return Integer.MAX_VALUE;
056                    return this.conditionNumber - ((JumpData) o).conditionNumber;
057            }
058    
059            void touchBranch(boolean branch)
060            {
061                    if (branch)
062                    {
063                            this.trueHits++;
064                    }
065                    else
066                    {
067                            this.falseHits++;
068                    }
069            }
070    
071            public int getConditionNumber()
072            {
073                    return this.conditionNumber;
074            }
075    
076            public long getTrueHits()
077            {
078                    return this.trueHits;
079            }
080    
081            public long getFalseHits()
082            {
083                    return this.falseHits;
084            }
085    
086            public double getBranchCoverageRate()
087            {
088                    return ((double) getNumberOfCoveredBranches()) / getNumberOfValidBranches();
089            }
090    
091            public boolean equals(Object obj)
092            {
093                    if (this == obj)
094                            return true;
095                    if ((obj == null) || !(obj.getClass().equals(this.getClass())))
096                            return false;
097    
098                    JumpData branchData = (JumpData) obj;
099                    return (this.trueHits == branchData.trueHits)
100                                    && (this.falseHits == branchData.falseHits)
101                                    && (this.conditionNumber == branchData.conditionNumber);
102            }
103    
104            public int hashCode()
105            {
106                    return this.conditionNumber;
107            }
108    
109            public int getNumberOfCoveredBranches()
110            {
111                    return ((trueHits > 0) ? 1 : 0) + ((falseHits > 0) ? 1: 0);
112            }
113    
114            public int getNumberOfValidBranches()
115            {
116                    return 2;
117            }
118    
119            public void merge(BranchCoverageData coverageData)
120            {
121                    JumpData jumpData = (JumpData) coverageData;
122                    this.trueHits += jumpData.trueHits;
123                    this.falseHits += jumpData.falseHits;
124            }
125    
126    }