mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#365 cache implementation
This commit is contained in:
parent
59c791034c
commit
d0c685f6b8
@ -18,7 +18,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.services;
|
package org.mapstruct.ap.services;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
import org.mapstruct.spi.AccessorNamingStrategy;
|
import org.mapstruct.spi.AccessorNamingStrategy;
|
||||||
|
|
||||||
@ -29,22 +31,29 @@ import org.mapstruct.spi.AccessorNamingStrategy;
|
|||||||
*/
|
*/
|
||||||
public class Services {
|
public class Services {
|
||||||
|
|
||||||
|
private static final Map<Class<?>, Object> CACHE = new WeakHashMap<Class<?>, Object>();
|
||||||
|
|
||||||
private Services() {
|
private Services() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtain an implementation of {@link AccessorNamingStrategy}. If no specialized implementation is found using
|
* Obtain an implementation of {@link AccessorNamingStrategy}. If no specialized implementation is found using
|
||||||
* {@link ServiceLoader}, a JavaBeans-compliant default implementation is returned.
|
* {@link ServiceLoader}, a JavaBeans-compliant default implementation is returned. The result is cached across
|
||||||
|
* invocations.
|
||||||
*
|
*
|
||||||
* @return The implementation of {@link AccessorNamingStrategy}.
|
* @return The implementation of {@link AccessorNamingStrategy}.
|
||||||
* @throws IllegalStateException If more than one implementation is found by
|
* @throws IllegalStateException If more than one implementation is found by
|
||||||
* {@link ServiceLoader#load(Class, ClassLoader)}.
|
* {@link ServiceLoader#load(Class, ClassLoader)}.
|
||||||
*/
|
*/
|
||||||
public static AccessorNamingStrategy getAccessorNamingStrategy() {
|
public static AccessorNamingStrategy getAccessorNamingStrategy() {
|
||||||
AccessorNamingStrategy impl = get( AccessorNamingStrategy.class );
|
AccessorNamingStrategy impl = (AccessorNamingStrategy) CACHE.get( AccessorNamingStrategy.class );
|
||||||
|
if ( impl == null ) {
|
||||||
|
impl = get( AccessorNamingStrategy.class );
|
||||||
if ( impl == null ) {
|
if ( impl == null ) {
|
||||||
impl = new DefaultAccessorNamingStrategy();
|
impl = new DefaultAccessorNamingStrategy();
|
||||||
}
|
}
|
||||||
|
CACHE.put( AccessorNamingStrategy.class, impl );
|
||||||
|
}
|
||||||
return impl;
|
return impl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user