-
Notifications
You must be signed in to change notification settings - Fork 461
/
DataType.java
145 lines (122 loc) · 4.54 KB
/
DataType.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.dotmarketing.portlets.rules.parameter.type;
import com.dotcms.repackage.com.google.common.collect.ImmutableMap;
import com.dotcms.repackage.com.google.common.collect.Maps;
import com.dotmarketing.portlets.rules.parameter.type.constraint.TypeConstraint;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.Collections;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.Nullable;
/**
* @author Geoff M. Granum
*/
public abstract class DataType<T> {
private final String id;
private final String errorMessageKey;
private T defaultValue;
private Map<String, TypeConstraint> restrictions = Maps.newHashMap();
public DataType(String id, String errorMessageKey) {
this.id = id;
this.errorMessageKey = errorMessageKey;
}
public DataType(String id, String errorMessageKey, Map<String, TypeConstraint> restrictions) {
this.id = id;
this.errorMessageKey = errorMessageKey;
this.restrictions = restrictions;
}
public String getId() {
return id;
}
public String getErrorMessageKey() {
return errorMessageKey;
}
public abstract T convert(String from);
public T getDefaultValue() {
return defaultValue;
}
public DataType<T> defaultValue(T defaultValue) {
this.defaultValue = defaultValue;
return this;
}
/**
* Verify that a value can be represented by this DataType. If valid an empty list will
* be returned. If invalid a map containing each failed constraint will be returned.
* @param value The string representation of the value to be verified.
* @return A map of Errors
*/
public final Map<String, String> verify(String value){
Map<String, String> errors = null;
for (TypeConstraint constraint : restrictions.values()) {
if(!constraint.fn.apply(value)) {
if(errors == null) {
errors = Maps.newHashMap();
}
errors.put(constraint.id, value);
}
}
return errors == null ? Collections.emptyMap() : errors;
}
public void checkValid(String value){
Map<String, String> errors = verify(value);
if(!errors.isEmpty()){
String failedMsg = "Constraint(s) failed: [" + StringUtils.join(errors.keySet(), ",") + "]";
throw new ParameterNotValidException(failedMsg);
}
}
public DataType<T> restrict(TypeConstraint restriction) {
this.restrictions.put(restriction.id, restriction);
return this;
}
public Map<String, TypeConstraint> getConstraints() {
return ImmutableMap.copyOf(this.restrictions);
}
/**
* Utility type used to correctly read immutable object from JSON representation.
*
* @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
@Deprecated
@JsonDeserialize
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json {
@javax.annotation.Nullable
String id;
@javax.annotation.Nullable
String errorMessageKey;
@javax.annotation.Nullable
Map<String, TypeConstraint> restrictions;
@JsonProperty("id")
public void setKey(@Nullable String id) {
this.id = id;
}
@JsonProperty("errorMessageKey")
public void setDataType(@Nullable String errorMessageKey) {
this.errorMessageKey = errorMessageKey;
}
@JsonProperty("constraints")
public void setRestrictions(@Nullable Map<String, TypeConstraint> restrictions) {
this.restrictions = restrictions;
}
}
/**
* @param json A JSON-bindable data structure
* @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
@Deprecated
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
static DataType fromJson(DataType.Json json) {
return new DataType(json.id, json.errorMessageKey, json.restrictions) {
@Override
public Object convert(String from) {
throw new UnsupportedOperationException();
}
};
}
}