1 8 package com.mountainminds.eclemma.internal.core.analysis; 9 10 import com.mountainminds.eclemma.core.analysis.ICounter; 11 12 19 public abstract class Counter implements ICounter { 20 21 22 private static final int SINGLETON_LIMIT = 10; 23 24 private static final Counter[][] SINGLETONS = new Counter[SINGLETON_LIMIT + 1][]; 25 26 static { 27 for (int i = 0; i <= SINGLETON_LIMIT; i++) { 28 SINGLETONS[i] = new Counter[i + 1]; 29 for (int j = 0; j <= i; j++) SINGLETONS[i][j] = new Fix(i, j); 30 } 31 } 32 33 34 public static final Counter COUNTER_0_0 = SINGLETONS[0][0]; 35 36 39 private static class Var extends Counter { 40 public Var(long total, long covered) { 41 super(total, covered); 42 } 43 public Counter increment(int total, int covered) { 44 this.total += total; 45 this.covered += covered; 46 return this; 47 } 48 } 49 50 53 private static class Fix extends Counter { 54 public Fix(long total, long covered) { 55 super(total, covered); 56 } 57 public Counter increment(int total, int covered) { 58 return getInstance(this.total + total, this.covered + covered); 59 } 60 } 61 62 69 public static Counter getInstance(long total, long covered) { 70 if (total <= SINGLETON_LIMIT && covered <= total) { 71 return SINGLETONS[(int) total][(int) covered]; 72 } else { 73 return new Var(total, covered); 74 } 75 } 76 77 protected long total; 78 protected long covered; 79 80 protected Counter(long total, long covered) { 81 this.total = total; 82 this.covered = covered; 83 } 84 85 93 public abstract Counter increment(int total, int covered); 94 95 97 public long getTotalCount() { 98 return total; 99 } 100 101 public long getCoveredCount() { 102 return covered; 103 } 104 105 public double getRatio() { 106 return (double) covered / (double) total; 107 } 108 109 public int compareTo(Object obj) { 110 ICounter counter = (ICounter) obj; 111 return Double.compare(getRatio(), counter.getRatio()); 112 } 113 114 public boolean equals(Object obj) { 115 if (obj instanceof ICounter) { 116 ICounter counter = (ICounter) obj; 117 return getTotalCount() == counter.getTotalCount() 118 && getCoveredCount() == counter.getCoveredCount(); 119 } else { 120 return false; 121 } 122 } 123 124 public int hashCode() { 125 long t = getTotalCount(); 126 long c = 17 * getCoveredCount(); 127 return (int) (t ^ (t >>> 32) ^ c ^ (c >>> 32)); 128 } 129 130 public String toString() { 131 StringBuffer b = new StringBuffer ("Counter["); b.append(getCoveredCount()); 133 b.append('/').append(getTotalCount()); 134 b.append(']'); 135 return b.toString(); 136 } 137 138 } 139 | Popular Tags |