1 package com.github.mygreen.supercsv.validation;
2
3 import java.io.Serializable;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Optional;
7
8 import com.github.mygreen.supercsv.localization.MessageInterpolator;
9 import com.github.mygreen.supercsv.localization.MessageResolver;
10
11
12
13
14
15
16
17
18 public class CsvError implements Serializable {
19
20
21 private static final long serialVersionUID = -1561852232153053944L;
22
23
24
25
26 private final String objectName;
27
28
29
30
31
32 private final String[] codes;
33
34
35
36
37 private final Map<String, Object> variables;
38
39
40
41
42
43 private final String defaultMessage;
44
45 public CsvError(final String objectName, final String[] codes, final Map<String, Object> variables,
46 final String defaultMessaage) {
47 this.objectName = objectName;
48 this.codes = codes;
49 this.variables = variables;
50 this.defaultMessage = defaultMessaage;
51 }
52
53
54
55
56
57
58
59 public String format(final MessageResolver messageResolver, final MessageInterpolator messageInterpolator) {
60
61 for(String code : getCodes()) {
62 Optional<String> message = messageResolver.getMessage(code);
63 if(message.isPresent()) {
64 return messageInterpolator.interpolate(message.get(), getVariables(), true, messageResolver);
65 }
66
67 }
68
69
70 return messageInterpolator.interpolate(getDefaultMessage(), getVariables(), true, messageResolver);
71 }
72
73
74
75
76
77 public String getObjectName() {
78 return objectName;
79 }
80
81
82
83
84
85 public String[] getCodes() {
86 return codes;
87 }
88
89
90
91
92
93 public Map<String, Object> getVariables() {
94 return variables;
95 }
96
97
98
99
100
101 public String getDefaultMessage() {
102 return defaultMessage;
103 }
104
105 public static final class Builder {
106
107 private final String objectName;
108
109 private final String[] codes;
110
111 private Map<String, Object> variables = new HashMap<>();
112
113 private String defaultMessage;
114
115 public Builder(final String objectName, final String[] codes) {
116 this.objectName = objectName;
117 this.codes = codes;
118 }
119
120 public Builder variables(final Map<String, Object> variables) {
121 this.variables.putAll(variables);
122 return this;
123 }
124
125 public Builder variables(final String key, final Object value) {
126 this.variables.put(key, value);
127 return this;
128 }
129
130 public Builder defaultMessage(final String defaultMessage) {
131 this.defaultMessage = defaultMessage;
132 return this;
133 }
134
135 public CsvError build() {
136
137 return new CsvError(objectName, codes, variables, defaultMessage);
138 }
139
140 }
141
142 }