23
23
24
24
/**
25
25
* A simple log message type for use with Commons Logging, allowing
26
- * for convenient late resolution of a given {@link Supplier} instance
27
- * (typically bound to a Java 8 lambda expression) in {@link #toString()}.
26
+ * for convenient lazy resolution of a given {@link Supplier} instance
27
+ * (typically bound to a Java 8 lambda expression) or a printf-style
28
+ * format string ({@link String#format})in its {@link #toString()}.
28
29
*
29
30
* @author Juergen Hoeller
30
31
* @since 5.2
32
+ * @see #lazy(Supplier)
33
+ * @see #format(String, Object)
34
+ * @see #format(String, Object...)
31
35
* @see org.apache.commons.logging.Log#fatal(Object)
32
36
* @see org.apache.commons.logging.Log#error(Object)
33
37
* @see org.apache.commons.logging.Log#warn(Object)
34
38
* @see org.apache.commons.logging.Log#info(Object)
35
39
* @see org.apache.commons.logging.Log#debug(Object)
36
40
* @see org.apache.commons.logging.Log#trace(Object)
37
41
*/
38
- public class LogMessage {
39
-
40
- private final Supplier <? extends CharSequence > supplier ;
42
+ public abstract class LogMessage implements CharSequence {
41
43
42
44
@ Nullable
43
45
private String result ;
44
46
45
47
46
- /**
47
- * Construct a new {@code LogMessage} for the given supplier.
48
- * @param supplier the lazily resolving supplier
49
- * (typically bound to a Java 8 lambda expression)
50
- */
51
- public LogMessage ( Supplier <? extends CharSequence > supplier ) {
52
- Assert . notNull ( supplier , "Supplier must not be null" );
53
- this . supplier = supplier ;
48
+ @ Override
49
+ public int length () {
50
+ return toString (). length ();
51
+ }
52
+
53
+ @ Override
54
+ public char charAt ( int index ) {
55
+ return toString (). charAt ( index ) ;
54
56
}
55
57
58
+ @ Override
59
+ public CharSequence subSequence (int start , int end ) {
60
+ return toString ().subSequence (start , end );
61
+ }
56
62
57
63
/**
58
64
* This will be called by the logging provider, potentially once
@@ -61,9 +67,202 @@ public LogMessage(Supplier<? extends CharSequence> supplier) {
61
67
@ Override
62
68
public String toString () {
63
69
if (this .result == null ) {
64
- this .result = this . supplier . get (). toString ();
70
+ this .result = buildString ();
65
71
}
66
72
return this .result ;
67
73
}
68
74
75
+ abstract String buildString ();
76
+
77
+
78
+ /**
79
+ * Build a lazy resolution message from the given supplier.
80
+ * @param supplier the supplier (typically bound to a Java 8 lambda expression)
81
+ * @see #toString()
82
+ */
83
+ public static LogMessage lazy (Supplier <? extends CharSequence > supplier ) {
84
+ return new LazyMessage (supplier );
85
+ }
86
+
87
+ /**
88
+ * Build a formatted message from the given format string and argument.
89
+ * @param format the format string (following {@link String#format} rules)
90
+ * @param arg1 the argument
91
+ * @see String#format(String, Object...)
92
+ */
93
+ public static LogMessage format (String format , Object arg1 ) {
94
+ return new FormatMessage1 (format , arg1 );
95
+ }
96
+
97
+ /**
98
+ * Build a formatted message from the given format string and arguments.
99
+ * @param format the format string (following {@link String#format} rules)
100
+ * @param arg1 the first argument
101
+ * @param arg2 the second argument
102
+ * @see String#format(String, Object...)
103
+ */
104
+ public static LogMessage format (String format , Object arg1 , Object arg2 ) {
105
+ return new FormatMessage2 (format , arg1 , arg2 );
106
+ }
107
+
108
+ /**
109
+ * Build a formatted message from the given format string and arguments.
110
+ * @param format the format string (following {@link String#format} rules)
111
+ * @param arg1 the first argument
112
+ * @param arg2 the second argument
113
+ * @param arg3 the third argument
114
+ * @see String#format(String, Object...)
115
+ */
116
+ public static LogMessage format (String format , Object arg1 , Object arg2 , Object arg3 ) {
117
+ return new FormatMessage3 (format , arg1 , arg2 , arg3 );
118
+ }
119
+
120
+ /**
121
+ * Build a formatted message from the given format string and arguments.
122
+ * @param format the format string (following {@link String#format} rules)
123
+ * @param arg1 the first argument
124
+ * @param arg2 the second argument
125
+ * @param arg3 the third argument
126
+ * @param arg4 the fourth argument
127
+ * @see String#format(String, Object...)
128
+ */
129
+ public static LogMessage format (String format , Object arg1 , Object arg2 , Object arg3 , Object arg4 ) {
130
+ return new FormatMessage4 (format , arg1 , arg2 , arg3 , arg4 );
131
+ }
132
+
133
+ /**
134
+ * Build a formatted message from the given format string and varargs.
135
+ * @param format the format string (following {@link String#format} rules)
136
+ * @param args the varargs array (costly, prefer individual arguments)
137
+ * @see String#format(String, Object...)
138
+ */
139
+ public static LogMessage format (String format , Object ... args ) {
140
+ return new FormatMessageX (format , args );
141
+ }
142
+
143
+
144
+ private static final class LazyMessage extends LogMessage {
145
+
146
+ private Supplier <? extends CharSequence > supplier ;
147
+
148
+ LazyMessage (Supplier <? extends CharSequence > supplier ) {
149
+ Assert .notNull (supplier , "Supplier must not be null" );
150
+ this .supplier = supplier ;
151
+ }
152
+
153
+ @ Override
154
+ String buildString () {
155
+ return this .supplier .get ().toString ();
156
+ }
157
+ }
158
+
159
+
160
+ private static abstract class FormatMessage extends LogMessage {
161
+
162
+ protected final String format ;
163
+
164
+ FormatMessage (String format ) {
165
+ Assert .notNull (format , "Format must not be null" );
166
+ this .format = format ;
167
+ }
168
+ }
169
+
170
+
171
+ private static final class FormatMessage1 extends FormatMessage {
172
+
173
+ private final Object arg1 ;
174
+
175
+ FormatMessage1 (String format , Object arg1 ) {
176
+ super (format );
177
+ this .arg1 = arg1 ;
178
+ }
179
+
180
+ @ Override
181
+ protected String buildString () {
182
+ return String .format (this .format , this .arg1 );
183
+ }
184
+ }
185
+
186
+
187
+ private static final class FormatMessage2 extends FormatMessage {
188
+
189
+ private final Object arg1 ;
190
+
191
+ private final Object arg2 ;
192
+
193
+ FormatMessage2 (String format , Object arg1 , Object arg2 ) {
194
+ super (format );
195
+ this .arg1 = arg1 ;
196
+ this .arg2 = arg2 ;
197
+ }
198
+
199
+ @ Override
200
+ String buildString () {
201
+ return String .format (this .format , this .arg1 , this .arg2 );
202
+ }
203
+ }
204
+
205
+
206
+ private static final class FormatMessage3 extends FormatMessage {
207
+
208
+ private final Object arg1 ;
209
+
210
+ private final Object arg2 ;
211
+
212
+ private final Object arg3 ;
213
+
214
+ FormatMessage3 (String format , Object arg1 , Object arg2 , Object arg3 ) {
215
+ super (format );
216
+ this .arg1 = arg1 ;
217
+ this .arg2 = arg2 ;
218
+ this .arg3 = arg3 ;
219
+ }
220
+
221
+ @ Override
222
+ String buildString () {
223
+ return String .format (this .format , this .arg1 , this .arg2 , this .arg3 );
224
+ }
225
+ }
226
+
227
+
228
+ private static final class FormatMessage4 extends FormatMessage {
229
+
230
+ private final Object arg1 ;
231
+
232
+ private final Object arg2 ;
233
+
234
+ private final Object arg3 ;
235
+
236
+ private final Object arg4 ;
237
+
238
+ FormatMessage4 (String format , Object arg1 , Object arg2 , Object arg3 , Object arg4 ) {
239
+ super (format );
240
+ this .arg1 = arg1 ;
241
+ this .arg2 = arg2 ;
242
+ this .arg3 = arg3 ;
243
+ this .arg4 = arg4 ;
244
+ }
245
+
246
+ @ Override
247
+ String buildString () {
248
+ return String .format (this .format , this .arg1 , this .arg2 , this .arg3 , this .arg4 );
249
+ }
250
+ }
251
+
252
+
253
+ private static final class FormatMessageX extends FormatMessage {
254
+
255
+ private final Object [] args ;
256
+
257
+ FormatMessageX (String format , Object ... args ) {
258
+ super (format );
259
+ this .args = args ;
260
+ }
261
+
262
+ @ Override
263
+ String buildString () {
264
+ return String .format (this .format , this .args );
265
+ }
266
+ }
267
+
69
268
}
0 commit comments