Repository navigation
Expand file tree
/
Copy pathSessionBudget.java
More file actions
53 lines (43 loc) · 1.55 KB
/
Copy pathSessionBudget.java
File metadata and controls
53 lines (43 loc) · 1.55 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
package com.qoder.cloudagents.forward;
import com.qoder.cloudagents.core.*;
import com.qoder.cloudagents.errors.ConfigurationException;
import java.math.BigDecimal;
import java.util.*;
/** Immutable JSON configuration; unknown fields are preserved. */
public final class SessionBudget extends JsonObject {
private SessionBudget(JsonValue value) {
super(value);
}
public static SessionBudget fromJson(JsonValue value) {
return new SessionBudget(value);
}
public String type() {
return optional("type", JsonValue::asString);
}
public String maxCreditCost() {
return optional("max_credit_cost", JsonValue::asString);
}
public static Builder builder() {
return new Builder();
}
public static final class Builder extends JsonObjectBuilder<Builder> {
private Builder() {}
public Builder type(String value) {
return put("type", value);
}
public Builder maxCreditCost(String value) {
return put("max_credit_cost", value);
}
public SessionBudget build() {
require("type", "max_credit_cost");
if (!"limit".equals(json().get("type").asString()))
throw new ConfigurationException("Budget type must be limit");
String cost = json().get("max_credit_cost").asString();
if (!cost.matches("[0-9]{1,12}(\\.[0-9]{1,2})?") || new BigDecimal(cost).signum() <= 0)
throw new ConfigurationException(
"maxCreditCost must be a positive decimal string with at most 12 integer and 2"
+ " fractional digits");
return new SessionBudget(json());
}
}
}