This is an automated email from the ASF dual-hosted git repository.

tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git

commit 2ebe98bd8310eae2e7e434e444ed47a7249ca025
Author: Thomas Vandahl <[email protected]>
AuthorDate: Wed Mar 25 21:59:13 2026 +0100

    Use Instant and Duration like anywhere else
---
 .../org/apache/commons/jcs4/jcache/JCSCache.java   | 27 +++++-------
 .../commons/jcs4/jcache/JCSCachingManager.java     |  4 +-
 .../apache/commons/jcs4/jcache/JCSListener.java    | 19 +++-----
 .../org/apache/commons/jcs4/jcache/Statistics.java | 51 ++++++++++++----------
 .../java/org/apache/commons/jcs4/jcache/Times.java | 37 ----------------
 .../jcs4/jcache/jmx/JCSCacheStatisticsMXBean.java  |  8 ++--
 6 files changed, 56 insertions(+), 90 deletions(-)

diff --git 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCache.java
 
b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCache.java
index 82e1efb5..1ad6c9fd 100644
--- 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCache.java
+++ 
b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCache.java
@@ -23,6 +23,7 @@ import static 
org.apache.commons.jcs4.jcache.serialization.Serializations.copy;
 
 import java.io.Closeable;
 import java.io.IOException;
+import java.time.Instant;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -291,7 +292,7 @@ public class JCSCache<K, V> implements Cache<K, V>
         JMXs.unregister(cacheStatsObjectName);
     }
 
-    private V doGetControllingExpiry(final long getStart, final K key, final 
boolean updateAcess, final boolean forceDoLoad, final boolean skipLoad,
+    private V doGetControllingExpiry(final Instant getStart, final K key, 
final boolean updateAcess, final boolean forceDoLoad, final boolean skipLoad,
             final boolean propagateLoadException)
     {
         final boolean statisticsEnabled = config.isStatisticsEnabled();
@@ -337,7 +338,7 @@ public class JCSCache<K, V> implements Cache<K, V>
         }
         if (statisticsEnabled && v != null)
         {
-            statistics.addGetTime(Times.now(false) - getStart);
+            statistics.addGetTime(java.time.Duration.between(getStart, 
Instant.now()));
         }
         return v;
     }
