Skip to content

Commit d85727f

Browse files
makingClaude
and
Claude
authored
Implement singleton pattern for SimpleMessageFormatter (#450)
* Implement singleton pattern for SimpleMessageFormatter - Add getInstance() method to SimpleMessageFormatter - Deprecate public constructor for backward compatibility - Replace all direct instantiation with getInstance() calls - Update documentation with singleton usage information 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Apply spring-javaformat --------- Co-authored-by: Claude <[email protected]>
1 parent 36e8fe5 commit d85727f

9 files changed

+35
-10
lines changed

Diff for: src/main/java/am/ik/yavi/builder/ValidatorBuilder.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ public Validator<T> build() {
194194
return new Validator<>(messageKeySeparator,
195195
new PredicatesList<>(this.conflictStrategy, this.predicatesList).toList(), this.collectionValidators,
196196
this.conditionalValidators,
197-
this.messageFormatter == null ? new SimpleMessageFormatter() : this.messageFormatter, this.failFast);
197+
this.messageFormatter == null ? SimpleMessageFormatter.getInstance() : this.messageFormatter,
198+
this.failFast);
198199
}
199200

200201
/**

Diff for: src/main/java/am/ik/yavi/message/SimpleMessageFormatter.java

+24
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,32 @@
1818
import java.text.MessageFormat;
1919
import java.util.Locale;
2020

21+
/**
22+
* A simple implementation of {@link MessageFormatter} that formats messages using
23+
* {@link MessageFormat}. This class is a singleton to optimize resource usage.
24+
*/
2125
public class SimpleMessageFormatter implements MessageFormatter {
2226

27+
private static final SimpleMessageFormatter INSTANCE = new SimpleMessageFormatter();
28+
29+
/**
30+
* Returns the singleton instance of {@link SimpleMessageFormatter}.
31+
* @return the singleton instance
32+
*/
33+
public static SimpleMessageFormatter getInstance() {
34+
return INSTANCE;
35+
}
36+
37+
/**
38+
* Constructor.
39+
* @deprecated Use {@link #getInstance()} instead to get the singleton instance. This
40+
* constructor will be removed in a future version.
41+
*/
42+
@Deprecated
43+
public SimpleMessageFormatter() {
44+
// Public constructor kept for backward compatibility
45+
}
46+
2347
@Override
2448
public String format(String messageKey, String defaultMessageFormat, Object[] args, Locale locale) {
2549
return new MessageFormat(defaultMessageFormat, locale).format(args);

Diff for: src/test/java/am/ik/yavi/core/BiConsumerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class BiConsumerTest {
3333

3434
static final ErrorHandler<List<ConstraintViolation>> errorHandler = (errors, name, messageKey, args,
3535
defaultMessage) -> errors.add(new ConstraintViolation(name, messageKey, defaultMessage, args,
36-
new SimpleMessageFormatter(), Locale.ENGLISH));
36+
SimpleMessageFormatter.getInstance(), Locale.ENGLISH));
3737

3838
static final Validator<User> userValidator = ValidatorBuilder.of(User.class)
3939
.constraint(User::getName, "name", c -> c.notNull().greaterThanOrEqual(1).lessThanOrEqual(20))

Diff for: src/test/java/am/ik/yavi/core/BiValidatorTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class BiValidatorTest {
3333

3434
static final ErrorHandler<List<ConstraintViolation>> errorHandler = (errors, name, messageKey, args,
3535
defaultMessage) -> errors.add(new ConstraintViolation(name, messageKey, defaultMessage, args,
36-
new SimpleMessageFormatter(), Locale.ENGLISH));
36+
SimpleMessageFormatter.getInstance(), Locale.ENGLISH));
3737

3838
static final Validator<User> userValidator = ValidatorBuilder.of(User.class)
3939
.constraint(User::getName, "name", c -> c.notNull().greaterThanOrEqual(1).lessThanOrEqual(20))

