View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.util;
22  
23  import java.io.IOException;
24  import java.io.Writer;
25  import java.util.*;
26  import java.util.HashMap;
27  import java.util.Map;
28  import org.onemind.commons.java.lang.MutableLong;
29  /***
30   * For counting things
31   * @author TiongHiang Lee (thlee@onemindsoft.org)
32   * @version $Id: Counter.java,v 1.2 2005/06/22 22:58:25 thlee Exp $ $Name:  $
33   */
34  public class Counter
35  {
36  
37      /*** the counts **/
38      private final Map counts = new HashMap();
39  
40      /***
41       * Constructor
42       */
43      public Counter()
44      {
45      }
46  
47      /***
48       * Add count. Count as 1 if it is not counted before
49       * @param counted
50       */
51      public void count(Object counted)
52      {
53          count(counted, 1);
54      }
55  
56      /***
57       * Adjust count by value
58       * @param counted the counted
59       * @param countValue the count value
60       */
61      public void count(Object counted, long countValue)
62      {
63          MutableLong count = (MutableLong) counts.get(counted);
64          if (count == null)
65          {
66              counts.put(counted, new MutableLong(countValue));
67          } else
68          {
69              count.inc(countValue);
70          }
71      }
72  
73      /***
74       * Remove the count. Count as -1 if it is not counted before
75       * @param counted the counted
76       */
77      public void removeCount(Object counted)
78      {
79          MutableLong count = (MutableLong) counts.get(counted);
80          if (count == null)
81          {
82              counts.put(counted, new MutableLong(-1));
83          } else
84          {
85              count.dec(1);
86          }
87      }
88  
89      /***
90       * Get the count
91       * @param counted the counted
92       * @return the count
93       */
94      public long getCount(Object counted)
95      {
96          MutableLong count = (MutableLong) counts.get(counted);
97          if (count == null)
98          {
99              return 0;
100         } else
101         {
102             return count.longValue();
103         }
104     }
105 
106     /***
107      * Dump to output
108      * @param writer the writer
109      */
110     public void dump(Writer writer) throws IOException
111     {
112         MapUtils.dump(counts, writer);
113     }
114 
115     /***
116      * Reset the count for counted
117      * @param counted the counted
118      */
119     public void resetCount(Object counted)
120     {
121         counts.remove(counted);
122     }
123 
124     /***
125      * Reset all the counters
126      */
127     public void resetAll()
128     {
129         counts.clear();
130     }
131 
132     public String toString()
133     {
134         StringBuffer sb = new StringBuffer(super.toString());
135         sb.append(" - [");
136         Iterator it = counts.keySet().iterator();
137         while (it.hasNext())
138         {
139             Object obj = it.next();
140             sb.append(obj);
141             sb.append("=");
142             sb.append(counts.get(obj));
143             if (it.hasNext())
144             {
145                 sb.append(", ");
146             }
147         }
148         sb.append("]");
149         return sb.toString();
150     }
151 }