@@ -107,8 +107,7 @@ public class UserManagedCacheBuilder<K, V, T extends UserManagedCache<K, V>> imp
107107 private final Class <K > keyType ;
108108 private final Class <V > valueType ;
109109 private String id ;
110- private final Set <Service > services = new HashSet <>();
111- private final Set <ServiceCreationConfiguration <?, ?>> serviceCreationConfigurations = new HashSet <>();
110+ private ServiceLocator .DependencySet dependencySet = ServiceLocator .dependencySet ().with (Store .Provider .class );
112111 private ExpiryPolicy <? super K , ? super V > expiry = ExpiryPolicy .NO_EXPIRY ;
113112 private ClassLoader classLoader = ClassLoading .getDefaultClassLoader ();
114113 private EvictionAdvisor <? super K , ? super V > evictionAdvisor ;
@@ -143,8 +142,7 @@ private UserManagedCacheBuilder(UserManagedCacheBuilder<K, V, T> toCopy) {
143142 this .keyType = toCopy .keyType ;
144143 this .valueType = toCopy .valueType ;
145144 this .id = toCopy .id ;
146- this .services .addAll (toCopy .services );
147- this .serviceCreationConfigurations .addAll (toCopy .serviceCreationConfigurations );
145+ this .dependencySet = toCopy .dependencySet ;
148146 this .expiry = toCopy .expiry ;
149147 this .classLoader = toCopy .classLoader ;
150148 this .evictionAdvisor = toCopy .evictionAdvisor ;
@@ -167,19 +165,15 @@ private UserManagedCacheBuilder(UserManagedCacheBuilder<K, V, T> toCopy) {
167165 }
168166
169167 @ SuppressWarnings ("try" )
170- T build ( ServiceLocator . DependencySet serviceLocatorBuilder ) throws IllegalStateException {
168+ T internalBuild ( ) throws IllegalStateException {
171169
172170 String alias = id == null ? "UserManaged - " + instanceId .getAndIncrement () : id ;
173171 try (EhcachePrefixLoggerFactory .Context ignored = EhcachePrefixLoggerFactory .withContext ("cache-alias" , alias )){
174172 validateListenerConfig ();
175173
176174 ServiceLocator serviceLocator ;
177175 try {
178- for (ServiceCreationConfiguration <?, ?> serviceCreationConfig : serviceCreationConfigurations ) {
179- serviceLocatorBuilder = serviceLocatorBuilder .with (serviceCreationConfig );
180- }
181- serviceLocatorBuilder = serviceLocatorBuilder .with (Store .Provider .class );
182- serviceLocator = serviceLocatorBuilder .build ();
176+ serviceLocator = dependencySet .build ();
183177 serviceLocator .startAllServices ();
184178 } catch (Exception e ) {
185179 throw new IllegalStateException ("UserManagedCacheBuilder failed to build." , e );
@@ -354,13 +348,7 @@ private void validateListenerConfig() {
354348
355349 private void registerListeners (Cache <K , V > cache , ServiceProvider <Service > serviceProvider , List <LifeCycled > lifeCycledList ) {
356350 if (!eventListenerConfigurations .isEmpty ()) {
357- final CacheEventListenerProvider listenerProvider ;
358- CacheEventListenerProvider provider ;
359- if ((provider = serviceProvider .getService (CacheEventListenerProvider .class )) != null ) {
360- listenerProvider = provider ;
361- } else {
362- listenerProvider = new DefaultCacheEventListenerProvider ();
363- }
351+ final CacheEventListenerProvider listenerProvider = serviceProvider .getService (CacheEventListenerProvider .class );
364352 for (CacheEventListenerConfiguration <?> config : eventListenerConfigurations ) {
365353 final CacheEventListener <K , V > listener = listenerProvider .createEventListener (id , config );
366354 if (listener != null ) {
@@ -396,7 +384,7 @@ T cast(UserManagedCache<K, V> cache) {
396384 * @throws IllegalStateException if the user managed cache cannot be built
397385 */
398386 public final T build (final boolean init ) throws IllegalStateException {
399- final T build = build ( dependencySet (). withoutMandatoryServices (). with ( services ) );
387+ final T build = internalBuild ( );
400388 if (init ) {
401389 build .init ();
402390 }
@@ -557,6 +545,7 @@ public final UserManagedCacheBuilder<K, V, T> withEventListeners(CacheEventListe
557545 */
558546 public final UserManagedCacheBuilder <K , V , T > withEventListeners (CacheEventListenerConfiguration <?> ... cacheEventListenerConfigurations ) {
559547 UserManagedCacheBuilder <K , V , T > otherBuilder = new UserManagedCacheBuilder <>(this );
548+ otherBuilder .dependencySet .with (CacheEventListenerProvider .class );
560549 otherBuilder .eventListenerConfigurations .addAll (Arrays .asList (cacheEventListenerConfigurations ));
561550 return otherBuilder ;
562551 }
@@ -737,9 +726,8 @@ public UserManagedCacheBuilder<K, V, T> withValueSerializer(Serializer<V> valueS
737726 @ Deprecated
738727 public UserManagedCacheBuilder <K , V , T > withSizeOfMaxObjectGraph (long size ) {
739728 UserManagedCacheBuilder <K , V , T > otherBuilder = new UserManagedCacheBuilder <>(this );
740- removeAnySizeOfEngine (otherBuilder );
741729 otherBuilder .objectGraphSize = size ;
742- otherBuilder .serviceCreationConfigurations . add (new org .ehcache .impl .config .store .heap .DefaultSizeOfEngineProviderConfiguration (otherBuilder .maxObjectSize , otherBuilder .sizeOfUnit , otherBuilder .objectGraphSize ));
730+ otherBuilder .dependencySet . with (new org .ehcache .impl .config .store .heap .DefaultSizeOfEngineProviderConfiguration (otherBuilder .maxObjectSize , otherBuilder .sizeOfUnit , otherBuilder .objectGraphSize ));
743731 return otherBuilder ;
744732 }
745733
@@ -756,10 +744,9 @@ public UserManagedCacheBuilder<K, V, T> withSizeOfMaxObjectGraph(long size) {
756744 @ Deprecated
757745 public UserManagedCacheBuilder <K , V , T > withSizeOfMaxObjectSize (long size , MemoryUnit unit ) {
758746 UserManagedCacheBuilder <K , V , T > otherBuilder = new UserManagedCacheBuilder <>(this );
759- removeAnySizeOfEngine (otherBuilder );
760747 otherBuilder .maxObjectSize = size ;
761748 otherBuilder .sizeOfUnit = unit ;
762- otherBuilder .serviceCreationConfigurations . add (new org .ehcache .impl .config .store .heap .DefaultSizeOfEngineProviderConfiguration (otherBuilder .maxObjectSize , otherBuilder .sizeOfUnit , otherBuilder .objectGraphSize ));
749+ otherBuilder .dependencySet . with (new org .ehcache .impl .config .store .heap .DefaultSizeOfEngineProviderConfiguration (otherBuilder .maxObjectSize , otherBuilder .sizeOfUnit , otherBuilder .objectGraphSize ));
763750 return otherBuilder ;
764751 }
765752
@@ -791,10 +778,7 @@ public static <K, V> UserManagedCacheBuilder<K, V, UserManagedCache<K, V>> newUs
791778 @ SuppressWarnings ("deprecation" )
792779 public UserManagedCacheBuilder <K , V , T > using (Service service ) {
793780 UserManagedCacheBuilder <K , V , T > otherBuilder = new UserManagedCacheBuilder <>(this );
794- if (service instanceof org .ehcache .core .spi .store .heap .SizeOfEngineProvider ) {
795- removeAnySizeOfEngine (otherBuilder );
796- }
797- otherBuilder .services .add (service );
781+ otherBuilder .dependencySet .with (service );
798782 return otherBuilder ;
799783 }
800784
@@ -815,17 +799,7 @@ public UserManagedCacheBuilder<K, V, T> using(Service service) {
815799 @ SuppressWarnings ("deprecation" )
816800 public UserManagedCacheBuilder <K , V , T > using (ServiceCreationConfiguration <?, ?> serviceConfiguration ) {
817801 UserManagedCacheBuilder <K , V , T > otherBuilder = new UserManagedCacheBuilder <>(this );
818- if (serviceConfiguration instanceof org .ehcache .impl .config .store .heap .DefaultSizeOfEngineProviderConfiguration ) {
819- removeAnySizeOfEngine (otherBuilder );
820- }
821- otherBuilder .serviceCreationConfigurations .add (serviceConfiguration );
802+ otherBuilder .dependencySet .with (serviceConfiguration );
822803 return otherBuilder ;
823804 }
824-
825- @ Deprecated
826- private static void removeAnySizeOfEngine (UserManagedCacheBuilder <?, ?, ?> builder ) {
827- builder .services .remove (findSingletonAmongst (org .ehcache .core .spi .store .heap .SizeOfEngineProvider .class , builder .services ));
828- builder .serviceCreationConfigurations .remove (findSingletonAmongst (org .ehcache .impl .config .store .heap .DefaultSizeOfEngineProviderConfiguration .class , builder .serviceCreationConfigurations ));
829- }
830-
831805}
0 commit comments