1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.nuiton.validator.bean.simple;
23
24 import org.nuiton.validator.NuitonValidatorScope;
25
26 import java.io.Serializable;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.StringTokenizer;
30
31 import static org.nuiton.i18n.I18n.t;
32
33
34
35
36
37
38
39
40
41 public class SimpleBeanValidatorMessage<E extends SimpleBeanValidatorMessage<?>> implements Comparable<E>, Serializable {
42
43 private static final long serialVersionUID = 1L;
44
45
46 protected SimpleBeanValidator<?> validator;
47
48
49 protected String field;
50
51
52 protected String message;
53
54
55 protected NuitonValidatorScope scope;
56
57 public SimpleBeanValidatorMessage(SimpleBeanValidator<?> validator,
58 String field,
59 String message,
60 NuitonValidatorScope scope) {
61 this.field = field;
62 this.validator = validator;
63 this.message = message == null ? null : message.trim();
64 this.scope = scope;
65 }
66
67 public SimpleBeanValidator<?> getValidator() {
68 return validator;
69 }
70
71 public String getField() {
72 return field;
73 }
74
75 public NuitonValidatorScope getScope() {
76 return scope;
77 }
78
79 public String getMessage() {
80 return message;
81 }
82
83 @Override
84 public int compareTo(E o) {
85
86 int result = getScope().compareTo(o.getScope());
87 if (result == 0) {
88
89 result = field.compareTo(o.field);
90 if (result == 0) {
91
92 result = message.compareTo(o.message);
93 }
94 }
95 return result;
96 }
97
98 @Override
99 public boolean equals(Object o) {
100 if (this == o) {
101 return true;
102 }
103 if (!(o instanceof SimpleBeanValidatorMessage<?>)) {
104 return false;
105 }
106
107 SimpleBeanValidatorMessage<?> that = (SimpleBeanValidatorMessage<?>) o;
108
109 return field.equals(that.field) &&
110 (message != null ? !message.equals(that.message) : that.message == null) &&
111 scope == that.scope;
112 }
113
114 @Override
115 public int hashCode() {
116 int result = field.hashCode();
117 result = 31 * result + (message != null ? message.hashCode() : 0);
118 result = 31 * result + (scope != null ? scope.hashCode() : 0);
119 return result;
120 }
121
122 @Override
123 public String toString() {
124 return scope + " - " + getI18nError(message);
125 }
126
127 public String getI18nError(String error) {
128 String text;
129 if (!error.contains("##")) {
130 text = t(error);
131 } else {
132 StringTokenizer stk = new StringTokenizer(error, "##");
133 String errorName = stk.nextToken();
134 List<String> args = new ArrayList<String>();
135 while (stk.hasMoreTokens()) {
136 args.add(stk.nextToken());
137 }
138 text = t(errorName, args.toArray());
139 }
140 return text;
141 }
142 }