package org.jboss.util;
import java.io.Serializable;
public class LongCounter
implements Serializable, Cloneable
{
static final long serialVersionUID = 1615297462859270139L;
private long count;
public LongCounter(final long count) {
this.count = count;
}
public LongCounter() {}
public long increment() {
return ++count;
}
public long decrement() {
return --count;
}
public long getCount() {
return count;
}
public void reset() {
this.count = 0;
}
public boolean equals(final Object obj) {
if (obj == this) return true;
if (obj != null && obj.getClass() == getClass()) {
return ((LongCounter)obj).count == count;
}
return false;
}
public String toString() {
return String.valueOf(count);
}
public Object clone() {
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
private static class Wrapper
extends LongCounter
{
protected final LongCounter counter;
public Wrapper(final LongCounter counter) {
this.counter = counter;
}
public long increment() {
return counter.increment();
}
public long decrement() {
return counter.decrement();
}
public long getCount() {
return counter.getCount();
}
public void reset() {
counter.reset();
}
public boolean equals(final Object obj) {
return counter.equals(obj);
}
public String toString() {
return counter.toString();
}
public Object clone() {
return counter.clone();
}
}
public static LongCounter makeSynchronized(final LongCounter counter)
{
return new Wrapper(counter) {
public synchronized long increment() {
return this.counter.increment();
}
public synchronized long decrement() {
return this.counter.decrement();
}
public synchronized long getCount() {
return this.counter.getCount();
}
public synchronized void reset() {
this.counter.reset();
}
public synchronized int hashCode() {
return this.counter.hashCode();
}
public synchronized boolean equals(final Object obj) {
return this.counter.equals(obj);
}
public synchronized String toString() {
return this.counter.toString();
}
public synchronized Object clone() {
return this.counter.clone();
}
};
}
public static LongCounter makeDirectional(final LongCounter counter,
final boolean increasing)
{
LongCounter temp;
if (increasing) {
temp = new Wrapper(counter) {
public long decrement() {
throw new UnsupportedOperationException();
}
public void reset() {
throw new UnsupportedOperationException();
}
};
}
else {
temp = new Wrapper(counter) {
public long increment() {
throw new UnsupportedOperationException();
}
};
}
return temp;
}
}