-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDescPriceIndex.java
More file actions
58 lines (49 loc) · 1.43 KB
/
Copy pathDescPriceIndex.java
File metadata and controls
58 lines (49 loc) · 1.43 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
import java.util.Collection;
import java.util.TreeMap;
/**
* Collection of descriptions and count of items indexed by their price
*
* @author G94
*/
public class DescPriceIndex {
TreeMap<Long, PriceIndex> map; /*
* item counts by price indexed again by
* descriptions
*/
public DescPriceIndex() {
this.map = new TreeMap<>();
}
void increment(long description, long price) {
PriceIndex counts = map.get(description);
if (counts == null) {
counts = new PriceIndex();
map.put(description, counts);
}
counts.increment(price);
}
void increment(Collection<Long> descriptions, long price) {
for (Long description : descriptions)
increment(description, price);
}
void decrement(long description, long price) {
PriceIndex counts = map.get(description);
if (counts != null)
counts.decrement(price);
}
void decrement(Collection<Long> descriptions, long price) {
for (Long description : descriptions)
decrement(description, price);
}
long findMinPrice(long des) {
PriceIndex counts = map.get(des);
return counts != null ? counts.findMinPrice() : 0;
}
long findMaxPrice(long des) {
PriceIndex counts = map.get(des);
return counts != null ? counts.findMaxPrice() : 0;
}
int findPriceRange(long des, long lowPrice, long highPrice) {
PriceIndex counts = map.get(des);
return counts != null ? counts.range(lowPrice, highPrice) : 0;
}
}