1 22 package org.jboss.util; 23 24 import java.io.Serializable ; 25 26 32 public class Counter 33 implements Serializable , Cloneable 34 { 35 36 private int count; 37 38 43 public Counter(final int count) { 44 this.count = count; 45 } 46 47 50 public Counter() {} 51 52 57 public int increment() { 58 return ++count; 59 } 60 61 66 public int decrement() { 67 return --count; 68 } 69 70 75 public int 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 ((Counter)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 Counter 135 { 136 137 protected final Counter counter; 138 139 public Wrapper(final Counter counter) { 140 this.counter = counter; 141 } 142 143 public int increment() { 144 return counter.increment(); 145 } 146 147 public int decrement() { 148 return counter.decrement(); 149 } 150 151 public int 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 Counter makeSynchronized(final Counter counter) { 179 return new Wrapper(counter) { 180 public synchronized int increment() { 181 return this.counter.increment(); 182 } 183 184 public synchronized int decrement() { 185 return this.counter.decrement(); 186 } 187 188 public synchronized int getCount() { 189 return this.counter.getCount(); 190 } 191 192 public synchronized void reset() { 193 this.counter.reset(); 194 } 195 196 public synchronized int hashCode() { 197 return this.counter.hashCode(); 198 } 199 200 public synchronized boolean equals(final Object obj) { 201 return this.counter.equals(obj); 202 } 203 204 public synchronized String toString() { 205 return this.counter.toString(); 206 } 207 208 public synchronized Object clone() { 209 return this.counter.clone(); 210 } 211 }; 212 } 213 214 222 public static Counter makeDirectional(final Counter counter, 223 final boolean increasing) 224 { 225 Counter temp; 226 if (increasing) { 227 temp = new Wrapper(counter) { 228 public int decrement() { 229 throw new UnsupportedOperationException (); 230 } 231 232 public void reset() { 233 throw new UnsupportedOperationException (); 234 } 235 }; 236 } 237 else { 238 temp = new Wrapper(counter) { 239 public int increment() { 240 throw new UnsupportedOperationException (); 241 } 242 }; 243 } 244 245 return temp; 246 } 247 } 248 | Popular Tags |