This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit 14ad67d6519ceb681e05b7d59af4bcbea62a3329 Author: Thomas Vandahl <[email protected]> AuthorDate: Tue Oct 6 22:05:20 2026 +0200 Adapt jcs-yaicache to jcs4 --- .../jcs4/utils/zip/CompressionUtilUnitTest.java | 22 +-- .../commons-jcs4-yajcache/README.txt | 4 +- .../jcs/yajcache/beans/CacheChangeEvent.java | 12 +- .../jcs/yajcache/beans/CacheChangeSupport.java | 8 +- .../jcs/yajcache/beans/CacheClearEvent.java | 5 +- .../jcs/yajcache/beans/CachePutBeanCloneEvent.java | 5 +- .../jcs/yajcache/beans/CachePutBeanCopyEvent.java | 5 +- .../jcs/yajcache/beans/CachePutCopyEvent.java | 5 +- .../commons/jcs/yajcache/beans/CachePutEvent.java | 5 +- .../jcs/yajcache/beans/CacheRemoveEvent.java | 5 +- .../jcs/yajcache/beans/ICacheChangeHandler.java | 1 - .../jcs/yajcache/beans/ICacheChangeListener.java | 4 +- .../jcs/yajcache/config/PerCacheConfig.java | 1 - .../jcs/yajcache/config/YajCacheConfig.java | 3 +- .../commons/jcs/yajcache/core/CacheEntry.java | 1 - .../commons/jcs/yajcache/core/CacheManager.java | 165 +++++++++++---------- .../commons/jcs/yajcache/core/CacheType.java | 68 ++++----- .../apache/commons/jcs/yajcache/core/ICache.java | 1 - .../commons/jcs/yajcache/core/ICacheSafe.java | 1 - .../jcs/yajcache/core/SafeCacheWrapper.java | 20 +-- .../jcs/yajcache/file/CacheFileContent.java | 17 +-- .../yajcache/file/CacheFileContentCorrupted.java | 8 +- .../jcs/yajcache/file/CacheFileContentType.java | 3 +- .../commons/jcs/yajcache/file/CacheFileDAO.java | 44 ++---- .../commons/jcs/yajcache/file/CacheFileUtils.java | 3 +- .../yajcache/lang/annotation/CopyRightApache.java | 34 ----- .../yajcache/lang/annotation/CopyRightType.java | 50 ------- .../jcs/yajcache/lang/annotation/Immutable.java | 1 - .../jcs/yajcache/lang/annotation/Implements.java | 3 +- .../jcs/yajcache/lang/annotation/JavaBean.java | 1 - .../jcs/yajcache/lang/annotation/NonNullable.java | 1 - .../commons/jcs/yajcache/lang/annotation/TODO.java | 1 - .../jcs/yajcache/lang/annotation/TestOnly.java | 1 - .../jcs/yajcache/lang/annotation/ThreadSafety.java | 1 - .../yajcache/lang/annotation/ThreadSafetyType.java | 3 +- .../lang/annotation/UnsupportedOperation.java | 1 - .../apache/commons/jcs/yajcache/lang/ref/IKey.java | 1 - .../jcs/yajcache/lang/ref/KeyedRefCollector.java | 17 +-- .../jcs/yajcache/lang/ref/KeyedSoftReference.java | 17 +-- .../jcs/yajcache/lang/ref/KeyedWeakReference.java | 1 - .../commons/jcs/yajcache/soft/SoftRefCache.java | 32 ++-- .../jcs/yajcache/soft/SoftRefFileCache.java | 43 +++--- .../commons/jcs/yajcache/util/BeanUtils.java | 22 +-- .../commons/jcs/yajcache/util/ClassUtils.java | 14 +- .../commons/jcs/yajcache/util/CollectionUtils.java | 3 - .../commons/jcs/yajcache/util/EqualsUtils.java | 8 +- .../commons/jcs/yajcache/util/SerializeUtils.java | 25 ++-- .../util/concurrent/locks/IKeyedReadWriteLock.java | 3 - .../util/concurrent/locks/KeyedReadWriteLock.java | 24 ++- .../jcs/yajcache/core/CacheManagerTest.java | 65 ++++---- .../jcs/yajcache/core/SafeCacheManagerTest.java | 84 +++++------ .../jcs/yajcache/file/CacheFileDAOTest.java | 35 ++--- .../jcs/yajcache/file/FileContentTypeTest.java | 17 ++- .../lang/annotation/CopyRightApacheTest.java | 37 ----- .../jcs/yajcache/soft/SoftRefCacheTest.java | 14 +- .../yajcache/soft/SoftRefFileCacheSafeTest.java | 13 +- .../jcs/yajcache/util/SerializeUtilsTest.java | 17 ++- .../jcs/yajcache/util/TestSerializable.java | 2 - 58 files changed, 411 insertions(+), 596 deletions(-) diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/zip/CompressionUtilUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/zip/CompressionUtilUnitTest.java index a697ba8d..120c5f2d 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/zip/CompressionUtilUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/zip/CompressionUtilUnitTest.java @@ -21,12 +21,14 @@ package org.apache.commons.jcs4.utils.zip; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; +import org.junit.jupiter.api.Test; + /** Tests for the compression util */ public class CompressionUtilUnitTest { @@ -35,6 +37,7 @@ public class CompressionUtilUnitTest * * @throws IOException */ + @Test public final void testCompressDecompressByteArray_success() throws IOException { @@ -56,6 +59,7 @@ public class CompressionUtilUnitTest * * @throws IOException */ + @Test public final void testCompressDecompressGzipByteArray_success() throws IOException { @@ -79,20 +83,10 @@ public class CompressionUtilUnitTest } /** Test method for decompressByteArray. */ + @Test public final void testDecompressByteArray_failure() { - try - { - // DO WORK - CompressionUtil.decompressByteArray( null ); - - // VERIFY - fail( "excepted an IllegalArgumentException" ); - } - catch ( final IllegalArgumentException exception ) - { - // expected - return; - } + assertThrows(IllegalArgumentException.class, () -> CompressionUtil.decompressByteArray( null ), + "excepted an IllegalArgumentException"); } } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/README.txt b/commons-jcs4-sandbox/commons-jcs4-yajcache/README.txt index 601f701a..3ca30d41 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/README.txt +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/README.txt @@ -39,8 +39,6 @@ All caches should be retrieved or removed via Pre-requisite ------------- 1) jdk 1.5.0_01+ installed -1) Ant 1.6.2+ installed -2) copy lib/junit-3.8.1.jar to your <ANT_HOME>/lib/ 3) For memory-file cache, the library needs to create a root directory /tmp/yajcache/ @@ -51,7 +49,7 @@ Build ----- Simply type: -ant +mvn clean package Alternatively, if you've got NetBeans 4.0, life is even easier. Simply open up the "cache" project folder, and build it. diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheChangeEvent.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheChangeEvent.java index ce9a6671..995c7541 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheChangeEvent.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheChangeEvent.java @@ -20,21 +20,25 @@ package org.apache.commons.jcs.yajcache.beans; */ import org.apache.commons.jcs.yajcache.core.ICache; -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; /** */ -@CopyRightApache public abstract class CacheChangeEvent<V> extends java.util.EventObject { + private static final long serialVersionUID = 7905317479465838141L; + /** Creates a new instance of CacheEvent */ protected CacheChangeEvent(@NonNullable final ICache<V> cache) { super(cache); } + /** Returns the cache which is the source of the events. */ + @SuppressWarnings("unchecked") protected @NonNullable ICache<V> getCache() { - return (ICache<V>)super.getSource(); + return (ICache<V>) super.getSource(); } + /** - * Dispatches the beans handling to the specific method invokation of the + * Dispatches the beans handling to the specific method invocation of the * given handler. */ public abstract boolean dispatch(@NonNullable ICacheChangeHandler<V> handler); diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheChangeSupport.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheChangeSupport.java index eb724547..4e815f3e 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheChangeSupport.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheChangeSupport.java @@ -19,16 +19,16 @@ package org.apache.commons.jcs.yajcache.beans; * under the License. */ -import org.apache.commons.jcs.yajcache.core.ICache; -import org.apache.commons.jcs.yajcache.lang.annotation.*; - import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import org.apache.commons.jcs.yajcache.core.ICache; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; + /** */ -@CopyRightApache public class CacheChangeSupport<V> { + private final @NonNullable List<ICacheChangeListener<V>> listeners = new CopyOnWriteArrayList<>(); diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheClearEvent.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheClearEvent.java index 07db6d3c..efd086df 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheClearEvent.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheClearEvent.java @@ -20,11 +20,12 @@ package org.apache.commons.jcs.yajcache.beans; */ import org.apache.commons.jcs.yajcache.core.ICache; -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; /** */ -@CopyRightApache public class CacheClearEvent<V> extends CacheChangeEvent<V> { + private static final long serialVersionUID = -4103088208518299928L; + public CacheClearEvent(@NonNullable final ICache<V> cache) { super(cache); diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutBeanCloneEvent.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutBeanCloneEvent.java index 551ec372..787fb236 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutBeanCloneEvent.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutBeanCloneEvent.java @@ -20,11 +20,12 @@ package org.apache.commons.jcs.yajcache.beans; */ import org.apache.commons.jcs.yajcache.core.ICache; -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; /** */ -@CopyRightApache public class CachePutBeanCloneEvent<V> extends CachePutEvent<V> { + private static final long serialVersionUID = 108017413065659237L; + protected CachePutBeanCloneEvent(@NonNullable final ICache<V> cache, @NonNullable final String key, @NonNullable final V val) { diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutBeanCopyEvent.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutBeanCopyEvent.java index c5653183..726dda89 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutBeanCopyEvent.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutBeanCopyEvent.java @@ -20,11 +20,12 @@ package org.apache.commons.jcs.yajcache.beans; */ import org.apache.commons.jcs.yajcache.core.ICache; -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; /** */ -@CopyRightApache public class CachePutBeanCopyEvent<V> extends CachePutEvent<V> { + private static final long serialVersionUID = 7668603902135440728L; + public CachePutBeanCopyEvent(@NonNullable final ICache<V> cache, @NonNullable final String key, @NonNullable final V val) { diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutCopyEvent.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutCopyEvent.java index d2751cb3..99889303 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutCopyEvent.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutCopyEvent.java @@ -20,11 +20,12 @@ package org.apache.commons.jcs.yajcache.beans; */ import org.apache.commons.jcs.yajcache.core.ICache; -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; /** */ -@CopyRightApache public class CachePutCopyEvent<V> extends CachePutEvent<V> { + private static final long serialVersionUID = -5452066439473355390L; + public CachePutCopyEvent(@NonNullable final ICache<V> cache, @NonNullable final String key, @NonNullable final V val) { diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutEvent.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutEvent.java index 42525850..879e0e74 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutEvent.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CachePutEvent.java @@ -20,11 +20,12 @@ package org.apache.commons.jcs.yajcache.beans; */ import org.apache.commons.jcs.yajcache.core.ICache; -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; /** */ -@CopyRightApache public class CachePutEvent<V> extends CacheChangeEvent<V> { + private static final long serialVersionUID = -8382959786540510736L; + private final @NonNullable String key; private final @NonNullable V value; diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheRemoveEvent.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheRemoveEvent.java index a43c7bcd..e55f7200 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheRemoveEvent.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/CacheRemoveEvent.java @@ -20,11 +20,12 @@ package org.apache.commons.jcs.yajcache.beans; */ import org.apache.commons.jcs.yajcache.core.ICache; -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; /** */ -@CopyRightApache public class CacheRemoveEvent<V> extends CacheChangeEvent<V> { + private static final long serialVersionUID = -6725579678468214003L; + private final @NonNullable String key; public CacheRemoveEvent(@NonNullable final ICache<V> cache, diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/ICacheChangeHandler.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/ICacheChangeHandler.java index 84d03d27..7836e021 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/ICacheChangeHandler.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/ICacheChangeHandler.java @@ -24,7 +24,6 @@ import org.apache.commons.jcs.yajcache.lang.annotation.*; /** * Cache change beans listener/handler. */ -@CopyRightApache public interface ICacheChangeHandler<V> { boolean handlePut(@NonNullable String cacheName, @NonNullable String key, @NonNullable V value); diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/ICacheChangeListener.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/ICacheChangeListener.java index 48ff967a..528e8c08 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/ICacheChangeListener.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/beans/ICacheChangeListener.java @@ -18,13 +18,11 @@ package org.apache.commons.jcs.yajcache.beans; * specific language governing permissions and limitations * under the License. */ - -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; /** * Cache change beans listener/handler. */ -@CopyRightApache public interface ICacheChangeListener<V> extends java.util.EventListener { void cacheChange(@NonNullable CacheChangeEvent<V> evt); } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/config/PerCacheConfig.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/config/PerCacheConfig.java index 510f4c70..4e08dac0 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/config/PerCacheConfig.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/config/PerCacheConfig.java @@ -23,7 +23,6 @@ import org.apache.commons.jcs.yajcache.lang.annotation.*; /** */ -@CopyRightApache @JavaBean @TODO("configuration via XML file") public class PerCacheConfig { diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/config/YajCacheConfig.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/config/YajCacheConfig.java index e7d683cf..4b1a49d5 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/config/YajCacheConfig.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/config/YajCacheConfig.java @@ -25,8 +25,7 @@ import java.io.File; /** */ -//@CopyRightApache -//@TODO("Optional configuration via XML config file") +////@TODO("Optional configuration via XML config file") // http://www.netbeans.org/issues/show_bug.cgi?id=53704 public enum YajCacheConfig { inst; diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheEntry.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheEntry.java index 0915124b..97a8f3e0 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheEntry.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheEntry.java @@ -27,7 +27,6 @@ import java.util.Map; /** */ -@CopyRightApache public class CacheEntry<V> implements Map.Entry<String,V> { private @NonNullable final String key; private @NonNullable V value; diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheManager.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheManager.java index a088754d..8e69c07d 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheManager.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheManager.java @@ -1,5 +1,10 @@ package org.apache.commons.jcs.yajcache.core; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.Lock; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -20,22 +25,17 @@ package org.apache.commons.jcs.yajcache.core; */ import org.apache.commons.jcs.yajcache.file.CacheFileUtils; -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; +import org.apache.commons.jcs.yajcache.lang.annotation.TestOnly; import org.apache.commons.jcs.yajcache.soft.SoftRefFileCache; import org.apache.commons.jcs.yajcache.util.concurrent.locks.IKeyedReadWriteLock; import org.apache.commons.jcs.yajcache.util.concurrent.locks.KeyedReadWriteLock; import org.apache.commons.lang3.builder.ToStringBuilder; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.locks.Lock; - /** * Enumerates cache managers for getting, creating and removing named caches. */ -// @CopyRightApache -// http://www.netbeans.org/issues/show_bug.cgi?id=53704 +// // http://www.netbeans.org/issues/show_bug.cgi?id=53704 public enum CacheManager { inst; @@ -43,16 +43,14 @@ public enum CacheManager { private final AtomicInteger countGetCache = new AtomicInteger(); private final AtomicInteger countCreateCache = new AtomicInteger(); - private final AtomicInteger countCreateCacheRace = new AtomicInteger(); private final AtomicInteger countCreateFileCache = new AtomicInteger(); - private final AtomicInteger countCreateFileCacheRace = new AtomicInteger(); private final AtomicInteger countRemoveCache = new AtomicInteger(); private final AtomicInteger countRemoveFileCache = new AtomicInteger(); // Cache name to Cache mapping. - private final ConcurrentMap<String,ICache<?>> map = - new ConcurrentHashMap<>(); + private final ConcurrentMap<String,ICache<?>> map = new ConcurrentHashMap<>(); + /** * Used for entire cache with external IO, * so cache create/removal won't conflict with normal get/put operations. @@ -63,36 +61,39 @@ public enum CacheManager { * Returns an existing cache for the specified name; * or null if not found. */ - public ICache getCache(@NonNullable final String name) { - return this.map.get(name); + @SuppressWarnings("unchecked") + public <V> ICache<V> getCache(@NonNullable final String name) { + return (ICache<V>) map.get(name); } + /** * Returns an existing safe cache for the specified name; * or null if such a safe cache cannot not found. */ - public ICacheSafe getSafeCache(@NonNullable final String name) { - final ICache c = this.getCache(name); + public <V> ICacheSafe<V> getSafeCache(@NonNullable final String name) { + final ICache<V> c = getCache(name); - if (!(c instanceof ICacheSafe)) { - return null; + if (c instanceof ICacheSafe<V> safeCache) { + return safeCache; } - return (ICacheSafe)c; + return null; } + /** * Returns an existing cache for the specified name and value type; * or null if not found. */ -// @SuppressWarnings({"unchecked"}) public <V> ICache<V> getCache( @NonNullable final String name, @NonNullable final Class<V> valueType) { if (debug) { - this.countGetCache.incrementAndGet(); + countGetCache.incrementAndGet(); } - final ICache c = this.map.get(name); + final ICache<V> c = getCache(name); return c != null && checkValueType(c, valueType) ? c : null; } + /** * Returns an existing safe cache for the specified name and value type; * or null if such a safe cache cannot be found. @@ -101,13 +102,14 @@ public enum CacheManager { @NonNullable final String name, @NonNullable final Class<V> valueType) { - final ICache<V> c = this.getCache(name, valueType); + final ICache<V> c = getCache(name, valueType); - if (!(c instanceof ICacheSafe)) { - return null; + if (c instanceof ICacheSafe<V> safeCache) { + return checkValueType(c, valueType) ? safeCache : null; } - return checkValueType(c, valueType) ? (ICacheSafe<V>)c : null; + return null; } + /** * Returns a cache for the specified name, value type and cache type. * Creates the cache if necessary. @@ -120,27 +122,28 @@ public enum CacheManager { @NonNullable final Class<V> valueType, @NonNullable final CacheType cacheType) { - ICache c = this.map.get(name); + ICache<V> c = getCache(name); if (c == null) { switch(cacheType) { case SOFT_REFERENCE: case SOFT_REFERENCE_SAFE: - c = this.tryCreateCache(name, valueType, cacheType); + c = tryCreateCache(name, valueType, cacheType); break; case SOFT_REFERENCE_FILE: case SOFT_REFERENCE_FILE_SAFE: - c = this.tryCreateFileCache(name, valueType, cacheType); + c = tryCreateFileCache(name, valueType, cacheType); break; default: throw new AssertionError(cacheType); } } else { - this.checkTypes(c, cacheType, valueType); + checkTypes(c, cacheType, valueType); } return c; } + /** * Returns a safe cache for the specified name, value type and cache type. * Creates the cache if necessary. @@ -162,16 +165,18 @@ public enum CacheManager { default: throw new IllegalArgumentException(cacheType.toString()); } - return (ICacheSafe<V>)this.getCache(name, valueType, cacheType); + return (ICacheSafe<V>) getCache(name, valueType, cacheType); } + /** * Removes the specified cache, if it exists. */ - public ICache removeCache(@NonNullable final String name) { + public <V> ICache<V> removeCache(@NonNullable final String name) { if (debug) { - this.countRemoveCache.incrementAndGet(); + countRemoveCache.incrementAndGet(); } - final ICache c = this.map.remove(name); + @SuppressWarnings("unchecked") + final ICache<V> c = (ICache<V>) map.remove(name); if (c != null) { final CacheType cacheType = c.getCacheType(); @@ -184,9 +189,9 @@ public enum CacheManager { case SOFT_REFERENCE_FILE: case SOFT_REFERENCE_FILE_SAFE: if (debug) { - this.countRemoveFileCache.incrementAndGet(); + countRemoveFileCache.incrementAndGet(); } - final Lock lock = this.keyedRWLock.writeLock(name); + final Lock lock = keyedRWLock.writeLock(name); lock.lock(); try { // Clear will delete the files as well. @@ -203,6 +208,7 @@ public enum CacheManager { } return c; } + /** * Creates the specified cache if not already created. * @@ -212,30 +218,29 @@ public enum CacheManager { * @throws ClassCastException if the cache already exists for an * incompatible value type or incompatible cache type. */ -// @SuppressWarnings({"unchecked"}) private @NonNullable <V> ICache<V> tryCreateCache( @NonNullable final String name, @NonNullable final Class<V> valueType, @NonNullable final CacheType cacheType) { if (debug) { - this.countCreateCache.incrementAndGet(); + countCreateCache.incrementAndGet(); } - final ICache<V> newCache = cacheType.createCache(name, valueType); -// SoftRefFileCache<V> newCache = new SoftRefFileCache<V>(name, valueType); -// newCache.addCacheChangeListener(new CacheFileManager<V>(newCache)); - final ICache oldCache = this.map.putIfAbsent(name, newCache); - if (oldCache != null) { - // race condition: cache already created by another thread. - if (debug) { - this.countCreateCacheRace.incrementAndGet(); + @SuppressWarnings("unchecked") + final ICache<V> cache = (ICache<V>) map.compute(name, (k, v) -> { + if (v == null) { + return cacheType.createCache(k, valueType); + } else if (checkValueType((ICache<V>) v, valueType)) { + return v; + } else { + throw new ClassCastException(valueType + " is incompatible with " + v.getValueType()); } - this.checkTypes(oldCache, cacheType, valueType); - return oldCache; - } - return newCache; + }); + + return cache; } + /** * Creates the specified file cache if not already created. * @@ -245,48 +250,39 @@ public enum CacheManager { * @throws ClassCastException if the cache already exists for an * incompatible value type or incompatible cache type. */ + @SuppressWarnings("unchecked") private @NonNullable <V> ICache<V> tryCreateFileCache( @NonNullable final String name, @NonNullable final Class<V> valueType, @NonNullable final CacheType cacheType) { if (debug) { - this.countCreateFileCache.incrementAndGet(); + countCreateFileCache.incrementAndGet(); } - ICache<V> newCache = null; - ICache oldCache = null; - final Lock lock = this.keyedRWLock.writeLock(name); + ICache<V> cache = null; + final Lock lock = keyedRWLock.writeLock(name); lock.lock(); try { - newCache = cacheType.createCache(name, valueType); - oldCache = this.map.putIfAbsent(name, newCache); + cache = (ICache<V>) map.computeIfAbsent(name, k -> cacheType.createCache(k, valueType)); } finally { lock.unlock(); } - - if (oldCache != null) { - // race condition: cache already created by another thread. - if (debug) { - this.countCreateFileCacheRace.incrementAndGet(); - } - this.checkTypes(oldCache, cacheType, valueType); - return oldCache; - } - return newCache; + return cache; } @TestOnly("Used solely to simluate a race condition during cache creation ") @NonNullable <V> ICache<V> testCreateCacheRaceCondition( @NonNullable final String name, @NonNullable final Class<V> valueType, @NonNullable final CacheType cacheType) { - return this.tryCreateCache(name, valueType, cacheType); + return tryCreateCache(name, valueType, cacheType); } @TestOnly("Used solely to simluate a race condition during cache creation ") @NonNullable <V> ICache<V> testCreateFileCacheRaceCondition( @NonNullable final String name, @NonNullable final Class<V> valueType, @NonNullable final CacheType cacheType) { - return this.tryCreateCache(name, valueType, cacheType); + return tryCreateCache(name, valueType, cacheType); } + /** * Checks the compatibility of the given cacheType and valueType with the * given cache. @@ -294,13 +290,13 @@ public enum CacheManager { * @throws ClassCastException if the cache already exists for an * incompatible value type or incompatible cache type. */ - private <V> void checkTypes(final ICache c, + private <V> void checkTypes(final ICache<V> c, @NonNullable final CacheType cacheType, @NonNullable final Class<V> valueType) { if (c == null) { return; } - if (!c.getCacheType().isAsssignableFrom(cacheType)) { + if (!c.getCacheType().isAssignableFrom(cacheType)) { throw new ClassCastException("Cache " + c.getName() + " of type " + c.getCacheType() + " already exists and cannot be used for cache type " + cacheType); @@ -311,6 +307,7 @@ public enum CacheManager { + " already exists and cannot be used for value type " + valueType); } } + /** * Checks the compatibility of the given valueType with the * given cache. @@ -318,31 +315,35 @@ public enum CacheManager { * @return true if the valueType is compatible with the cache; * false otherwise. */ - private boolean checkValueType(@NonNullable final ICache c, @NonNullable final Class<?> valueType) + private <V> boolean checkValueType(@NonNullable final ICache<V> c, @NonNullable final Class<V> valueType) { - final Class<?> cacheValueType = c.getValueType(); + final Class<V> cacheValueType = c.getValueType(); return cacheValueType.isAssignableFrom(valueType); } + /** Retrieves a read lock on the given file cache. */ public Lock readLock(final SoftRefFileCache<?> cache) { - return this.keyedRWLock.readLock(cache.getName()); + return keyedRWLock.readLock(cache.getName()); + } + + /** Dispose all cache instances */ + public void dispose() { + map.keySet().forEach(this::removeCache); + map.clear(); } + @Override public String toString() { return new ToStringBuilder(this) .append("\n") - .append("countCreateCache", this.countCreateCache) - .append("\n") - .append("countCreateCacheRace", this.countCreateCacheRace) - .append("\n") - .append("countCreateFileCache", this.countCreateFileCache) + .append("countCreateCache", countCreateCache) .append("\n") - .append("countCreateFileCacheRace", this.countCreateFileCacheRace) + .append("countCreateFileCache", countCreateFileCache) .append("\n") - .append("countCreateFileCacheRace", this.countGetCache) + .append("countCreateFileCacheRace", countGetCache) .append("\n") - .append("countRemoveCache", this.countRemoveCache) + .append("countRemoveCache", countRemoveCache) .append("\n") - .append("countRemoveFileCache", this.countRemoveFileCache) + .append("countRemoveFileCache", countRemoveFileCache) .toString(); } } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheType.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheType.java index 7ee6dc32..caadbb9c 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheType.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/CacheType.java @@ -18,54 +18,54 @@ package org.apache.commons.jcs.yajcache.core; * specific language governing permissions and limitations * under the License. */ - -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; import org.apache.commons.jcs.yajcache.soft.SoftRefCache; import org.apache.commons.jcs.yajcache.soft.SoftRefFileCache; /** * Enumerates cache types. */ -// @CopyRightApache -// http://www.netbeans.org/issues/show_bug.cgi?id=53704 +// // http://www.netbeans.org/issues/show_bug.cgi?id=53704 public enum CacheType { - SOFT_REFERENCE, - SOFT_REFERENCE_SAFE, - SOFT_REFERENCE_FILE, - SOFT_REFERENCE_FILE_SAFE; - - /** Instantiates and returns a new instance of cache of the current type. */ - <V> ICache<V> createCache(final String name, @NonNullable final Class<V> valueType) - { - switch(this) { - case SOFT_REFERENCE: - return new SoftRefCache<>(name, valueType); - case SOFT_REFERENCE_SAFE: - return new SafeCacheWrapper<>(new SoftRefCache<>(name, valueType)); - case SOFT_REFERENCE_FILE: - return new SoftRefFileCache<>(name, valueType); - case SOFT_REFERENCE_FILE_SAFE: - return new SafeCacheWrapper<>(new SoftRefFileCache<>(name, valueType)); + SOFT_REFERENCE { + @Override + public <V> ICache<V> createCache(String name, Class<V> valueType) + { + return new SoftRefCache<>(name, valueType); } - throw new AssertionError(this); - } - /** Instantiates and returns a new instance of safe cache of the current type. */ - <V> ICacheSafe<V> createSafeCache(final String name, @NonNullable final Class<V> valueType) - { - switch(this) { - case SOFT_REFERENCE_SAFE: - return new SafeCacheWrapper<>(new SoftRefCache<>(name, valueType)); - case SOFT_REFERENCE_FILE_SAFE: - return new SafeCacheWrapper<>(new SoftRefFileCache<>(name, valueType)); + }, + SOFT_REFERENCE_SAFE { + @Override + public <V> ICacheSafe<V> createCache(String name, Class<V> valueType) + { + return new SafeCacheWrapper<>(new SoftRefCache<>(name, valueType)); } - throw new UnsupportedOperationException(""); - } + }, + SOFT_REFERENCE_FILE { + @Override + public <V> ICache<V> createCache(String name, Class<V> valueType) + { + return new SoftRefFileCache<>(name, valueType); + } + }, + SOFT_REFERENCE_FILE_SAFE { + @Override + public <V> ICacheSafe<V> createCache(String name, Class<V> valueType) + { + return new SafeCacheWrapper<>(new SoftRefFileCache<>(name, valueType)); + } + }; + + + /** Instantiates and returns a new instance of cache of the current type. */ + public abstract <V> ICache<V> createCache(final String name, @NonNullable final Class<V> valueType); + /** * Returns true if cache of the given cache type can be used as * cache of the current cache type; * false otherwise. */ - public boolean isAsssignableFrom(final CacheType from) { + public boolean isAssignableFrom(final CacheType from) { switch(this) { case SOFT_REFERENCE: return true; diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/ICache.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/ICache.java index a3f72377..10236145 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/ICache.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/ICache.java @@ -26,7 +26,6 @@ import java.util.Map; /** * Interface of a Cache. */ -@CopyRightApache @ThreadSafety(ThreadSafetyType.SAFE) public interface ICache<V> extends Map<String,V> { /** Returns the cache name. */ diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/ICacheSafe.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/ICacheSafe.java index 0b8c6895..c70fc49d 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/ICacheSafe.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/ICacheSafe.java @@ -25,7 +25,6 @@ import java.util.Map; /** */ -@CopyRightApache @ThreadSafety(ThreadSafetyType.SAFE) public interface ICacheSafe<V> extends ICache<V> { /** diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/SafeCacheWrapper.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/SafeCacheWrapper.java index 5f900e2c..a7146a92 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/SafeCacheWrapper.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/core/SafeCacheWrapper.java @@ -1,5 +1,10 @@ package org.apache.commons.jcs.yajcache.core; +import java.io.Serializable; +import java.util.Collection; +import java.util.Map; +import java.util.Set; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,21 +23,15 @@ package org.apache.commons.jcs.yajcache.core; * specific language governing permissions and limitations * under the License. */ - -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.Implements; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; import org.apache.commons.jcs.yajcache.util.BeanUtils; import org.apache.commons.jcs.yajcache.util.SerializeUtils; import org.apache.commons.lang3.builder.ToStringBuilder; -import java.io.Serializable; -import java.util.Collection; -import java.util.Map; -import java.util.Set; - /** * Safe Cache as a wrapper of an underlying cache. */ -@CopyRightApache public class SafeCacheWrapper<V> implements ICacheSafe<V> { /** Underlying cache. */ @@ -172,9 +171,10 @@ public class SafeCacheWrapper<V> implements ICacheSafe<V> this.cache.put(e.getKey(), BeanUtils.inst.cloneShallow(e.getValue())); } } + @SuppressWarnings("unchecked") private V dup(final V val) { - if (val instanceof Serializable) { - return (V)SerializeUtils.inst.dup((Serializable)val); + if (val instanceof Serializable ser) { + return (V) SerializeUtils.inst.dup(ser); } return val; } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContent.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContent.java index 5aae80d0..de5056f7 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContent.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContent.java @@ -1,5 +1,9 @@ package org.apache.commons.jcs.yajcache.file; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.Arrays; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,12 +22,7 @@ package org.apache.commons.jcs.yajcache.file; * specific language governing permissions and limitations * under the License. */ - -import org.apache.commons.jcs.yajcache.lang.annotation.*; - -import java.io.IOException; -import java.io.RandomAccessFile; -import java.util.Arrays; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; /** * Cache File Content which represents the file persistence format @@ -37,7 +36,6 @@ import java.util.Arrays; * <ByteArray> : byte[] *</pre> */ -@CopyRightApache public class CacheFileContent { public static final CacheFileContent CORRUPTED = CacheFileContentCorrupted.inst; /** @@ -150,7 +148,8 @@ public class CacheFileContent { return cfc; } /** Returns the deserialized content. */ - public @NonNullable Object deserialize() { - return CacheFileContentType.fromByte(this.contentType).deserialize(this.content); + @SuppressWarnings("unchecked") + public @NonNullable <V> V deserialize() { + return (V) CacheFileContentType.fromByte(this.contentType).deserialize(this.content); } } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContentCorrupted.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContentCorrupted.java index 1eaccdb6..aad1b871 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContentCorrupted.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContentCorrupted.java @@ -18,16 +18,14 @@ package org.apache.commons.jcs.yajcache.file; * specific language governing permissions and limitations * under the License. */ - -import org.apache.commons.jcs.yajcache.lang.annotation.*; - import java.io.IOException; import java.io.RandomAccessFile; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; + /** * Corrupted Cache File Content. */ -@CopyRightApache public class CacheFileContentCorrupted extends CacheFileContent { static CacheFileContentCorrupted inst = new CacheFileContentCorrupted(); @@ -68,7 +66,7 @@ public class CacheFileContentCorrupted extends CacheFileContent { return false; } /** Returns the deserialized content. */ - @Override public Object deserialize() { + @Override public <V> V deserialize() { return null; } } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContentType.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContentType.java index 4f7c8aad..263e9163 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContentType.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileContentType.java @@ -30,8 +30,7 @@ import java.io.Serializable; * Currently the content of a cache item can be persisted into an array of * bytes via either Java Serialization, or the XMLEncoder. */ -// @CopyRightApache -// http://www.netbeans.org/issues/show_bug.cgi?id=53704 +// // http://www.netbeans.org/issues/show_bug.cgi?id=53704 public enum CacheFileContentType { JAVA_SERIALIZATION, XML_ENCODER; diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileDAO.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileDAO.java index 3250e445..d6b5a8f8 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileDAO.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileDAO.java @@ -25,27 +25,24 @@ import java.io.RandomAccessFile; import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; +import org.apache.commons.jcs4.log.Log; import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; /** * Enumerates cache File data access object. */ -// @CopyRightApache -// http://www.netbeans.org/issues/show_bug.cgi?id=53704 +// // http://www.netbeans.org/issues/show_bug.cgi?id=53704 public enum CacheFileDAO { inst; private final AtomicInteger countWriteIOException = new AtomicInteger(); - private final AtomicInteger countWriteCloseException = new AtomicInteger(); private final AtomicInteger countReadIOException = new AtomicInteger(); - private final AtomicInteger countReadCloseException = new AtomicInteger(); private final AtomicInteger countCorruptMinLength = new AtomicInteger(); private final AtomicInteger countCorruptLength = new AtomicInteger(); private final AtomicInteger countCorruptInvalid = new AtomicInteger(); - private final Log log = LogFactory.getLog(this.getClass()); + /** The logger. */ + private final Log log = Log.getLog(this.getClass()); /** * Writes the specified cache item into the file system. @@ -57,25 +54,15 @@ public enum CacheFileDAO { @NonNullable final String key, @NonNullable final byte[] val) { final File file = CacheFileUtils.inst.getCacheFile(cacheName, key); - RandomAccessFile raf = null; - try { - file.delete(); - file.createNewFile(); - raf = new RandomAccessFile(file, "rw"); + file.delete(); + + try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) + { CacheFileContent.getInstance(type, val).write(raf); return true; } catch (final IOException ex) { countWriteIOException.incrementAndGet(); log.error("", ex); - } finally { - if (raf != null) { - try { - raf.close(); - } catch (final Exception ex) { - countWriteCloseException.incrementAndGet(); - log.error("", ex); - } - } } return false; } @@ -101,9 +88,9 @@ public enum CacheFileDAO { + cacheName + " key=" + key); return CacheFileContent.CORRUPTED; } - RandomAccessFile raf = null; - try { - raf = new RandomAccessFile(file, "r"); + + try (RandomAccessFile raf = new RandomAccessFile(file, "r")) + { final CacheFileContent cfc = CacheFileContent.getInstance(raf); if (cfc.isValid()) { @@ -127,15 +114,6 @@ public enum CacheFileDAO { } catch (final IOException | org.apache.commons.lang3.SerializationException ex) { countReadIOException.incrementAndGet(); log.warn(ex.getClass().getName(), ex); - } finally { - if (raf != null) { - try { - raf.close(); - } catch (final Exception ex) { - countReadCloseException.incrementAndGet(); - log.error("", ex); - } - } } return CacheFileContent.CORRUPTED; } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileUtils.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileUtils.java index 3659cef3..965736ae 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileUtils.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/file/CacheFileUtils.java @@ -27,8 +27,7 @@ import java.io.File; /** * Enumerates cache file utilities. */ -// @CopyRightApache -// http://www.netbeans.org/issues/show_bug.cgi?id=53704 +// // http://www.netbeans.org/issues/show_bug.cgi?id=53704 public enum CacheFileUtils { inst; diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/CopyRightApache.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/CopyRightApache.java deleted file mode 100644 index 78f53664..00000000 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/CopyRightApache.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.apache.commons.jcs.yajcache.lang.annotation; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -/** - * Annotates the Apache Copyright. - */ -@CopyRightApache -@Documented -@Retention(RetentionPolicy.RUNTIME) -public @interface CopyRightApache { - CopyRightType value() default CopyRightType.APACHE; -} diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/CopyRightType.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/CopyRightType.java deleted file mode 100644 index abaa6793..00000000 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/CopyRightType.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.apache.commons.jcs.yajcache.lang.annotation; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/** - * Enumerates copyright types. - */ -// @CopyRightApache -// http://www.netbeans.org/issues/show_bug.cgi?id=53704 -public enum CopyRightType { - APACHE { - @Override public String toString() { - return "\n" - + "/* ========================================================================\n" - + " * Copyright 2005 The Apache Software Foundation\n" - + " *\n" - + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" - + " * you may not use this file except in compliance with the License.\n" - + " * You may obtain a copy of the License at\n" - + " *\n" - + " * https://www.apache.org/licenses/LICENSE-2.0\n" - + " *\n" - + " * Unless required by applicable law or agreed to in writing, software\n" - + " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" - + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - + " * See the License for the specific language governing permissions and\n" - + " * limitations under the License.\n" - + " * ========================================================================\n" - + " */\n" - ; - } - }; -} diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/Immutable.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/Immutable.java index d1ed9f6c..ef7c9927 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/Immutable.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/Immutable.java @@ -29,7 +29,6 @@ import java.lang.annotation.Target; /** * Element so annotated is expected to be immutable. */ -@CopyRightApache @Documented @Inherited @Retention(RetentionPolicy.SOURCE) diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/Implements.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/Implements.java index d20220b2..d783005b 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/Implements.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/Implements.java @@ -22,8 +22,7 @@ package org.apache.commons.jcs.yajcache.lang.annotation; /** * Implements the specified interface. */ -@CopyRightApache public @interface Implements { /** Interface being implemented. */ - public Class value(); + public Class<?> value(); } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/JavaBean.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/JavaBean.java index 7a2c6b42..03feeb85 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/JavaBean.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/JavaBean.java @@ -29,7 +29,6 @@ import java.lang.annotation.Target; /** * Element so annotated is expected to be a JavaBean. */ -@CopyRightApache @Documented @Inherited @Retention(RetentionPolicy.SOURCE) diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/NonNullable.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/NonNullable.java index a11c8f76..ecce4436 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/NonNullable.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/NonNullable.java @@ -29,7 +29,6 @@ import java.lang.annotation.Target; /** * Element so annotated is never expected to be null. */ -@CopyRightApache @Documented @Inherited @Retention(RetentionPolicy.SOURCE) diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/TODO.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/TODO.java index 6afe8dde..f0a3516f 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/TODO.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/TODO.java @@ -26,7 +26,6 @@ import java.lang.annotation.RetentionPolicy; /** * Annotates what needs to be done. */ -@CopyRightApache @Documented @Retention(RetentionPolicy.SOURCE) public @interface TODO { diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/TestOnly.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/TestOnly.java index 07fa7565..1745f3b9 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/TestOnly.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/TestOnly.java @@ -27,7 +27,6 @@ import java.lang.annotation.RetentionPolicy; /** * Annotates the target is for testing purposes only. */ -@CopyRightApache @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/ThreadSafety.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/ThreadSafety.java index 29c5fe0f..3577f091 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/ThreadSafety.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/ThreadSafety.java @@ -28,7 +28,6 @@ import java.lang.annotation.RetentionPolicy; * * http://www-106.ibm.com/developerworks/java/library/j-jtp09263.html */ -@CopyRightApache @Documented @Retention(RetentionPolicy.RUNTIME) public @interface ThreadSafety { diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/ThreadSafetyType.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/ThreadSafetyType.java index 448cff3d..2c2f5d03 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/ThreadSafetyType.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/ThreadSafetyType.java @@ -24,8 +24,7 @@ package org.apache.commons.jcs.yajcache.lang.annotation; * * http://www-106.ibm.com/developerworks/java/library/j-jtp09263.html */ -// @CopyRightApache -// http://www.netbeans.org/issues/show_bug.cgi?id=53704 +// // http://www.netbeans.org/issues/show_bug.cgi?id=53704 public enum ThreadSafetyType { /** Immutable objects are guaranteed to be thread-safe. */ IMMUTABLE, diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/UnsupportedOperation.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/UnsupportedOperation.java index c9787399..dc0f2eb1 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/UnsupportedOperation.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/annotation/UnsupportedOperation.java @@ -28,7 +28,6 @@ import java.lang.annotation.Target; /** * Unsupported Operation. */ -@CopyRightApache @Documented @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/IKey.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/IKey.java index 7005ac5b..e6cb448e 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/IKey.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/IKey.java @@ -23,7 +23,6 @@ import org.apache.commons.jcs.yajcache.lang.annotation.*; /** * Interface for accessing a key. */ -@CopyRightApache public interface IKey<K> { /** Returns the key. */ @NonNullable @Immutable K getKey(); diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedRefCollector.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedRefCollector.java index ad14c85b..284d323e 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedRefCollector.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedRefCollector.java @@ -19,16 +19,14 @@ package org.apache.commons.jcs.yajcache.lang.ref; * under the License. */ -import org.apache.commons.jcs.yajcache.lang.annotation.*; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicInteger; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; +import org.apache.commons.lang3.builder.ToStringBuilder; + /** * Keyed Reference garbage collector which removes stale * Keyed {@link Reference} entries @@ -36,11 +34,9 @@ import java.util.concurrent.atomic.AtomicInteger; * The stale Keyed References are put into the given {@link ReferenceQueue} * by the JVM garbage collector. */ -@CopyRightApache public class KeyedRefCollector<K> implements Runnable { private static final boolean debug = true; - private final Log log = debug ? LogFactory.getLog(this.getClass()) : null; - private final @NonNullable ReferenceQueue q; + private final @NonNullable ReferenceQueue<?> q; private final @NonNullable ConcurrentMap<K, ? extends IKey<K>> synMap; private final AtomicInteger count = new AtomicInteger(); /** @@ -58,10 +54,11 @@ public class KeyedRefCollector<K> implements Runnable { */ @Override public void run() { - Reference ref; + Reference<?> ref; while ((ref = this.q.poll()) != null) { - final IKey keyedRef = (IKey)ref; + @SuppressWarnings("unchecked") + final IKey<K> keyedRef = (IKey<K>) ref; // remove unused lock; may fail but that's fine. synMap.remove(keyedRef.getKey(), ref); // referent should have been cleared by GC. diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedSoftReference.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedSoftReference.java index 3bd2e786..2fe2ac08 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedSoftReference.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedSoftReference.java @@ -1,5 +1,8 @@ package org.apache.commons.jcs.yajcache.lang.ref; +import java.lang.ref.ReferenceQueue; +import java.lang.ref.SoftReference; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,18 +21,14 @@ package org.apache.commons.jcs.yajcache.lang.ref; * specific language governing permissions and limitations * under the License. */ - -import org.apache.commons.jcs.yajcache.lang.annotation.*; - -import java.lang.ref.ReferenceQueue; -import java.lang.ref.SoftReference; +import org.apache.commons.jcs.yajcache.lang.annotation.Immutable; +import org.apache.commons.jcs.yajcache.lang.annotation.Implements; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; /** * {@link SoftReference} with an embedded key. */ -@CopyRightApache -public class KeyedSoftReference<K,T> extends SoftReference<T> - implements IKey<K> +public class KeyedSoftReference<K, T> extends SoftReference<T> implements IKey<K> { /** The embedded key. */ private final @NonNullable @Immutable K key; @@ -44,7 +43,7 @@ public class KeyedSoftReference<K,T> extends SoftReference<T> */ public KeyedSoftReference(@NonNullable @Immutable final K key, final T referent) { - super(referent); + super(referent); this.key = key; } /** diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedWeakReference.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedWeakReference.java index d5285083..330d0ef7 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedWeakReference.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/lang/ref/KeyedWeakReference.java @@ -26,7 +26,6 @@ import java.lang.ref.WeakReference; /** * {@link WeakReference} with an embedded key. */ -@CopyRightApache public class KeyedWeakReference<K,T> extends WeakReference<T> implements IKey<K> { diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/soft/SoftRefCache.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/soft/SoftRefCache.java index da429513..31160cb9 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/soft/SoftRefCache.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/soft/SoftRefCache.java @@ -19,19 +19,6 @@ package org.apache.commons.jcs.yajcache.soft; * under the License. */ -import org.apache.commons.jcs.yajcache.config.PerCacheConfig; -import org.apache.commons.jcs.yajcache.core.CacheEntry; -import org.apache.commons.jcs.yajcache.core.CacheType; -import org.apache.commons.jcs.yajcache.core.ICache; -import org.apache.commons.jcs.yajcache.lang.annotation.*; -import org.apache.commons.jcs.yajcache.lang.ref.KeyedRefCollector; -import org.apache.commons.jcs.yajcache.lang.ref.KeyedSoftReference; -import org.apache.commons.jcs.yajcache.util.CollectionUtils; -import org.apache.commons.jcs.yajcache.util.EqualsUtils; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import java.lang.ref.ReferenceQueue; import java.util.ArrayList; import java.util.Collection; @@ -43,18 +30,29 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicInteger; +import org.apache.commons.jcs.yajcache.config.PerCacheConfig; +import org.apache.commons.jcs.yajcache.core.CacheEntry; +import org.apache.commons.jcs.yajcache.core.CacheType; +import org.apache.commons.jcs.yajcache.core.ICache; +import org.apache.commons.jcs.yajcache.lang.annotation.Implements; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; +import org.apache.commons.jcs.yajcache.lang.annotation.TODO; +import org.apache.commons.jcs.yajcache.lang.ref.KeyedRefCollector; +import org.apache.commons.jcs.yajcache.lang.ref.KeyedSoftReference; +import org.apache.commons.jcs.yajcache.util.CollectionUtils; +import org.apache.commons.jcs.yajcache.util.EqualsUtils; +import org.apache.commons.lang3.builder.ToStringBuilder; + /** * Cache implemented using {@link KeyedSoftReference} and {@link ConcurrentHashMap}. */ -@CopyRightApache @TODO("Annotate the thread-safetyness of the methods") public class SoftRefCache<V> implements ICache<V> { private static final boolean debug = true; - private final Log log = debug ? LogFactory.getLog(this.getClass()) : null; private final @NonNullable ReferenceQueue<V> refq = new ReferenceQueue<>(); private final @NonNullable String name; private final @NonNullable Class<V> valueType; - private final @NonNullable ConcurrentMap<String,KeyedSoftReference<String,V>> map; + private final @NonNullable ConcurrentMap<String, KeyedSoftReference<String,V>> map; private final @NonNullable KeyedRefCollector<String> collector; private @NonNullable PerCacheConfig config; @@ -157,7 +155,7 @@ public class SoftRefCache<V> implements ICache<V> { this.countPut.incrementAndGet(); } this.collector.run(); - final KeyedSoftReference<String,V> oldRef = + final KeyedSoftReference<String, V> oldRef = map.put(key, new KeyedSoftReference<>(key, value, refq)); if (oldRef == null) { diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/soft/SoftRefFileCache.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/soft/SoftRefFileCache.java index 378e5901..a0d550ff 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/soft/SoftRefFileCache.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/soft/SoftRefFileCache.java @@ -19,6 +19,19 @@ package org.apache.commons.jcs.yajcache.soft; * under the License. */ +import java.io.Serializable; +import java.lang.ref.ReferenceQueue; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.Lock; + import org.apache.commons.jcs.yajcache.beans.CacheChangeSupport; import org.apache.commons.jcs.yajcache.beans.ICacheChangeListener; import org.apache.commons.jcs.yajcache.config.PerCacheConfig; @@ -30,7 +43,10 @@ import org.apache.commons.jcs.yajcache.file.CacheFileContent; import org.apache.commons.jcs.yajcache.file.CacheFileContentType; import org.apache.commons.jcs.yajcache.file.CacheFileDAO; import org.apache.commons.jcs.yajcache.file.CacheFileUtils; -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.Implements; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; +import org.apache.commons.jcs.yajcache.lang.annotation.TODO; +import org.apache.commons.jcs.yajcache.lang.annotation.UnsupportedOperation; import org.apache.commons.jcs.yajcache.lang.ref.KeyedRefCollector; import org.apache.commons.jcs.yajcache.lang.ref.KeyedSoftReference; import org.apache.commons.jcs.yajcache.util.CollectionUtils; @@ -39,35 +55,18 @@ import org.apache.commons.jcs.yajcache.util.concurrent.locks.IKeyedReadWriteLock import org.apache.commons.jcs.yajcache.util.concurrent.locks.KeyedReadWriteLock; import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.io.Serializable; -import java.lang.ref.ReferenceQueue; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.locks.Lock; /** * Cache implemented using Soft References. */ -@CopyRightApache @TODO("Annotate the thread-safetyness of the methods") public class SoftRefFileCache<V> implements ICache<V> { private static final boolean debug = true; - private final Log log = debug ? LogFactory.getLog(this.getClass()) : null; private final @NonNullable ReferenceQueue<V> refq = new ReferenceQueue<>(); private final @NonNullable String name; private final @NonNullable Class<V> valueType; - private final @NonNullable ConcurrentMap<String,KeyedSoftReference<String,V>> map; + private final @NonNullable ConcurrentMap<String, KeyedSoftReference<String,V>> map; private PerCacheConfig config; private final @NonNullable KeyedRefCollector<String> collector; @@ -227,7 +226,7 @@ public class SoftRefFileCache<V> implements ICache<V> if (debug) { this.countGetHitFile.incrementAndGet(); } - val = (V)cfc.deserialize(); + val = cfc.deserialize(); if (val == null) { // Corrupted file. Try remove it from file system. @@ -341,7 +340,7 @@ public class SoftRefFileCache<V> implements ICache<V> if (debug) { this.countPutReadFile.incrementAndGet(); } - ret = (V)cfc.deserialize(); + ret = cfc.deserialize(); } if (!EqualsUtils.inst.equals(value, ret)) { // Considered new value being put to memory. @@ -431,7 +430,7 @@ public class SoftRefFileCache<V> implements ICache<V> } if (cfc != null) { // If corrupted, invoking deserialize will return null. - ret = (V)cfc.deserialize(); + ret = cfc.deserialize(); } } // Must exist the file system, corrupted or not. diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/BeanUtils.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/BeanUtils.java index 34a889c5..e956590a 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/BeanUtils.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/BeanUtils.java @@ -18,41 +18,41 @@ package org.apache.commons.jcs.yajcache.util; * specific language governing permissions and limitations * under the License. */ - -import org.apache.commons.jcs.yajcache.lang.annotation.*; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; +import org.apache.commons.jcs.yajcache.lang.annotation.TODO; +import org.apache.commons.jcs4.log.Log; + /** */ -// @CopyRightApache -// http://www.netbeans.org/issues/show_bug.cgi?id=53704 +// // http://www.netbeans.org/issues/show_bug.cgi?id=53704 public enum BeanUtils { inst; private static final boolean debug = false; - private final Log log = debug ? LogFactory.getLog(this.getClass()) : null; + private final Log log = debug ? Log.getLog(this.getClass()) : null; + @SuppressWarnings("unchecked") public <B> B cloneDeep(final B bean) { if (bean == null || ClassUtils.inst.isImmutable(bean)) { return bean; } - return (B)fromXmlByteArray(toXmlByteArray(bean)); + return (B) fromXmlByteArray(toXmlByteArray(bean)); } + @SuppressWarnings("unchecked") public <B> B cloneShallow(final B bean) { if (bean == null || ClassUtils.inst.isImmutable(bean)) { return bean; } try { - return (B)org.apache.commons.beanutils.BeanUtils.cloneBean(bean); + return (B) org.apache.commons.beanutils.BeanUtils.cloneBean(bean); } catch (final Exception ex) { - LogFactory.getLog(this.getClass()).error("", ex); + Log.getLog(this.getClass()).error("", ex); throw new RuntimeException(ex); } } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/ClassUtils.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/ClassUtils.java index 05e712bb..87fd11c2 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/ClassUtils.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/ClassUtils.java @@ -1,5 +1,8 @@ package org.apache.commons.jcs.yajcache.util; +import java.math.BigDecimal; +import java.math.BigInteger; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,22 +21,17 @@ package org.apache.commons.jcs.yajcache.util; * specific language governing permissions and limitations * under the License. */ - -import org.apache.commons.jcs.yajcache.lang.annotation.*; - -import java.math.BigDecimal; -import java.math.BigInteger; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; /** */ -// @CopyRightApache -// http://www.netbeans.org/issues/show_bug.cgi?id=53704 +// // http://www.netbeans.org/issues/show_bug.cgi?id=53704 public enum ClassUtils { inst; /** * Returns true if instances of the given class is known to be immutable; * false if we don't know. */ - public boolean isImmutable(@NonNullable final Class t) { + public boolean isImmutable(@NonNullable final Class<?> t) { return t == String.class || t.isPrimitive() || t == Boolean.class diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/CollectionUtils.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/CollectionUtils.java index dbad7545..9152a88f 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/CollectionUtils.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/CollectionUtils.java @@ -19,14 +19,11 @@ package org.apache.commons.jcs.yajcache.util; * under the License. */ -import org.apache.commons.jcs.yajcache.lang.annotation.*; - import java.util.concurrent.ConcurrentHashMap; /** * Enumerates collection related utilities. */ -@CopyRightApache public enum CollectionUtils { inst; diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/EqualsUtils.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/EqualsUtils.java index 155b14b0..68c9286e 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/EqualsUtils.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/EqualsUtils.java @@ -38,14 +38,14 @@ public enum EqualsUtils { if (lhs == null || rhs == null) { return false; } - final Class lClass = lhs.getClass(); - final Class rClass = rhs.getClass(); + final Class<?> lClass = lhs.getClass(); + final Class<?> rClass = rhs.getClass(); if (lClass.isArray() && rClass.isArray()) { - final Class lCompType = lClass.getComponentType(); - final Class rCompType = rClass.getComponentType(); + final Class<?> lCompType = lClass.getComponentType(); + final Class<?> rCompType = rClass.getComponentType(); if (lCompType.isPrimitive()) { if (rCompType.isPrimitive()) { diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/SerializeUtils.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/SerializeUtils.java index b4bca64b..6dcd5ed9 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/SerializeUtils.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/SerializeUtils.java @@ -1,5 +1,8 @@ package org.apache.commons.jcs.yajcache.util; +import java.io.Serializable; +import java.lang.reflect.Array; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,17 +21,12 @@ package org.apache.commons.jcs.yajcache.util; * specific language governing permissions and limitations * under the License. */ - -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; import org.apache.commons.lang3.SerializationUtils; -import java.io.Serializable; -import java.lang.reflect.Array; - /** */ -// @CopyRightApache -// http://www.netbeans.org/issues/show_bug.cgi?id=53704 +// // http://www.netbeans.org/issues/show_bug.cgi?id=53704 public enum SerializeUtils { inst; /** @@ -38,29 +36,30 @@ public enum SerializeUtils { * short-cutting the deep clone process if possible. */ public <V extends Serializable> V dup(final V obj) { - Class k = null; + Class<?> k = null; if (obj == null || ClassUtils.inst.isImmutable(k=obj.getClass())) { return obj; } - final Class t = k.getComponentType(); + final Class<?> t = k.getComponentType(); // an array. - if ((t != null) && ClassUtils.inst.isImmutable(t)) + if (t != null && ClassUtils.inst.isImmutable(t)) { // array elements are immutable. // short cut via shallow clone. return this.cloneArray(obj); } // deep clone. - return (V)SerializationUtils.clone(obj); + return (V) SerializationUtils.clone(obj); } private @NonNullable <A> A cloneArray(@NonNullable final A a) { final int len = Array.getLength(a); - final Object result = Array.newInstance(a.getClass().getComponentType(), len); + @SuppressWarnings("unchecked") + final A result = (A) Array.newInstance(a.getClass().getComponentType(), len); System.arraycopy(a, 0, result, 0, len); - return (A)result; + return result; } // public Class<?> getLeaveComponentType(Class<?> k) { // if (k == null) diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/concurrent/locks/IKeyedReadWriteLock.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/concurrent/locks/IKeyedReadWriteLock.java index 1d1e6592..515858ba 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/concurrent/locks/IKeyedReadWriteLock.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/concurrent/locks/IKeyedReadWriteLock.java @@ -19,13 +19,10 @@ package org.apache.commons.jcs.yajcache.util.concurrent.locks; * under the License. */ -import org.apache.commons.jcs.yajcache.lang.annotation.*; - import java.util.concurrent.locks.Lock; /** * Factory Interface for key specific ReadWriteLock. */ -@CopyRightApache public interface IKeyedReadWriteLock<K> { Lock readLock(K key); Lock writeLock(K key); diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/concurrent/locks/KeyedReadWriteLock.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/concurrent/locks/KeyedReadWriteLock.java index 0a495402..33868823 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/concurrent/locks/KeyedReadWriteLock.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/main/java/org/apache/commons/jcs/yajcache/util/concurrent/locks/KeyedReadWriteLock.java @@ -1,5 +1,13 @@ package org.apache.commons.jcs.yajcache.util.concurrent.locks; +import java.lang.ref.ReferenceQueue; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,25 +26,15 @@ package org.apache.commons.jcs.yajcache.util.concurrent.locks; * specific language governing permissions and limitations * under the License. */ - -import org.apache.commons.jcs.yajcache.lang.annotation.*; +import org.apache.commons.jcs.yajcache.lang.annotation.NonNullable; import org.apache.commons.jcs.yajcache.lang.ref.KeyedRefCollector; import org.apache.commons.jcs.yajcache.lang.ref.KeyedWeakReference; import org.apache.commons.lang3.builder.ToStringBuilder; -import java.lang.ref.ReferenceQueue; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; - /** * Factory for key specific ReadWriteLock. * Unused locks are automatically garbage collected. */ -@CopyRightApache public class KeyedReadWriteLock<K> implements IKeyedReadWriteLock<K> { private static final boolean debug = true; @@ -125,8 +123,8 @@ public class KeyedReadWriteLock<K> implements IKeyedReadWriteLock<K> { this.countRWLockCreate.incrementAndGet(); } try { - return rwlClass.newInstance(); - } catch (final IllegalAccessException | InstantiationException ex) { + return rwlClass.getDeclaredConstructor().newInstance(); + } catch (final IllegalArgumentException | ReflectiveOperationException ex) { throw new RuntimeException(ex); } } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/core/CacheManagerTest.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/core/CacheManagerTest.java index 01bb2eef..0f8ca158 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/core/CacheManagerTest.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/core/CacheManagerTest.java @@ -19,20 +19,25 @@ package org.apache.commons.jcs.yajcache.core; * under the License. */ -import junit.framework.TestCase; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; -import org.apache.commons.jcs.yajcache.lang.annotation.CopyRightApache; import org.apache.commons.jcs.yajcache.lang.annotation.TestOnly; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.apache.commons.jcs4.log.Log; +import org.junit.jupiter.api.Test; /** */ -@CopyRightApache @TestOnly -public class CacheManagerTest extends TestCase { - private final Log log = LogFactory.getLog(this.getClass()); +public class CacheManagerTest { + /** The logger. */ + private final Log log = Log.getLog(this.getClass()); + @Test public void testGetCache() { CacheManager.inst.getCache("myCache", String.class); CacheManager.inst.removeCache("myCache"); @@ -53,57 +58,51 @@ public class CacheManagerTest extends TestCase { c = CacheManager.inst.getCache("myCache", String.class); c.put("1", "First Put"); c.put("2", "Second Put"); - assertEquals(c.size(), 2); - assertTrue("Second Put" == c.get("2")); - assertTrue("First Put" == c.get("1")); + assertEquals(2, c.size()); + assertEquals("Second Put", c.get("2")); + assertEquals("First Put", c.get("1")); log.debug("Test getCache and clear"); c = CacheManager.inst.getCache("myCache", String.class); c.clear(); assertEquals(c.size(), 0); - assertTrue(null == c.get("2")); - assertTrue(null == c.get("1")); + assertNull(c.get("2")); + assertNull(c.get("1")); log.debug("Test getCache and getValueType"); - final ICache c1 = CacheManager.inst.getCache("myCache"); + final ICache<?> c1 = CacheManager.inst.getCache("myCache"); assertTrue(c1.getValueType() == String.class); + log.debug("Test checking of cache value type"); - try { - final ICache<Integer> c2 = CacheManager.inst.getCache("myCache", Integer.class); - fail("Expected ClassCastException"); - } catch (final ClassCastException ex) { - // should go here. - } + assertNull(CacheManager.inst.getCache("myCache", Integer.class), "Expected null"); log.debug(CacheManager.inst); } + @Test public void testGetCacheRaceCondition() { log.debug("Test simulation of race condition in creating cache"); - final ICache intCache = CacheManager.inst.testCreateCacheRaceCondition( + CacheManager.inst.removeCache("race"); + final ICache<?> intCache = CacheManager.inst.testCreateCacheRaceCondition( "race", Integer.class, CacheType.SOFT_REFERENCE); - final ICache intCache1 = CacheManager.inst.testCreateCacheRaceCondition( + final ICache<?> intCache1 = CacheManager.inst.testCreateCacheRaceCondition( "race", Integer.class, CacheType.SOFT_REFERENCE); log.debug("Test simulation of the worst case scenario: " + "race condition in creating cache AND class cast exception"); - try { - final ICache<Double> doubleCache = - CacheManager.inst.testCreateCacheRaceCondition( - "race", Double.class, CacheType.SOFT_REFERENCE); - fail("Bug: Cache for Integer cannot be used for Double"); - } catch (final ClassCastException ex) { - // should go here. - } - assertTrue(intCache == intCache1); + assertThrows(ClassCastException.class, () -> CacheManager.inst.testCreateCacheRaceCondition( + "race", Double.class, CacheType.SOFT_REFERENCE), + "Bug: Cache for Integer cannot be used for Double"); + assertSame(intCache, intCache1); log.debug(CacheManager.inst); } + @Test public void testRemoveCache() { log.debug("Test remove cache"); - final ICache<Integer> intCache = CacheManager.inst.getCache("race", Integer.class); + final ICache<Integer> intCache = CacheManager.inst.getCache("race", Integer.class, CacheType.SOFT_REFERENCE); intCache.put("1", 1); assertEquals(intCache.size(), 1); assertEquals(intCache, CacheManager.inst.removeCache("race")); assertEquals(intCache.size(), 0); - final ICache intCache1 = CacheManager.inst.getCache("race", Integer.class); - assertFalse(intCache == intCache1); + final ICache<?> intCache1 = CacheManager.inst.getCache("race", Integer.class); + assertNotSame(intCache, intCache1); CacheManager.inst.removeCache("race"); final ICache<Double> doubleCache = CacheManager.inst.testCreateCacheRaceCondition( "race", Double.class, CacheType.SOFT_REFERENCE); diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/core/SafeCacheManagerTest.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/core/SafeCacheManagerTest.java index 470a78a2..7ea438c4 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/core/SafeCacheManagerTest.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/core/SafeCacheManagerTest.java @@ -19,87 +19,91 @@ package org.apache.commons.jcs.yajcache.core; * under the License. */ -import junit.framework.TestCase; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; -import org.apache.commons.jcs.yajcache.lang.annotation.CopyRightApache; import org.apache.commons.jcs.yajcache.lang.annotation.TestOnly; import org.apache.commons.jcs.yajcache.util.TestSerializable; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.apache.commons.jcs4.log.Log; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; /** */ -@CopyRightApache @TestOnly -public class SafeCacheManagerTest extends TestCase { - private final Log log = LogFactory.getLog(this.getClass()); +public class SafeCacheManagerTest { + /** The logger. */ + private final Log log = Log.getLog(this.getClass()); + @AfterEach + public void tearDown() { + CacheManager.inst.dispose(); + } + + @Test public void testGetCache() { log.debug("Test getCache and get"); ICacheSafe<String> c = CacheManager.inst.getSafeCache( "myCache", String.class, CacheType.SOFT_REFERENCE_SAFE); - assertTrue(null == c.get("bla")); + assertNull(c.get("bla")); log.debug("Test getCache and put"); c = CacheManager.inst.getSafeCache("myCache", String.class); c.put("bla", "First Put"); - assertTrue("First Put" == c.get("bla")); + assertEquals("First Put", c.get("bla")); assertEquals(c.size(), 1); log.debug("Test getCache and remove"); c = CacheManager.inst.getSafeCache("myCache", String.class); c.remove("bla"); - assertTrue(null == c.get("bla")); + assertNull(c.get("bla")); log.debug("Test getCache and two put's"); c = CacheManager.inst.getSafeCache("myCache", String.class); c.put("1", "First Put"); c.put("2", "Second Put"); assertEquals(c.size(), 2); - assertTrue("Second Put" == c.get("2")); - assertTrue("First Put" == c.get("1")); + assertEquals("Second Put", c.get("2")); + assertEquals("First Put", c.get("1")); log.debug("Test getCache and clear"); c = CacheManager.inst.getSafeCache("myCache", String.class); c.clear(); - assertEquals(c.size(), 0); - assertTrue(null == c.get("2")); - assertTrue(null == c.get("1")); + assertEquals(0, c.size()); + assertNull(c.get("2")); + assertNull(c.get("1")); log.debug("Test getCache and getValueType"); - final ICacheSafe c1 = CacheManager.inst.getSafeCache("myCache"); - assertTrue(c1.getValueType() == String.class); + final ICacheSafe<?> c1 = CacheManager.inst.getSafeCache("myCache"); + assertEquals(String.class, c1.getValueType()); log.debug("Test checking of cache value type"); - try { - final ICacheSafe<Integer> c2 = CacheManager.inst.getSafeCache("myCache", Integer.class); - fail("Expected ClassCastException"); - } catch (final ClassCastException ex) { - // should go here. - } + assertNull(CacheManager.inst.getSafeCache("myCache", Integer.class)); log.debug(CacheManager.inst); } + @Test public void testGetCacheRaceCondition() { log.debug("Test simulation of race condition in creating cache"); - final ICache intCache = CacheManager.inst.testCreateCacheRaceCondition( + final ICache<?> intCache = CacheManager.inst.testCreateCacheRaceCondition( "race", Integer.class, CacheType.SOFT_REFERENCE_SAFE); - final ICache intCache1 = CacheManager.inst.testCreateCacheRaceCondition( + final ICache<?> intCache1 = CacheManager.inst.testCreateCacheRaceCondition( "race", Integer.class, CacheType.SOFT_REFERENCE_SAFE); log.debug("Test simulation of the worst case scenario: " + "race condition in creating cache AND class cast exception"); - try { - final ICache doubleCache = CacheManager.inst.testCreateCacheRaceCondition( - "race", Double.class, CacheType.SOFT_REFERENCE_SAFE); - fail("Expected ClassCastException"); - } catch (final ClassCastException ex) { - // should go here. - } - assertTrue(intCache == intCache1); + assertThrows(ClassCastException.class, () -> CacheManager.inst.testCreateCacheRaceCondition( + "race", Double.class, CacheType.SOFT_REFERENCE_SAFE), + "Expected ClassCastException"); + assertSame(intCache, intCache1); } + @Test public void testRemoveCache() { log.debug("Test remove cache"); - final ICacheSafe<Integer> intCache = CacheManager.inst.getSafeCache("race", Integer.class); + final ICacheSafe<Integer> intCache = CacheManager.inst.getSafeCache("race", Integer.class, CacheType.SOFT_REFERENCE_SAFE); intCache.put("1", 1); assertEquals(intCache.size(), 1); assertEquals(intCache, CacheManager.inst.removeCache("race")); assertEquals(intCache.size(), 0); - final ICacheSafe intCache1 = CacheManager.inst.getSafeCache("race", Integer.class); + final ICacheSafe<?> intCache1 = CacheManager.inst.getSafeCache("race", Integer.class); assertFalse(intCache == intCache1); CacheManager.inst.removeCache("race"); final ICache<Double> doubleCache = @@ -110,6 +114,7 @@ public class SafeCacheManagerTest extends TestCase { log.debug(CacheManager.inst); } + @Test public void testGetSafeCache() { log.debug("Test getCache and getCopy"); { @@ -169,15 +174,10 @@ public class SafeCacheManagerTest extends TestCase { assertTrue(null == c.getCopy("2")); assertTrue(null == c.getCopy("1")); log.debug("Test getCache and getValueType"); - final ICacheSafe c1 = CacheManager.inst.getSafeCache("myCache"); + final ICacheSafe<?> c1 = CacheManager.inst.getSafeCache("myCache"); assertTrue(c1.getValueType() == TestSerializable.class); log.debug("Test checking of cache value type"); - try { - final ICacheSafe<Integer> c2 = CacheManager.inst.getSafeCache("myCache", Integer.class); - fail("Bug: Cache for string cannot be used for Integer."); - } catch (final ClassCastException ex) { - // should go here. - } + assertNull(CacheManager.inst.getSafeCache("myCache", Integer.class)); log.debug(CacheManager.inst); } } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/file/CacheFileDAOTest.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/file/CacheFileDAOTest.java index 38717e09..2fb905b5 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/file/CacheFileDAOTest.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/file/CacheFileDAOTest.java @@ -19,24 +19,26 @@ package org.apache.commons.jcs.yajcache.file; * under the License. */ +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.io.File; import java.io.RandomAccessFile; import java.util.Arrays; -import junit.framework.TestCase; - -import org.apache.commons.jcs.yajcache.lang.annotation.CopyRightApache; import org.apache.commons.jcs.yajcache.lang.annotation.TestOnly; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.apache.commons.jcs4.log.Log; +import org.junit.jupiter.api.Test; /** */ -@CopyRightApache @TestOnly -public class CacheFileDAOTest extends TestCase { - private final Log log = LogFactory.getLog(this.getClass()); +public class CacheFileDAOTest { + /** The logger. */ + private final Log log = Log.getLog(this.getClass()); + @Test public void test() { log.debug("testing cache directory " + CacheFileUtils.inst.getCacheDir("testCache").getAbsolutePath()); @@ -56,7 +58,7 @@ public class CacheFileDAOTest extends TestCase { log.debug("test readCacheItem"); final byte[] ba1r = CacheFileDAO.inst.readCacheItem("testCache", "key1").getContent(); assertTrue(Arrays.equals(ba1, ba1r)); - final byte[] ba2r = (byte[]) CacheFileDAO.inst.readCacheItem("testCache", "key2").getContent(); + final byte[] ba2r = CacheFileDAO.inst.readCacheItem("testCache", "key2").getContent(); assertTrue(Arrays.equals(ba2, ba2r)); log.debug("test removeCacheItem"); @@ -64,6 +66,8 @@ public class CacheFileDAOTest extends TestCase { assertTrue(CacheFileDAO.inst.removeCacheItem("testCache", "key2")); assertFalse(CacheFileDAO.inst.removeCacheItem("testCache", "key3")); } + + @Test public void testCorruptedFile() throws Exception { log.debug("create testCacheCorrupt Cache directory"); CacheFileUtils.inst.mkCacheDirs("testCacheCorrupt"); @@ -84,7 +88,7 @@ public class CacheFileDAOTest extends TestCase { cfc = CacheFileDAO.inst.readCacheItem("testCacheCorrupt", "keyy"); byte[] ba2i = cfc == null ? null : cfc.getContent(); - assertTrue(ba2i == null); + assertNull(ba2i); log.debug("test readCacheItem with corrupted length"); file.delete(); @@ -96,7 +100,7 @@ public class CacheFileDAOTest extends TestCase { cfc = CacheFileDAO.inst.readCacheItem("testCacheCorrupt", "keyy"); ba2i = cfc == null ? null : cfc.getContent(); - assertTrue(ba2i == null); + assertNull(ba2i); log.debug("test readCacheItem with corrupted content"); file.delete(); @@ -108,7 +112,7 @@ public class CacheFileDAOTest extends TestCase { cfc = CacheFileDAO.inst.readCacheItem("testCacheCorrupt", "keyy"); ba2i = cfc == null ? null : cfc.getContent(); - assertTrue(ba2i == null); + assertNull(ba2i); log.debug("test readCacheItem with appended content"); file.delete(); @@ -120,7 +124,7 @@ public class CacheFileDAOTest extends TestCase { cfc = CacheFileDAO.inst.readCacheItem("testCacheCorrupt", "keyy"); ba2i = cfc == null ? null : cfc.getContent(); - assertTrue(ba2i == null); + assertNull(ba2i); log.debug("test readCacheItem with content less than min length"); file.delete(); @@ -132,9 +136,6 @@ public class CacheFileDAOTest extends TestCase { cfc = CacheFileDAO.inst.readCacheItem("testCacheCorrupt", "keyy"); ba2i = cfc == null ? null : cfc.getContent(); - assertTrue(ba2i == null); - - log.debug(CacheFileDAO.inst.toString()); - + assertNull(ba2i); } } diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/file/FileContentTypeTest.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/file/FileContentTypeTest.java index 1fab791f..e103b216 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/file/FileContentTypeTest.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/file/FileContentTypeTest.java @@ -19,22 +19,25 @@ package org.apache.commons.jcs.yajcache.file; * under the License. */ -import junit.framework.TestCase; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; -import org.apache.commons.jcs.yajcache.lang.annotation.CopyRightApache; import org.apache.commons.jcs.yajcache.lang.annotation.TestOnly; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.apache.commons.jcs4.log.Log; +import org.junit.jupiter.api.Test; /** */ -@CopyRightApache @TestOnly -public class FileContentTypeTest extends TestCase { - private final Log log = LogFactory.getLog(this.getClass()); +public class FileContentTypeTest { + /** The logger. */ + private final Log log = Log.getLog(this.getClass()); + /** * Test of toByte method, of class org.apache.commons.jcs.yajcache.config.FileContentType. */ + @Test public void test() { log.debug("test toByte"); final Byte bJavaSerialization = CacheFileContentType.JAVA_SERIALIZATION.toByte(); diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/lang/annotation/CopyRightApacheTest.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/lang/annotation/CopyRightApacheTest.java deleted file mode 100644 index 8b7dcd3c..00000000 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/lang/annotation/CopyRightApacheTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.apache.commons.jcs.yajcache.lang.annotation; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import junit.framework.TestCase; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - */ -@CopyRightApache -@TestOnly -public class CopyRightApacheTest extends TestCase { - private final Log log = LogFactory.getLog(this.getClass()); - - public void test() { - log.debug(this.getClass().getAnnotation(CopyRightApache.class)); - } -} diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/soft/SoftRefCacheTest.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/soft/SoftRefCacheTest.java index 9c0a618e..e02ac15f 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/soft/SoftRefCacheTest.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/soft/SoftRefCacheTest.java @@ -19,23 +19,21 @@ package org.apache.commons.jcs.yajcache.soft; * under the License. */ -import junit.framework.TestCase; - import org.apache.commons.jcs.yajcache.core.CacheManager; import org.apache.commons.jcs.yajcache.core.CacheType; import org.apache.commons.jcs.yajcache.core.ICache; -import org.apache.commons.jcs.yajcache.lang.annotation.CopyRightApache; import org.apache.commons.jcs.yajcache.lang.annotation.TestOnly; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.apache.commons.jcs4.log.Log; +import org.junit.jupiter.api.Test; /** */ -@CopyRightApache @TestOnly -public class SoftRefCacheTest extends TestCase { - private final Log log = LogFactory.getLog(this.getClass()); +public class SoftRefCacheTest { + /** The logger. */ + private final Log log = Log.getLog(this.getClass()); + @Test public void testSoftRefCache() throws Exception { final ICache<byte[]> c = CacheManager.inst.getCache( "bytesCache", byte[].class, CacheType.SOFT_REFERENCE); diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/soft/SoftRefFileCacheSafeTest.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/soft/SoftRefFileCacheSafeTest.java index c82fa375..e74e8c55 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/soft/SoftRefFileCacheSafeTest.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/soft/SoftRefFileCacheSafeTest.java @@ -19,23 +19,20 @@ package org.apache.commons.jcs.yajcache.soft; * under the License. */ -import junit.framework.TestCase; - import org.apache.commons.jcs.yajcache.core.CacheManager; import org.apache.commons.jcs.yajcache.core.CacheType; import org.apache.commons.jcs.yajcache.core.ICacheSafe; -import org.apache.commons.jcs.yajcache.lang.annotation.CopyRightApache; import org.apache.commons.jcs.yajcache.lang.annotation.TestOnly; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.apache.commons.jcs4.log.Log; +import org.junit.jupiter.api.Test; /** */ -@CopyRightApache @TestOnly -public class SoftRefFileCacheSafeTest extends TestCase { - private final Log log = LogFactory.getLog(this.getClass()); +public class SoftRefFileCacheSafeTest { + private final Log log = Log.getLog(this.getClass()); + @Test public void testSoftRefCache() throws Exception { final ICacheSafe<byte[]> c = CacheManager.inst.getSafeCache( "bytesCache", byte[].class, CacheType.SOFT_REFERENCE_FILE_SAFE); diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/util/SerializeUtilsTest.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/util/SerializeUtilsTest.java index 885df3d9..dab09cc1 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/util/SerializeUtilsTest.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/util/SerializeUtilsTest.java @@ -19,21 +19,22 @@ package org.apache.commons.jcs.yajcache.util; * under the License. */ -import java.util.Arrays; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -import junit.framework.TestCase; +import java.util.Arrays; -import org.apache.commons.jcs.yajcache.lang.annotation.CopyRightApache; import org.apache.commons.jcs.yajcache.lang.annotation.TestOnly; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.apache.commons.jcs4.log.Log; /** */ -@CopyRightApache @TestOnly -public class SerializeUtilsTest extends TestCase { - private final Log log = LogFactory.getLog(this.getClass()); +public class SerializeUtilsTest { + /** The logger. */ + private final Log log = Log.getLog(this.getClass()); + /** * Test of dup method, of class org.apache.commons.jcs.yajcache.util.SerializeUtils. */ diff --git a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/util/TestSerializable.java b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/util/TestSerializable.java index 809c187c..e40f0de8 100644 --- a/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/util/TestSerializable.java +++ b/commons-jcs4-sandbox/commons-jcs4-yajcache/src/test/java/org/apache/commons/jcs/yajcache/util/TestSerializable.java @@ -21,13 +21,11 @@ package org.apache.commons.jcs.yajcache.util; import java.io.Serializable; -import org.apache.commons.jcs.yajcache.lang.annotation.CopyRightApache; import org.apache.commons.jcs.yajcache.lang.annotation.TestOnly; import org.apache.commons.lang3.builder.EqualsBuilder; /** */ -@CopyRightApache @TestOnly public class TestSerializable implements Serializable { private static final long serialVersionUID = 1L;