Diff for: src/test/java/am/ik/yavi/core/ConstraintViolationsExceptionTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ConstraintViolationsExceptionTest {
2828
@Test
2929
void customMessage() {
3030
final ConstraintViolations violations = new ConstraintViolations();
31-
final SimpleMessageFormatter messageFormatter = new SimpleMessageFormatter();
31+
final SimpleMessageFormatter messageFormatter = SimpleMessageFormatter.getInstance();
3232
violations.add(new ConstraintViolation("name1", "key", "{0} is invalid.", new Object[] { "a" },
3333
messageFormatter, Locale.ENGLISH));
3434
final ConstraintViolationsException exception = new ConstraintViolationsException("error!", violations);
@@ -38,7 +38,7 @@ void customMessage() {
3838
@Test
3939
void defaultMessage() {
4040
final ConstraintViolations violations = new ConstraintViolations();
41-
final SimpleMessageFormatter messageFormatter = new SimpleMessageFormatter();
41+
final SimpleMessageFormatter messageFormatter = SimpleMessageFormatter.getInstance();
4242
violations.add(new ConstraintViolation("name1", "key", "{0} is invalid.", new Object[] { "a" },
4343
messageFormatter, Locale.ENGLISH));
4444
final ConstraintViolationsException exception = new ConstraintViolationsException(violations);

Diff for: src/test/java/am/ik/yavi/core/ConstraintViolationsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class ConstraintViolationsTest {
2727

2828
@Test
2929
public void apply() {
30-
SimpleMessageFormatter messageFormatter = new SimpleMessageFormatter();
30+
SimpleMessageFormatter messageFormatter = SimpleMessageFormatter.getInstance();
3131
ConstraintViolations violations = new ConstraintViolations();
3232
violations.add(new ConstraintViolation("foo0", "abc0", "hello0", new Object[] { 1 }, messageFormatter,
3333
Locale.getDefault()));

Diff for: src/test/java/am/ik/yavi/core/ValidatedTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void successWith() {
3838
void failureWith() {
3939
final Validated<Object> validated = Validated
4040
.failureWith(new ConstraintViolation("name", "notNull", "\"{0}\" must not be blank.",
41-
new Object[] { "name", "" }, new SimpleMessageFormatter(), Locale.ENGLISH));
41+
new Object[] { "name", "" }, SimpleMessageFormatter.getInstance(), Locale.ENGLISH));
4242
assertThat(validated.isValid()).isFalse();
4343
assertThat(validated.errors()).hasSize(1);
4444
assertThat(validated.errors().get(0).message()).isEqualTo("\"name\" must not be blank.");
@@ -48,7 +48,7 @@ void failureWith() {
4848
void testFailureWith() {
4949
final Validated<Object> validated = Validated.failureWith(
5050
Collections.singletonList(new ConstraintViolation("name", "notNull", "\"{0}\" must not be blank.",
51-
new Object[] { "name", "" }, new SimpleMessageFormatter(), Locale.ENGLISH)));
51+
new Object[] { "name", "" }, SimpleMessageFormatter.getInstance(), Locale.ENGLISH)));
5252
assertThat(validated.isValid()).isFalse();
5353
assertThat(validated.errors()).hasSize(1);
5454
assertThat(validated.errors().get(0).message()).isEqualTo("\"name\" must not be blank.");

Diff for: src/test/java/am/ik/yavi/factory/BiConsumerFactoryTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class BiConsumerFactoryTest {
3232

3333
private final ErrorHandler<List<ConstraintViolation>> errorHandler = (errors, name, messageKey, args,
3434
defaultMessage) -> errors.add(new ConstraintViolation(name, messageKey, defaultMessage, args,
35-
new SimpleMessageFormatter(), Locale.ENGLISH));
35+
SimpleMessageFormatter.getInstance(), Locale.ENGLISH));
3636

3737
private final BiConsumerFactory<List<ConstraintViolation>> validatorFactory = new BiConsumerFactory<>(
3838
this.errorHandler);

Diff for: src/test/java/am/ik/yavi/factory/BiValidatorFactoryTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class BiValidatorFactoryTest {
3232

3333
private final ErrorHandler<List<ConstraintViolation>> errorHandler = (errors, name, messageKey, args,
3434
defaultMessage) -> errors.add(new ConstraintViolation(name, messageKey, defaultMessage, args,
35-
new SimpleMessageFormatter(), Locale.ENGLISH));
35+
SimpleMessageFormatter.getInstance(), Locale.ENGLISH));
3636

3737
private final BiValidatorFactory<List<ConstraintViolation>> validatorFactory = new BiValidatorFactory<>(
3838
this.errorHandler);

0 commit comments

Comments
 (0)