This is an automated email from the ASF dual-hosted git repository. tv pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
The following commit(s) were added to refs/heads/master by this push: new e95cb2b Handle PMD and Spotbugs findings e95cb2b is described below commit e95cb2baafdde62c5eca6d407d877a247de6ff19 Author: Thomas Vandahl <t...@apache.org> AuthorDate: Thu Dec 23 15:09:58 2021 +0100 Handle PMD and Spotbugs findings --- .../jcs3/auxiliary/disk/PurgatoryElement.java | 7 +- .../auxiliary/disk/block/BlockDiskKeyStore.java | 4 +- .../auxiliary/disk/jdbc/mysql/MySQLDiskCache.java | 12 ++- .../disk/jdbc/mysql/util/ScheduleParser.java | 11 +- .../auxiliary/lateral/LateralCacheMonitor.java | 5 +- .../lateral/socket/tcp/LateralTCPListener.java | 6 +- .../remote/http/server/RemoteHttpCacheServlet.java | 8 +- .../auxiliary/remote/server/RemoteCacheServer.java | 2 - .../apache/commons/jcs3/engine/CacheElement.java | 4 +- .../jcs3/engine/control/CompositeCache.java | 3 +- .../engine/control/CompositeCacheConfigurator.java | 20 +--- .../jcs3/engine/control/CompositeCacheManager.java | 119 ++++++++++----------- .../util/SoftReferenceElementDescriptor.java | 2 +- .../block/BlockDiskCacheConcurrentUnitTest.java | 24 ++--- .../commons/jcs3/jcache/JCSCachingManager.java | 15 +-- .../commons/jcs3/jcache/cdi/CDIJCacheHelper.java | 4 +- 16 files changed, 118 insertions(+), 128 deletions(-) diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/PurgatoryElement.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/PurgatoryElement.java index 3f14c13..593356e 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/PurgatoryElement.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/PurgatoryElement.java @@ -30,7 +30,8 @@ import org.apache.commons.jcs3.engine.behavior.IElementAttributes; * been written to disk. */ public class PurgatoryElement<K, V> - extends CacheElement<K, V> + extends CacheElement<K, V> // Remove this superclass in next major release +// implements ICacheElement<K, V> { /** Don't change */ private static final long serialVersionUID = -8152034342684135628L; @@ -48,9 +49,7 @@ public class PurgatoryElement<K, V> */ public PurgatoryElement( final ICacheElement<K, V> cacheElement ) { - super(cacheElement.getCacheName(), - cacheElement.getKey(), cacheElement.getVal(), - cacheElement.getElementAttributes()); + super(cacheElement.getCacheName(), cacheElement.getKey(), cacheElement.getVal()); this.cacheElement = cacheElement; } diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskKeyStore.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskKeyStore.java index 5379835..75f33ac 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskKeyStore.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskKeyStore.java @@ -436,8 +436,8 @@ public class BlockDiskKeyStore<K> if (fileSignature != KEY_FILE_SIGNATURE) { - try (final InputStream fis = Files.newInputStream(keyFile.toPath()); - final ObjectInputStream ois = new ObjectInputStreamClassLoaderAware(fis, null)) + try (InputStream fis = Files.newInputStream(keyFile.toPath()); + ObjectInputStream ois = new ObjectInputStreamClassLoaderAware(fis, null)) { while (true) { diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/MySQLDiskCache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/MySQLDiskCache.java index 8934f05..d1ad058 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/MySQLDiskCache.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/MySQLDiskCache.java @@ -75,7 +75,8 @@ public class MySQLDiskCache<K, V> @Override protected ICacheElement<K, V> processGet( final K key ) { - if ( (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING) && this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) + if (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING && + this.mySQLDiskCacheAttributes.isBalkDuringOptimization()) { return null; } @@ -92,7 +93,8 @@ public class MySQLDiskCache<K, V> @Override protected Map<K, ICacheElement<K, V>> processGetMatching( final String pattern ) { - if ( (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING) && this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) + if (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING && + this.mySQLDiskCacheAttributes.isBalkDuringOptimization()) { return null; } @@ -123,7 +125,8 @@ public class MySQLDiskCache<K, V> @Override protected void processUpdate( final ICacheElement<K, V> element ) { - if ( (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING) && this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) + if (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING && + this.mySQLDiskCacheAttributes.isBalkDuringOptimization()) { return; } @@ -142,7 +145,8 @@ public class MySQLDiskCache<K, V> @Override protected int deleteExpired() { - if ( (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING) && this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) + if (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING && + this.mySQLDiskCacheAttributes.isBalkDuringOptimization()) { return -1; } diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/util/ScheduleParser.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/util/ScheduleParser.java index e2a489e..2819cf1 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/util/ScheduleParser.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/util/ScheduleParser.java @@ -23,7 +23,6 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; -import java.util.StringTokenizer; /** * Parses the very simple schedule format. @@ -48,14 +47,12 @@ public class ScheduleParser throw new ParseException( "Cannot create schedules for a null String.", 0 ); } - final StringTokenizer toker = new StringTokenizer( schedule, "," ); - final Date[] dates = new Date[toker.countTokens()]; + final String timeStrings[] = schedule.split("\\s*,\\s*"); + final Date[] dates = new Date[timeStrings.length]; int cnt = 0; - while ( toker.hasMoreTokens() ) + for (String time : timeStrings) { - final String time = toker.nextToken(); - dates[cnt] = getDateForSchedule( time ); - cnt++; + dates[cnt++] = getDateForSchedule(time); } return dates; } diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCacheMonitor.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCacheMonitor.java index a0f9c7c..6f7274a 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCacheMonitor.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/LateralCacheMonitor.java @@ -51,10 +51,13 @@ public class LateralCacheMonitor extends AbstractAuxiliaryCacheMonitor * Allows close classes, ie testers to set the idle period to something testable. * <p> * @param idlePeriod + * + * @deprecated Use setIdlePeriod() */ + @Deprecated protected static void forceShortIdlePeriod( final long idlePeriod ) { - LateralCacheMonitor.idlePeriod = idlePeriod; + LateralCacheMonitor.setIdlePeriod(idlePeriod); } /** diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPListener.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPListener.java index fc92109..daefb18 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPListener.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPListener.java @@ -475,7 +475,7 @@ public class LateralTCPListener<K, V> */ private void runListener(final ServerSocketChannel serverSocket) { - try (final Selector selector = Selector.open()) + try (Selector selector = Selector.open()) { serverSocket.register(selector, SelectionKey.OP_ACCEPT); log.debug("Waiting for clients to connect"); @@ -703,8 +703,8 @@ public class LateralTCPListener<K, V> // if a hashcode was given and filtering is on // check to see if they are the same // if so, then don't remove, otherwise issue a remove - if ( (led.getValHashCode() != -1) && - getTcpLateralCacheAttributes().isFilterRemoveByHashCode() ) + if (led.getValHashCode() != -1 && + getTcpLateralCacheAttributes().isFilterRemoveByHashCode()) { final ICacheElement<K, V> test = getCache( cacheName ).localGet( key ); if ( test != null ) diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/http/server/RemoteHttpCacheServlet.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/http/server/RemoteHttpCacheServlet.java index 0125cf1..f80083e 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/http/server/RemoteHttpCacheServlet.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/http/server/RemoteHttpCacheServlet.java @@ -69,13 +69,13 @@ public class RemoteHttpCacheServlet private static ICacheServiceNonLocal<Serializable, Serializable> remoteCacheService; /** This needs to be standard, since the other side is standard */ - private final StandardSerializer serializer = new StandardSerializer(); + private static final StandardSerializer serializer = new StandardSerializer(); /** Number of service calls. */ private int serviceCalls; /** The interval at which we will log the count. */ - private final int logInterval = 100; + private static final int logInterval = 100; /** * Initializes the cache. @@ -134,7 +134,7 @@ public class RemoteHttpCacheServlet { RemoteCacheRequest<Serializable, Serializable> remoteRequest = null; - try (final InputStream inputStream = request.getInputStream()) + try (InputStream inputStream = request.getInputStream()) { log.debug( "After getting input stream and before reading it" ); remoteRequest = readRequestFromStream( inputStream ); @@ -169,7 +169,7 @@ public class RemoteHttpCacheServlet */ protected void writeResponse( final HttpServletResponse response, final RemoteCacheResponse<Object> cacheResponse ) { - try (final OutputStream outputStream = response.getOutputStream()) + try (OutputStream outputStream = response.getOutputStream()) { response.setContentType( "application/octet-stream" ); serializer.serializeTo(cacheResponse, outputStream); diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheServer.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheServer.java index 4081a8a..c5caa67 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheServer.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheServer.java @@ -265,8 +265,6 @@ public class RemoteCacheServer<K, V> try { final CacheListeners<K, V> cacheDesc = getCacheListeners( item.getCacheName() ); - /* Object val = */item.getVal(); - final boolean fromCluster = isRequestFromCluster( requesterId ); log.debug( "In update, requesterId = [{0}] fromCluster = {1}", requesterId, fromCluster ); diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/CacheElement.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/CacheElement.java index 519026c..ea2399e 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/CacheElement.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/CacheElement.java @@ -1,7 +1,5 @@ package org.apache.commons.jcs3.engine; -import java.util.Objects; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -21,6 +19,8 @@ import java.util.Objects; * under the License. */ +import java.util.Objects; + import org.apache.commons.jcs3.engine.behavior.ICacheElement; import org.apache.commons.jcs3.engine.behavior.IElementAttributes; diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCache.java index 7696323..f6ae9cd 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCache.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCache.java @@ -1231,7 +1231,8 @@ public class CompositeCache<K, V> || fromRemote && aux.getCacheType() == CacheType.REMOTE_CACHE) { log.info("In DISPOSE, [{0}] SKIPPING auxiliary [{1}] fromRemote [{2}]", - this.cacheAttr::getCacheName, aux::getCacheName, + this.cacheAttr::getCacheName, + () -> aux == null ? "null" : aux.getCacheName(), () -> fromRemote); continue; } diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheConfigurator.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheConfigurator.java index 99b6d8e..def0f1c 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheConfigurator.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheConfigurator.java @@ -22,7 +22,6 @@ package org.apache.commons.jcs3.engine.control; import java.util.ArrayList; import java.util.List; import java.util.Properties; -import java.util.StringTokenizer; import org.apache.commons.jcs3.auxiliary.AuxiliaryCache; import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes; @@ -220,30 +219,19 @@ public class CompositeCacheConfigurator log.debug( "Parsing region name \"{0}\", value \"{1}\"", regName, auxiliaries ); - // We must skip over ',' but not white space - final StringTokenizer st = new StringTokenizer( auxiliaries, "," ); - - // If value is not in the form ", appender.." or "", then we should set - // the priority of the category. + String auxNames[] = auxiliaries.split("\\s*,\\s*"); // just to be on the safe side... - if ( !( auxiliaries.startsWith( "," ) || auxiliaries.equals( "" ) ) && !st.hasMoreTokens() ) + if (auxNames.length == 0) { return null; } - AuxiliaryCache<K, V> auxCache; - String auxName; - while ( st.hasMoreTokens() ) + for (String auxName : auxNames) { - auxName = st.nextToken().trim(); - if (auxName.equals( "," )) - { - continue; - } log.debug( "Parsing auxiliary named \"{0}\".", auxName ); - auxCache = parseAuxiliary( props, ccm, auxName, regName ); + AuxiliaryCache<K, V> auxCache = parseAuxiliary( props, ccm, auxName, regName ); if ( auxCache != null ) { diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheManager.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheManager.java index b491404..63d1177 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheManager.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheManager.java @@ -237,7 +237,7 @@ public class CompositeCacheManager } /** Creates a shutdown hook and starts the scheduler service */ - protected void initialize() + protected synchronized void initialize() { if (!isInitialized) { @@ -411,7 +411,7 @@ public class CompositeCacheManager * <p> * @param properties assumed not null */ - private void doConfigure( final Properties properties ) + private synchronized void doConfigure( final Properties properties ) { // We will expose this for managers that need raw properties. this.configurationProperties = properties; @@ -584,85 +584,82 @@ public class CompositeCacheManager /** * Calls freeCache on all regions */ - public void shutDown() + public synchronized void shutDown() { - synchronized (CompositeCacheManager.class) + // shutdown element event queue + if (this.elementEventQueue != null) { - // shutdown element event queue - if (this.elementEventQueue != null) - { - this.elementEventQueue.dispose(); - } + this.elementEventQueue.dispose(); + } + + // notify any observers + IShutdownObserver observer = null; + while ((observer = shutdownObservers.poll()) != null) + { + observer.shutdown(); + } - // notify any observers - IShutdownObserver observer = null; - while ((observer = shutdownObservers.poll()) != null) + // Unregister JMX bean + if (isJMXRegistered) + { + final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + try { - observer.shutdown(); + final ObjectName jmxObjectName = new ObjectName(jmxName); + mbs.unregisterMBean(jmxObjectName); } - - // Unregister JMX bean - if (isJMXRegistered) + catch (final Exception e) { - final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - try - { - final ObjectName jmxObjectName = new ObjectName(jmxName); - mbs.unregisterMBean(jmxObjectName); - } - catch (final Exception e) - { - log.warn( "Could not unregister JMX bean.", e ); - } - - isJMXRegistered = false; + log.warn( "Could not unregister JMX bean.", e ); } - // do the traditional shutdown of the regions. - getCacheNames().forEach(this::freeCache); + isJMXRegistered = false; + } + + // do the traditional shutdown of the regions. + getCacheNames().forEach(this::freeCache); - // shut down auxiliaries - for (final String key : auxiliaryCaches.keySet()) + // shut down auxiliaries + for (final String key : auxiliaryCaches.keySet()) + { + try { - try - { - freeAuxiliaryCache(key); - } - catch (final IOException e) - { - log.warn("Auxiliary cache {0} failed to shut down", key, e); - } + freeAuxiliaryCache(key); + } + catch (final IOException e) + { + log.warn("Auxiliary cache {0} failed to shut down", key, e); } + } - // shut down factories - auxiliaryFactoryRegistry.values().forEach(AuxiliaryCacheFactory::dispose); + // shut down factories + auxiliaryFactoryRegistry.values().forEach(AuxiliaryCacheFactory::dispose); - auxiliaryAttributeRegistry.clear(); - auxiliaryFactoryRegistry.clear(); + auxiliaryAttributeRegistry.clear(); + auxiliaryFactoryRegistry.clear(); - // shutdown all scheduled jobs - this.scheduledExecutor.shutdownNow(); + // shutdown all scheduled jobs + this.scheduledExecutor.shutdownNow(); - // shutdown all thread pools - ThreadPoolManager.dispose(); + // shutdown all thread pools + ThreadPoolManager.dispose(); - if (shutdownHook != null) + if (shutdownHook != null) + { + try { - try - { - Runtime.getRuntime().removeShutdownHook(shutdownHook); - } - catch (final IllegalStateException e) - { - // May fail if the JVM is already shutting down - } - - this.shutdownHook = null; + Runtime.getRuntime().removeShutdownHook(shutdownHook); + } + catch (final IllegalStateException e) + { + // May fail if the JVM is already shutting down } - isConfigured = false; - isInitialized = false; + this.shutdownHook = null; } + + isConfigured = false; + isInitialized = false; } /** */ diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/memory/util/SoftReferenceElementDescriptor.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/memory/util/SoftReferenceElementDescriptor.java index 7a4ca6e..5e797a5 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/memory/util/SoftReferenceElementDescriptor.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/memory/util/SoftReferenceElementDescriptor.java @@ -33,7 +33,7 @@ public class SoftReferenceElementDescriptor<K, V> private static final long serialVersionUID = -1905161209035522460L; /** The CacheElement wrapped by this descriptor */ - private final SoftReference<ICacheElement<K, V>> srce; + private transient final SoftReference<ICacheElement<K, V>> srce; /** * Constructs a usable MemoryElementDescriptor. diff --git a/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java b/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java index 9acfd4b..cc8116a 100644 --- a/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java +++ b/commons-jcs-core/src/test/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java @@ -140,13 +140,13 @@ public class BlockDiskCacheConcurrentUnitTest final CacheAccess<String, String> jcs = JCS.getInstance( region ); // Add items to cache - for ( int i = 0; i <= items; i++ ) + for ( int i = 0; i < items; i++ ) { jcs.put( i + ":key", region + " data " + i ); } // Test that all items are in cache - for ( int i = 0; i <= items; i++ ) + for ( int i = 0; i < items; i++ ) { final String value = jcs.get( i + ":key" ); @@ -155,13 +155,13 @@ public class BlockDiskCacheConcurrentUnitTest // Test that getElements returns all the expected values final Set<String> keys = new HashSet<>(); - for ( int i = 0; i <= items; i++ ) + for ( int i = 0; i < items; i++ ) { keys.add( i + ":key" ); } final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys ); - for ( int i = 0; i <= items; i++ ) + for ( int i = 0; i < items; i++ ) { final ICacheElement<String, String> element = elements.get( i + ":key" ); assertNotNull( "element " + i + ":key is missing", element ); @@ -169,14 +169,14 @@ public class BlockDiskCacheConcurrentUnitTest } // Remove all the items - for ( int i = 0; i <= items; i++ ) + for ( int i = 0; i < items; i++ ) { jcs.remove( i + ":key" ); } // Verify removal // another thread may have inserted since - for ( int i = 0; i <= items; i++ ) + for ( int i = 0; i < items; i++ ) { assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs .get( i + ":key" ) ); @@ -201,13 +201,13 @@ public class BlockDiskCacheConcurrentUnitTest final CacheAccess<String, String> jcs = JCS.getInstance( region ); // Add items to cache - for ( int i = start; i <= end; i++ ) + for ( int i = start; i < end; i++ ) { jcs.put( i + ":key", region + " data " + i ); } // Test that all items are in cache - for ( int i = start; i <= end; i++ ) + for ( int i = start; i < end; i++ ) { final String value = jcs.get( i + ":key" ); @@ -216,13 +216,13 @@ public class BlockDiskCacheConcurrentUnitTest // Test that getElements returns all the expected values final Set<String> keys = new HashSet<>(); - for ( int i = start; i <= end; i++ ) + for ( int i = start; i < end; i++ ) { keys.add( i + ":key" ); } final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys ); - for ( int i = start; i <= end; i++ ) + for ( int i = start; i < end; i++ ) { final ICacheElement<String, String> element = elements.get( i + ":key" ); assertNotNull( "element " + i + ":key is missing", element ); @@ -230,7 +230,7 @@ public class BlockDiskCacheConcurrentUnitTest } // Remove all the items - for ( int i = start; i <= end; i++ ) + for ( int i = start; i < end; i++ ) { jcs.remove( i + ":key" ); } @@ -239,7 +239,7 @@ public class BlockDiskCacheConcurrentUnitTest // Verify removal // another thread may have inserted since - for ( int i = start; i <= end; i++ ) + for ( int i = start; i < end; i++ ) { assertNull( "Removed key should be null: " + i + ":key " + "\n stats " + jcs.getStats(), jcs.get( i + ":key" ) ); diff --git a/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/JCSCachingManager.java b/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/JCSCachingManager.java index 886d3c0..629276f 100644 --- a/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/JCSCachingManager.java +++ b/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/JCSCachingManager.java @@ -189,19 +189,20 @@ public class JCSCachingManager implements CacheManager assertNotClosed(); assertNotNull(cacheName, "cacheName"); assertNotNull(configuration, "configuration"); - final Class<?> keyType = configuration.getKeyType(); - final Class<?> valueType = configuration.getValueType(); + final Class<K> keyType = configuration.getKeyType(); + final Class<V> valueType = configuration.getValueType(); if (caches.containsKey(cacheName)) { throw new javax.cache.CacheException("cache " + cacheName + " already exists"); } + @SuppressWarnings("unchecked") final Cache<K, V> cache = ClassLoaderAwareCache.wrap(loader, - new JCSCache/*<K, V>*/( + new JCSCache<>( loader, this, cacheName, - new JCSConfiguration/*<K, V>*/(configuration, keyType, valueType), + new JCSConfiguration<K, V>(configuration, keyType, valueType), properties, ExpiryAwareCache.class.cast(delegate.getCache(cacheName)))); caches.putIfAbsent(cacheName, cache); - return (Cache<K, V>) getCache(cacheName, keyType, valueType); + return getCache(cacheName, keyType, valueType); } @Override @@ -341,8 +342,8 @@ public class JCSCachingManager implements CacheManager @SuppressWarnings("unchecked") // don't know how to solve this final Configuration<K, V> config = cache.getConfiguration(Configuration.class); - if ((keyType != null && !config.getKeyType().isAssignableFrom(keyType)) - || (valueType != null && !config.getValueType().isAssignableFrom(valueType))) + if (keyType != null && !config.getKeyType().isAssignableFrom(keyType) || + valueType != null && !config.getValueType().isAssignableFrom(valueType)) { throw new IllegalArgumentException("this cache is <" + config.getKeyType().getName() + ", " + config.getValueType().getName() + "> " + " and not <" + keyType.getName() + ", " + valueType.getName() + ">"); diff --git a/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/cdi/CDIJCacheHelper.java b/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/cdi/CDIJCacheHelper.java index d01d0d5..d7b1e81 100644 --- a/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/cdi/CDIJCacheHelper.java +++ b/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/cdi/CDIJCacheHelper.java @@ -347,6 +347,7 @@ public class CDIJCacheHelper return defaultCacheResolverFactory(); } + @SuppressWarnings("unchecked") private <T> T instance(final Class<T> type) { final Set<Bean<?>> beans = beanManager.getBeans(type); @@ -463,7 +464,8 @@ public class CDIJCacheHelper return false; } final MethodKey classKey = MethodKey.class.cast(o); - return delegate.equals(classKey.delegate) && ((base == null && classKey.base == null) || (base != null && base.equals(classKey.base))); + return delegate.equals(classKey.delegate) && + (base == null && classKey.base == null || base != null && base.equals(classKey.base)); } @Override