首页

分享onemind的commons-java包实现的计算器Counter的源码完整示例代码

标签:commons-java,onemind,计数器,counter     发布时间:2018-01-08   

一、前言

关于onemind的commons-java源码包(1.5.5)中org.onemind.commons.java.util.Counter、org.onemind.commons.java.lang.MutableLong设计实现计数器的功能,详情参见如下源码说明部分。

二、源码说明

1.Counter类

package org.onemind.commons.java.util;@b@@b@import java.io.IOException;@b@import java.io.Writer;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.Set;@b@import org.onemind.commons.java.lang.MutableLong;@b@@b@public class Counter@b@{@b@  private final Map counts = new HashMap();@b@@b@  public void count(Object counted)@b@  {@b@    count(counted, 1L);@b@  }@b@@b@  public void count(Object counted, long countValue)@b@  {@b@    MutableLong count = (MutableLong)this.counts.get(counted);@b@    if (count == null)@b@    {@b@      this.counts.put(counted, new MutableLong(countValue));@b@    }@b@    else@b@      count.inc(countValue);@b@  }@b@@b@  public void removeCount(Object counted)@b@  {@b@    MutableLong count = (MutableLong)this.counts.get(counted);@b@    if (count == null)@b@    {@b@      this.counts.put(counted, new MutableLong(-1L));@b@    }@b@    else@b@      count.dec(1L);@b@  }@b@@b@  public long getCount(Object counted)@b@  {@b@    MutableLong count = (MutableLong)this.counts.get(counted);@b@    if (count == null)@b@    {@b@      return 0L;@b@    }@b@@b@    return count.longValue();@b@  }@b@@b@  public void dump(Writer writer)@b@    throws IOException@b@  {@b@    MapUtils.dump(this.counts, writer);@b@  }@b@@b@  public void resetCount(Object counted)@b@  {@b@    this.counts.remove(counted);@b@  }@b@@b@  public void resetAll()@b@  {@b@    this.counts.clear();@b@  }@b@@b@  public String toString()@b@  {@b@    StringBuffer sb = new StringBuffer(super.toString());@b@    sb.append(" - [");@b@    Iterator it = this.counts.keySet().iterator();@b@    while (it.hasNext())@b@    {@b@      Object obj = it.next();@b@      sb.append(obj);@b@      sb.append("=");@b@      sb.append(this.counts.get(obj));@b@      if (it.hasNext())@b@      {@b@        sb.append(", ");@b@      }@b@    }@b@    sb.append("]");@b@    return sb.toString();@b@  }@b@}

2.MutableLong类

package org.onemind.commons.java.lang;@b@@b@public class MutableLong extends Number@b@{@b@  private long _value;@b@@b@  public MutableLong(long l)@b@  {@b@    this._value = l;@b@  }@b@@b@  public final void set(long l)@b@  {@b@    this._value = l;@b@  }@b@@b@  public final byte byteValue()@b@  {@b@    return (byte)(int)this._value;@b@  }@b@@b@  public final double doubleValue()@b@  {@b@    return this._value;@b@  }@b@@b@  public final float floatValue()@b@  {@b@    return (float)this._value;@b@  }@b@@b@  public final int intValue()@b@  {@b@    return (int)this._value;@b@  }@b@@b@  public final long longValue()@b@  {@b@    return this._value;@b@  }@b@@b@  public final short shortValue()@b@  {@b@    return (short)(int)this._value;@b@  }@b@@b@  public void inc(long i)@b@  {@b@    this._value += i;@b@  }@b@@b@  public void dec(long i)@b@  {@b@    this._value -= i;@b@  }@b@@b@  public String toString()@b@  {@b@    return String.valueOf(this._value);@b@  }@b@}