Author: epbernard
Date: 2009-08-25 11:05:06 -0400 (Tue, 25 Aug 2009)
New Revision: 17414
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/LazyValidatorFactory.java
Log:
Implement a better double-check null.
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/LazyValidatorFactory.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/LazyValidatorFactory.java 2009-08-25
14:39:26 UTC (rev 17413)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/LazyValidatorFactory.java 2009-08-25
15:05:06 UTC (rev 17414)
@@ -50,41 +50,42 @@
this.configuration = configuration;
}
- public Validator getValidator() {
- if ( delegate == null ) {
- initFactory();
+ private ValidatorFactory getDelegate() {
+ ValidatorFactory result = delegate;
+ if (result == null) {
+ synchronized ( this ) {
+ result = delegate;
+ if (result == null) {
+ delegate = result = initFactory();
+ }
+ }
}
- return delegate.getValidator();
+ return result;
}
+ public Validator getValidator() {
+ return getDelegate().getValidator();
+ }
+
//we can initialize several times that's ok
- private void initFactory() {
+ private ValidatorFactory initFactory() {
if ( configuration == null ) {
- delegate = Validation.buildDefaultValidatorFactory();
+ return Validation.buildDefaultValidatorFactory();
}
else {
- delegate = configuration.buildValidatorFactory();
+ return configuration.buildValidatorFactory();
}
}
public ValidatorContext usingContext() {
- if ( delegate == null ) {
- initFactory();
- }
- return delegate.usingContext();
+ return getDelegate().usingContext();
}
public MessageInterpolator getMessageInterpolator() {
- if ( delegate == null ) {
- initFactory();
- }
- return delegate.getMessageInterpolator();
+ return getDelegate().getMessageInterpolator();
}
public <T> T unwrap(Class<T> clazz) {
- if ( delegate == null ) {
- initFactory();
- }
- return delegate.unwrap( clazz );
+ return getDelegate().unwrap( clazz );
}
}
Show replies by date