Starsector API
Loading...
Searching...
No Matches
CountingMap.java
Go to the documentation of this file.
1
package
com.fs.starfarer.api.util;
2
3
import
java.util.LinkedHashMap;
4
5
public
class
CountingMap
<
K
>
extends
LinkedHashMap<K, Integer> {
6
private
static
final
long
serialVersionUID = 1
L
;
7
8
public
void
add
(
K
key,
int
quantity) {
9
Integer
val
=
super
.get(key);
10
if
(
val
==
null
)
val
= 0;
11
val
+=quantity;
12
put
(key,
val
);
13
}
14
15
public
void
add
(
K
key) {
16
add
(key, 1);
17
}
18
19
public
void
sub
(
K
key) {
20
sub
(key, 1);
21
}
22
public
void
sub
(
K
key,
int
quantity) {
23
Integer
val
=
super
.get(key);
24
if
(
val
==
null
)
val
= 0;
25
val
-=quantity;
26
if
(
val
<= 0) {
27
remove
(key);
28
return
;
29
}
30
put
(key,
val
);
31
}
32
33
public
int
getCount
(
K
key) {
34
Integer
val
=
super
.get(key);
35
if
(
val
==
null
)
val
= 0;
36
return
val
;
37
}
38
39
@
Override
40
public
Integer
get
(
Object
key) {
41
return
getCount
((
K
) key);
42
}
43
44
public
int
getTotal
() {
45
int
total = 0;
46
for
(
K
key :
keySet
()) {
47
total +=
get
(key);
48
}
49
return
total;
50
}
51
52
public
K
getLargest
() {
53
int
max = 0;
54
K
maxKey
=
null
;
55
for
(
K
key :
keySet
()) {
56
int
c =
getCount
(key);
57
if
(c > max) {
58
max = c;
59
maxKey
= key;
60
}
61
}
62
return
maxKey
;
63
}
64
65
}
com.fs.starfarer.api.util.CountingMap
Definition
CountingMap.java:5
com.fs.starfarer.api.util.CountingMap.sub
void sub(K key)
Definition
CountingMap.java:19
com.fs.starfarer.api.util.CountingMap.add
void add(K key, int quantity)
Definition
CountingMap.java:8
com.fs.starfarer.api.util.CountingMap.getTotal
int getTotal()
Definition
CountingMap.java:44
com.fs.starfarer.api.util.CountingMap.getCount
int getCount(K key)
Definition
CountingMap.java:33
com.fs.starfarer.api.util.CountingMap.add
void add(K key)
Definition
CountingMap.java:15
com.fs.starfarer.api.util.CountingMap.getLargest
K getLargest()
Definition
CountingMap.java:52
com.fs.starfarer.api.util.CountingMap.sub
void sub(K key, int quantity)
Definition
CountingMap.java:22
com.fs.starfarer.api.util.TimeoutTracker
Definition
TimeoutTracker.java:8
src
com
fs
starfarer
api
util
CountingMap.java
Generated by
1.9.8