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