Class ObjectCounter

 1 /*
 2  * Copyright (c) 2016 Vivid Solutions.
 3  *
 4  * All rights reserved. This program and the accompanying materials
 5  * are made available under the terms of the Eclipse Public License 2.0
 6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 8  * and the Eclipse Distribution License is available at
 9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12 package org.locationtech.jts.util;
13  
14 import java.util.HashMap;
15 import java.util.Map;
16  
17 /**
18  * Counts occurrences of objects.
19  * 
20  * @author Martin Davis
21  *
22  */
23 public class ObjectCounter 
24 {
25  
26   private Map counts = new HashMap();
27   
28   public ObjectCounter() {
29   }
30  
31   public void add(Object o)
32   {
33     Counter counter = (Counter) counts.get(o);
34     if (counter == null)
35       counts.put(o, new Counter(1));
36     else
37       counter.increment();
38   }
39   
40   // TODO: add remove(Object o)
41   
42   public int count(Object o)
43   {
44     Counter counter = (Counter) counts.get(o);
45     if (counter == null)
46       return 0;
47     else
48       return counter.count();
49    
50   }
51   private static class Counter
52   {
53     int count = 0;
54     
55     public Counter()
56     {
57       
58     }
59     
60     public Counter(int count)
61     {
62       this.count = count;
63     }
64     
65     public int count()
66     {
67       return count;
68     }
69     
70     public void increment()
71     {
72       count++;
73     }
74   }
75 }
76