/
UsageUtils.java
96 lines (76 loc) · 3.54 KB
/
UsageUtils.java
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
/*
* Copyright 2014 The Billing Project, LLC
*
* Ning licenses this file to you 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.
*/
package org.killbill.billing.invoice.usage;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.killbill.billing.catalog.api.BillingMode;
import org.killbill.billing.catalog.api.Limit;
import org.killbill.billing.catalog.api.Tier;
import org.killbill.billing.catalog.api.TieredBlock;
import org.killbill.billing.catalog.api.Usage;
import org.killbill.billing.catalog.api.UsageType;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
public class UsageUtils {
public static List<Map<Integer,TieredBlock>> getConsumableInArrearTieredBlocks(final Usage usage, final String unitType) {
Preconditions.checkArgument(usage.getBillingMode() == BillingMode.IN_ARREAR && usage.getUsageType() == UsageType.CONSUMABLE);
Preconditions.checkArgument(usage.getTiers().length > 0);
final List<Map<Integer,TieredBlock>> result = Lists.newLinkedList();
int tierNum = 0;
for (Tier tier : usage.getTiers()) {
tierNum++;
for (TieredBlock tierBlock : tier.getTieredBlocks()) {
if (tierBlock.getUnit().getName().equals(unitType)) {
Map<Integer, TieredBlock> tieredBlockWithTierNum = new HashMap<>();
tieredBlockWithTierNum.put(tierNum, tierBlock);
result.add(tieredBlockWithTierNum);
}
}
}
return result;
}
public static Set<String> getConsumableInArrearUnitTypes(final Usage usage) {
Preconditions.checkArgument(usage.getBillingMode() == BillingMode.IN_ARREAR && usage.getUsageType() == UsageType.CONSUMABLE);
Preconditions.checkArgument(usage.getTiers().length > 0);
final Set<String> result = new HashSet<String>();
for (Tier tier : usage.getTiers()) {
for (TieredBlock tierBlock : tier.getTieredBlocks()) {
result.add(tierBlock.getUnit().getName());
}
}
return result;
}
public static List<Tier> getCapacityInArrearTier(final Usage usage) {
Preconditions.checkArgument(usage.getBillingMode() == BillingMode.IN_ARREAR && usage.getUsageType() == UsageType.CAPACITY);
Preconditions.checkArgument(usage.getTiers().length > 0);
return ImmutableList.copyOf(usage.getTiers());
}
public static Set<String> getCapacityInArrearUnitTypes(final Usage usage) {
Preconditions.checkArgument(usage.getBillingMode() == BillingMode.IN_ARREAR && usage.getUsageType() == UsageType.CAPACITY);
Preconditions.checkArgument(usage.getTiers().length > 0);
final Set<String> result = new HashSet<String>();
for (Tier tier : usage.getTiers()) {
for (Limit limit : tier.getLimits()) {
result.add(limit.getUnit().getName());
}
}
return result;
}
}