@@ -407,7 +408,7 @@ public class JCSCache<K, V> implements Cache<K, V>
     {
         try
         {
-            final long now = Times.now(false);
+            final Instant now = Instant.now();
             for (final K k : keys)
             {
                 if (replaceExistingValues)
@@ -465,8 +466,7 @@ public class JCSCache<K, V> implements Cache<K, V>
     {
         assertNotClosed();
         assertNotNull(key, "key");
-        final long getStart = Times.now(false);
-        return doGetControllingExpiry(getStart, key, true, false, false, true);
+        return doGetControllingExpiry(Instant.now(), key, true, false, false, 
true);
     }
 
     @Override
@@ -516,8 +516,7 @@ public class JCSCache<K, V> implements Cache<K, V>
         assertNotClosed();
         assertNotNull(key, "key");
         assertNotNull(value, "value");
-        final long getStart = Times.now(false);
-        final V v = doGetControllingExpiry(getStart, key, false, false, true, 
false);
+        final V v = doGetControllingExpiry(Instant.now(), key, false, false, 
true, false);
         put(key, value);
         return v;
     }
@@ -527,8 +526,7 @@ public class JCSCache<K, V> implements Cache<K, V>
     {
         assertNotClosed();
         assertNotNull(key, "key");
-        final long getStart = Times.now(false);
-        final V v = doGetControllingExpiry(getStart, key, false, false, true, 
false);
+        final V v = doGetControllingExpiry(Instant.now(), key, false, false, 
true, false);
         remove(key);
         return v;
     }
@@ -694,7 +692,7 @@ public class JCSCache<K, V> implements Cache<K, V>
         if (isNotZero(duration))
         {
             final boolean statisticsEnabled = config.isStatisticsEnabled();
-            final long start = Times.now(false);
+            final Instant start = Instant.now();
 
             final K jcsKey = storeByValue ? copy(serializer, 
manager.getClassLoader(), key) : key;
             IElementAttributes attributes = oldElt != null ? 
oldElt.elementAttributes() : delegate.getElementAttributes();
@@ -751,7 +749,7 @@ public class JCSCache<K, V> implements Cache<K, V>
             if (statisticsEnabled)
             {
                 statistics.increasePuts(1);
-                statistics.addPutTime(Times.now(false) - start);
+                statistics.addPutTime(java.time.Duration.between(start, 
Instant.now()));
             }
         } else if (!created)
         {
@@ -801,7 +799,7 @@ public class JCSCache<K, V> implements Cache<K, V>
         assertNotNull(key, "key");
 
         final boolean statisticsEnabled = config.isStatisticsEnabled();
-        final long start = Times.now(!statisticsEnabled);
+        final Instant start = Instant.now();
 
         writer.delete(key);
 
@@ -818,7 +816,7 @@ public class JCSCache<K, V> implements Cache<K, V>
         if (remove && statisticsEnabled)
         {
             statistics.increaseRemovals(1);
-            statistics.addRemoveTime(Times.now(false) - start);
+            statistics.addRemoveTime(java.time.Duration.between(start, 
Instant.now()));
         }
         return remove;
     }
@@ -829,8 +827,7 @@ public class JCSCache<K, V> implements Cache<K, V>
         assertNotClosed();
         assertNotNull(key, "key");
         assertNotNull(oldValue, "oldValue");
-        final long getStart = Times.now(false);
-        final V v = doGetControllingExpiry(getStart, key, false, false, false, 
false);
+        final V v = doGetControllingExpiry(Instant.now(), key, false, false, 
false, false);
         if (oldValue.equals(v))
         {
             remove(key);
diff --git 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCachingManager.java
 
b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCachingManager.java
index 4db7e76c..4413ca89 100644
--- 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCachingManager.java
+++ 
b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCachingManager.java
@@ -302,7 +302,9 @@ public class JCSCachingManager implements CacheManager
     {
         assertNotClosed();
         assertNotNull(cacheName, "cacheName");
-        return (Cache<K, V>) doGetCache(cacheName, Object.class, Object.class);
+        @SuppressWarnings("unchecked")  // common map for all caches
+        Cache<K, V> cache = (Cache<K, V>) doGetCache(cacheName, Object.class, 
Object.class);
+        return cache;
     }
 
     @Override
diff --git 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSListener.java
 
b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSListener.java
index a76e0cdc..752ed9d3 100644
--- 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSListener.java
+++ 
b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSListener.java
@@ -19,7 +19,6 @@
 package org.apache.commons.jcs4.jcache;
 
 import java.io.Closeable;
-import java.util.ArrayList;
 import java.util.List;
 
 import javax.cache.configuration.CacheEntryListenerConfiguration;
@@ -70,7 +69,8 @@ public class JCSListener<K, V> implements Closeable
     @Override
     public void close()
     {
-        if (Closeable.class.isInstance(delegate)) {
+        if (Closeable.class.isInstance(delegate))
+        {
             Closeable.class.cast(delegate);
         }
     }
@@ -82,18 +82,10 @@ public class JCSListener<K, V> implements Closeable
             return events;
         }
 
-        final List<CacheEntryEvent<? extends K, ? extends V>> filtered = new 
ArrayList<>(
-                events.size());
-        for (final CacheEntryEvent<? extends K, ? extends V> event : events)
-        {
-            if (filter.evaluate(event))
-            {
-                filtered.add(event);
-            }
-        }
-        return filtered;
+        return events.stream().filter(filter::evaluate).toList();
     }
 
+    @SuppressWarnings("unchecked")
     public void onCreated(final List<CacheEntryEvent<? extends K, ? extends 
V>> events) throws CacheEntryListenerException
     {
         if (create)
@@ -102,6 +94,7 @@ public class JCSListener<K, V> implements Closeable
         }
     }
 
+    @SuppressWarnings("unchecked")
     public void onExpired(final List<CacheEntryEvent<? extends K, ? extends 
V>> events) throws CacheEntryListenerException
     {
         if (expire)
@@ -110,6 +103,7 @@ public class JCSListener<K, V> implements Closeable
         }
     }
 
+    @SuppressWarnings("unchecked")
     public void onRemoved(final List<CacheEntryEvent<? extends K, ? extends 
V>> events) throws CacheEntryListenerException
     {
         if (remove)
@@ -118,6 +112,7 @@ public class JCSListener<K, V> implements Closeable
         }
     }
 
