forked from fizzed/cloudhopper-smpp
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathConcurrentCommandStatusCounter.java
More file actions
102 lines (87 loc) · 2.94 KB
/
ConcurrentCommandStatusCounter.java
File metadata and controls
102 lines (87 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.cloudhopper.smpp.util;
/*
* #%L
* ch-smpp
* %%
* Copyright (C) 2009 - 2015 Cloudhopper by Twitter
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
* @author joelauer
*/
public class ConcurrentCommandStatusCounter {
private ConcurrentHashMap<Integer,AtomicInteger> map;
public ConcurrentCommandStatusCounter() {
this.map = new ConcurrentHashMap<Integer,AtomicInteger>();
}
public void reset() {
this.map.clear();
}
public ConcurrentCommandStatusCounter copy() {
ConcurrentCommandStatusCounter copy = new ConcurrentCommandStatusCounter();
for (Map.Entry<Integer,AtomicInteger> entry : this.map.entrySet()) {
this.map.put(entry.getKey(), new AtomicInteger(entry.getValue().get()));
}
return copy;
}
public int get(int commandStatus) {
Integer key = Integer.valueOf(commandStatus);
AtomicInteger val = map.get(key);
if (val == null) {
return -1;
} else {
return val.get();
}
}
public int incrementAndGet(int commandStatus) {
Integer key = Integer.valueOf(commandStatus);
AtomicInteger val = map.get(key);
if (val == null) {
val = new AtomicInteger(0);
map.put(key, val);
}
return val.incrementAndGet();
}
public SortedMap<Integer,Integer> createSortedMapSnapshot() {
SortedMap<Integer,Integer> sortedMap = new TreeMap<Integer,Integer>();
for (Map.Entry<Integer,AtomicInteger> entry : this.map.entrySet()) {
sortedMap.put(entry.getKey(), new Integer(entry.getValue().get()));
}
return sortedMap;
}
@Override
public String toString() {
if (this.map.isEmpty()) {
return "";
}
SortedMap<Integer,Integer> sortedMap = createSortedMapSnapshot();
StringBuilder to = new StringBuilder();
for (Map.Entry<Integer,Integer> entry : sortedMap.entrySet()) {
if (to.length() != 0) {
to.append(" ");
}
to.append(entry.getKey());
to.append("=");
to.append(entry.getValue());
}
return to.toString();
}
}