-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCustomInputs.java
More file actions
110 lines (98 loc) · 3.87 KB
/
Copy pathCustomInputs.java
File metadata and controls
110 lines (98 loc) · 3.87 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
103
104
105
106
107
108
109
110
package com.maxmind.minfraud.request;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.maxmind.minfraud.AbstractModel;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
/**
* Custom inputs to be used in
* <a href="https://www.maxmind.com/en/accounts/current/minfraud-interactive/custom-rules">Custom
* Rules</a>.
* In order to use custom inputs, you must set them up from your account portal.
*/
public final class CustomInputs extends AbstractModel {
private final Map<String, Object> inputs;
private CustomInputs(Builder builder) {
inputs = Map.copyOf(builder.inputs);
}
/**
* {@code Builder} creates instances of {@code CustomInputs} from values set by the builder's
* methods.
*/
public static class Builder {
private static final long NUM_MAX = 10_000_000_000_000L;
private static final Pattern KEY_PATTERN = Pattern.compile("^[a-z0-9_]{1,25}$");
final Map<String, Object> inputs = new HashMap<>();
/**
* Add a string custom input.
*
* @param key The key for the custom input as defined on your account portal.
* @param value The custom input value. Must be less than 256 characters and must not
* contain new lines.
* @return The builder object.
* @throws IllegalArgumentException when the key or value are invalid.
*/
public Builder put(String key, String value) {
validateKey(key);
if (value.length() > 255 || value.contains("\n")) {
throw new IllegalArgumentException("The custom input string "
+ value + " is invalid. The string be less than"
+ "256 characters and the string must not contain a newline.");
}
inputs.put(key, value);
return this;
}
/**
* Add a numeric custom input.
*
* @param key The key for the custom input as defined on your account portal.
* @param value The custom input value. Must be between -10^13 and 10^13 exclusive.
* @return The builder object.
* @throws IllegalArgumentException when the key or value are invalid.
*/
public Builder put(String key, Number value) {
validateKey(key);
double doubleValue = value.doubleValue();
if (doubleValue <= -NUM_MAX || doubleValue >= NUM_MAX) {
throw new IllegalArgumentException(
"The custom input number " + value + "is invalid. "
+ "The number must be between -" + NUM_MAX
+ " and " + NUM_MAX + ", exclusive.");
}
inputs.put(key, value);
return this;
}
/**
* Add a boolean custom input.
*
* @param key The key for the custom input as defined on your account portal.
* @param value The custom input value.
* @return The builder object.
* @throws IllegalArgumentException when the key or value are invalid.
*/
public Builder put(String key, boolean value) {
validateKey(key);
inputs.put(key, value);
return this;
}
/**
* @return An instance of {@code CustomInputs} created from the fields set on this builder.
*/
public CustomInputs build() {
return new CustomInputs(this);
}
private void validateKey(String key) {
if (!KEY_PATTERN.matcher(key).matches()) {
throw new IllegalArgumentException("The custom input key "
+ key + " is invalid.");
}
}
}
/**
* @return an unmodifiable map containing the custom inputs.
*/
@JsonAnyGetter
public Map<String, Object> inputs() {
return inputs;
}
}