1 22 package org.jboss.util; 23 24 import java.io.Serializable ; 25 26 32 public class LongCounter 33 implements Serializable , Cloneable 34 { 35 36 private long count; 37 38 43 public LongCounter(final long count) { 44 this.count = count; 45 } 46 47 50 public LongCounter() {} 51 52 57 public long increment() { 58 return ++count; 59 } 60 61 66 public long decrement() { 67 return --count; 68 } 69 70 75 public long getCount() { 76 return count; 77 } 78 79 82 public void reset() { 83 this.count = 0; 84 } 85 86 92 public boolean equals(final Object obj) { 93 if (obj == this) return true; 94 95 if (obj != null && obj.getClass() == getClass()) { 96 return ((LongCounter)obj).count == count; 97 } 98 99 return false; 100 } 101 102 107 public String toString() { 108 return String.valueOf(count); 109 } 110 111 116 public Object clone() { 117 try { 118 return super.clone(); 119 } 120 catch (CloneNotSupportedException e) { 121 throw new InternalError (); 122 } 123 } 124 125 126 130 133 private static class Wrapper 134 extends LongCounter 135 { 136 137 protected final LongCounter counter; 138 139 public Wrapper(final LongCounter counter) { 140 this.counter = counter; 141 } 142 143 public long increment() { 144 return counter.increment(); 145 } 146 147 public long decrement() { 148 return counter.decrement(); 149 } 150 151 public long getCount() { 152 return counter.getCount(); 153 } 154 155 public void reset() { 156 counter.reset(); 157 } 158 159 public boolean equals(final Object obj) { 160 return counter.equals(obj); 161 } 162 163 public String toString() { 164 return counter.toString(); 165 } 166 167 public Object clone() { 168 return counter.clone(); 169 } 170 } 171 172 178 public static LongCounter makeSynchronized(final LongCounter counter) 179 { 180 return new Wrapper(counter) { 181 public synchronized long increment() { 182 return this.counter.increment(); 183 } 184 185 public synchronized long decrement() { 186 return this.counter.decrement(); 187 } 188 189 public synchronized long getCount() { 190 return this.counter.getCount(); 191 } 192 193 public synchronized void reset() { 194 this.counter.reset(); 195 } 196 197 public synchronized int hashCode() { 198 return this.counter.hashCode(); 199 } 200 201 public synchronized boolean equals(final Object obj) { 202 return this.counter.equals(obj); 203 } 204 205 public synchronized String toString() { 206 return this.counter.toString(); 207 } 208 209 public synchronized Object clone() { 210 return this.counter.clone(); 211 } 212 }; 213 } 214 215 223 public static LongCounter makeDirectional(final LongCounter counter, 224 final boolean increasing) 225 { 226 LongCounter temp; 227 if (increasing) { 228 temp = new Wrapper(counter) { 229 public long decrement() { 230 throw new UnsupportedOperationException (); 231 } 232 233 public void reset() { 234 throw new UnsupportedOperationException (); 235 } 236 }; 237 } 238 else { 239 temp = new Wrapper(counter) { 240 public long increment() { 241 throw new UnsupportedOperationException (); 242 } 243 }; 244 } 245 246 return temp; 247 } 248 } 249 | Popular Tags |