Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Counters |
|
| 1.2;1.2 |
1 | package org.jtheque.utils.count; | |
2 | ||
3 | /* | |
4 | * This file is part of JTheque. | |
5 | * | |
6 | * JTheque is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License. | |
9 | * | |
10 | * JTheque is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | import java.util.HashMap; | |
20 | import java.util.Iterator; | |
21 | import java.util.Map; | |
22 | import java.util.Map.Entry; | |
23 | ||
24 | /** | |
25 | * A collections of counters. | |
26 | * | |
27 | * @author Baptiste Wicht | |
28 | */ | |
29 | public final class Counters implements Iterable<Entry<String, Counter>> { | |
30 | private final Map<String, Counter> counters; | |
31 | ||
32 | /** | |
33 | * Construct a new Collection of counters. | |
34 | */ | |
35 | public Counters() { | |
36 | 4 | super(); |
37 | ||
38 | 4 | counters = new HashMap<String, Counter>(10); |
39 | 4 | } |
40 | ||
41 | /** | |
42 | * Add a counter with a specific name to the collection. | |
43 | * | |
44 | * @param name The name of the new counter. | |
45 | */ | |
46 | public void addCounter(String name) { | |
47 | 4 | counters.put(name, new Counter()); |
48 | 4 | } |
49 | ||
50 | /** | |
51 | * Return the counter named with the specific name. | |
52 | * | |
53 | * @param name The name of the counter we search. | |
54 | * @return The searched counter. | |
55 | */ | |
56 | public Counter getCounter(String name) { | |
57 | 14 | return counters.get(name); |
58 | } | |
59 | ||
60 | /** | |
61 | * Return the named counter or a new if the counter doesn't exist. | |
62 | * | |
63 | * @param name The name of the counter. | |
64 | * @return The searched counter or a new if we doesn't find one counter. | |
65 | */ | |
66 | public Counter getCounterOrAdd(String name) { | |
67 | 4 | Counter counter = counters.get(name); |
68 | ||
69 | 4 | if (counter == null) { |
70 | 4 | counter = new Counter(); |
71 | 4 | counters.put(name, counter); |
72 | } | |
73 | ||
74 | 4 | return counter; |
75 | } | |
76 | ||
77 | @Override | |
78 | public Iterator<Entry<String, Counter>> iterator() { | |
79 | 0 | return counters.entrySet().iterator(); |
80 | } | |
81 | } |