+    @SuppressWarnings("unchecked")
     public void onUpdated(final List<CacheEntryEvent<? extends K, ? extends 
V>> events) throws CacheEntryListenerException
     {
         if (update)
diff --git 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/Statistics.java
 
b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/Statistics.java
index 5863f364..2f361614 100644
--- 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/Statistics.java
+++ 
b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/Statistics.java
@@ -18,10 +18,15 @@
  */
 package org.apache.commons.jcs4.jcache;
 
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
 
 public class Statistics
 {
+    private static final Duration MAX_DURATION = 
ChronoUnit.FOREVER.getDuration();
+
     private volatile boolean active = true;
 
     private final AtomicLong removals = new AtomicLong();
@@ -30,21 +35,21 @@ public class Statistics
     private final AtomicLong hits = new AtomicLong();
     private final AtomicLong misses = new AtomicLong();
     private final AtomicLong evictions = new AtomicLong();
-    private final AtomicLong putTimeTaken = new AtomicLong();
-    private final AtomicLong getTimeTaken = new AtomicLong();
-    private final AtomicLong removeTimeTaken = new AtomicLong();
+    private final AtomicReference<Duration> putTimeTaken = new 
AtomicReference<Duration>(Duration.ZERO);
+    private final AtomicReference<Duration> getTimeTaken = new 
AtomicReference<Duration>(Duration.ZERO);
+    private final AtomicReference<Duration> removeTimeTaken = new 
AtomicReference<Duration>(Duration.ZERO);
 
-    public void addGetTime(final long duration)
+    public void addGetTime(final Duration duration)
     {
         increment(duration, getTimeTaken);
     }
 
-    public void addPutTime(final long duration)
+    public void addPutTime(final Duration duration)
     {
         increment(duration, putTimeTaken);
     }
 
-    public void addRemoveTime(final long duration)
+    public void addRemoveTime(final Duration duration)
     {
         increment(duration, removeTimeTaken);
     }
@@ -74,17 +79,17 @@ public class Statistics
         return removals.get();
     }
 
-    public long getTimeTakenForGets()
+    public Duration getTimeTakenForGets()
     {
         return getTimeTaken.get();
     }
 
-    public long getTimeTakenForPuts()
+    public Duration getTimeTakenForPuts()
     {
         return putTimeTaken.get();
     }
 
-    public long getTimeTakenForRemovals()
+    public Duration getTimeTakenForRemovals()
     {
         return removeTimeTaken.get();
     }
@@ -128,22 +133,24 @@ public class Statistics
         counter.addAndGet(number);
     }
 
-    private void increment(final long duration, final AtomicLong counter)
+    private void increment(final Duration duration, final 
AtomicReference<Duration> counter)
     {
         if (!active)
         {
             return;
         }
 
-        if (counter.get() < Long.MAX_VALUE - duration)
-        {
-            counter.addAndGet(duration);
-        }
-        else
-        {
-            reset();
-            counter.set(duration);
-        }
+        counter.accumulateAndGet(duration, (u, v) -> {
+            if (u.compareTo(MAX_DURATION.minus(v)) < 0)
+            {
+                return u.plus(v);
+            }
+            else
+            {
+                reset();
+                return v;
+            }
+        });
     }
 
     public void reset()
@@ -154,9 +161,9 @@ public class Statistics
         expiries.set(0);
         hits.set(0);
         evictions.set(0);
-        getTimeTaken.set(0);
-        putTimeTaken.set(0);
-        removeTimeTaken.set(0);
+        getTimeTaken.set(Duration.ZERO);
+        putTimeTaken.set(Duration.ZERO);
+        removeTimeTaken.set(Duration.ZERO);
     }
 
     public void setActive(final boolean active)
diff --git 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/Times.java 
b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/Times.java
deleted file mode 100644
index 01b622d3..00000000
--- 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/Times.java
+++ /dev/null
@@ -1,37 +0,0 @@
-package org.apache.commons.jcs4.jcache;
-
-/*
- * 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.
- */
-
-public class Times
-{
-    public static long now(final boolean ignore)
-    {
-        if (ignore)
-        {
-            return -1;
-        }
-        return System.nanoTime() / 1000;
-    }
-
-    private Times()
-    {
-        // no-op
-    }
-}
diff --git 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/jmx/JCSCacheStatisticsMXBean.java
 
b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/jmx/JCSCacheStatisticsMXBean.java
index c612c86d..d9045aa1 100644
--- 
a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/jmx/JCSCacheStatisticsMXBean.java
+++ 
b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/jmx/JCSCacheStatisticsMXBean.java
@@ -18,6 +18,8 @@
  */
 package org.apache.commons.jcs4.jcache.jmx;
 
+import java.time.Duration;
+
 import javax.cache.management.CacheStatisticsMXBean;
 
 import org.apache.commons.jcs4.jcache.Statistics;
@@ -31,14 +33,14 @@ public class JCSCacheStatisticsMXBean implements 
CacheStatisticsMXBean
         this.statistics = stats;
     }
 
-    private float averageTime(final long timeTaken)
+    private float averageTime(final Duration timeTaken)
     {
         final long gets = getCacheGets();
-        if (timeTaken == 0 || gets == 0)
+        if (timeTaken.isZero() || gets == 0)
         {
             return 0;
         }
-        return timeTaken / gets;
+        return timeTaken.toMillis() / (float) gets;
     }
 
     @Override

Reply via email to