You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tv...@apache.org on 2021/11/03 10:20:46 UTC

[commons-jcs] branch master updated: Simplify conditions avoiding unnecessary validation.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1b0de35  Simplify conditions avoiding unnecessary validation.
     new 7f9ba9a  Merge pull request #71 from arturobernalg/feature/simplify_conditions
1b0de35 is described below

commit 1b0de3592e5df194315813a2d9eef154200c7e5c
Author: Arturo Bernal <ar...@gmail.com>
AuthorDate: Wed Jun 9 15:20:09 2021 +0200

    Simplify conditions avoiding unnecessary validation.
---
 .../auxiliary/disk/block/BlockDiskKeyStore.java    |  2 +-
 .../auxiliary/disk/indexed/IndexedDiskCache.java   |  5 --
 .../disk/jdbc/mysql/MySQLDiskCacheFactory.java     |  8 +--
 .../remote/CommonRemoteCacheAttributes.java        |  6 +-
 .../remote/RemoteCacheFailoverRunner.java          |  6 +-
 .../commons/jcs3/auxiliary/remote/RemoteUtils.java |  2 +-
 .../remote/server/RemoteCacheStartupServlet.java   |  9 +--
 .../apache/commons/jcs3/engine/CacheListeners.java | 22 ++-----
 .../engine/control/CompositeCacheConfigurator.java |  2 +-
 .../apache/commons/jcs3/log/MessageFormatter.java  |  2 +-
 .../commons/jcs3/utils/config/OptionConverter.java | 17 ++---
 .../commons/jcs3/utils/net/HostNameUtil.java       | 75 ++++++++++------------
 .../commons/jcs3/jcache/JCSCachingManager.java     |  4 +-
 13 files changed, 63 insertions(+), 97 deletions(-)

diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskKeyStore.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskKeyStore.java
index cc9928c..5379835 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskKeyStore.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/block/BlockDiskKeyStore.java
@@ -596,7 +596,7 @@ public class BlockDiskKeyStore<K>
         }
         boolean ok = true;
         if (!log.isTraceEnabled()) {
-            return ok;
+            return true;
         }
         for (final Entry<Integer, Set<K>> e : blockAllocationMap.entrySet())
         {
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/indexed/IndexedDiskCache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/indexed/IndexedDiskCache.java
index c93928e..c0b77d1 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/indexed/IndexedDiskCache.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/indexed/IndexedDiskCache.java
@@ -733,11 +733,6 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V>
             storageLock.writeLock().unlock();
         }
 
-        if (reset)
-        {
-            reset();
-        }
-
         // this increments the remove count.
         // there is no reason to call this if an item was not removed.
         if (removed)
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/MySQLDiskCacheFactory.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/MySQLDiskCacheFactory.java
index c778d2b..2bbcde3 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/MySQLDiskCacheFactory.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/mysql/MySQLDiskCacheFactory.java
@@ -101,12 +101,10 @@ public class MySQLDiskCacheFactory
                 // loop through the dates.
                 try
                 {
+                    // canĀ“t be null, otherwise ScheduleParser.createDatesForSchedule will throw ParseException
                     final Date[] dates = ScheduleParser.createDatesForSchedule( attributes.getOptimizationSchedule() );
-                    if ( dates != null )
-                    {
-                        for (final Date date : dates) {
-                            this.scheduleOptimization( date, optimizer );
-                        }
+                    for (final Date date : dates) {
+                        this.scheduleOptimization( date, optimizer );
                     }
                 }
                 catch ( final ParseException e )
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/CommonRemoteCacheAttributes.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/CommonRemoteCacheAttributes.java
index f85bf71..c81a1ec 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/CommonRemoteCacheAttributes.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/CommonRemoteCacheAttributes.java
@@ -82,11 +82,7 @@ public class CommonRemoteCacheAttributes
     @Override
     public void setRemoteTypeName( final String s )
     {
-        final RemoteType rt = RemoteType.valueOf(s);
-        if (rt != null)
-        {
-            this.remoteType = rt;
-        }
+        this.remoteType = RemoteType.valueOf(s);
     }
 
     /**
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheFailoverRunner.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheFailoverRunner.java
index a88b6a5..38c5f86 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheFailoverRunner.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheFailoverRunner.java
@@ -118,8 +118,7 @@ public class RemoteCacheFailoverRunner<K, V> extends AbstractAuxiliaryCacheMonit
             {
                 log.info( "Failover index is <= 0, meaning we are not connected to a failover server." );
             }
-            else if ( failoverIndex > 0 )
-            {
+            else {
                 log.info( "Failover index is > 0, meaning we are connected to a failover server." );
             }
             // log if we are allright or not.
@@ -339,8 +338,7 @@ public class RemoteCacheFailoverRunner<K, V> extends AbstractAuxiliaryCacheMonit
                                 return true;
                             }
                         }
-                        else if ( fidx < 0 )
-                        {
+                        else {
                             // this should never happen
                             log.warn( "Failover index is less than 0, this shouldn't happen" );
                         }
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteUtils.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteUtils.java
index 4125967..daea183 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteUtils.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteUtils.java
@@ -121,7 +121,7 @@ public class RemoteUtils
         InputStream is = RemoteUtils.class.getResourceAsStream(propFile);
 
         // Try root of class path
-        if ((null == is) && (propFile != null && !propFile.startsWith("/")))
+        if (null == is && !propFile.startsWith("/"))
         {
             is = RemoteUtils.class.getResourceAsStream("/" + propFile);
         }
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheStartupServlet.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheStartupServlet.java
index 21d44bf..b45fe4f 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheStartupServlet.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheStartupServlet.java
@@ -232,12 +232,9 @@ public class RemoteCacheStartupServlet
         try
         {
             props = RemoteUtils.loadProps(propsFileName);
-            if (props != null)
-            {
-                registryHost = props.getProperty("registry.host", registryHost);
-                final String portS = props.getProperty("registry.port", String.valueOf(registryPort));
-                setRegistryPort(portS);
-            }
+            registryHost = props.getProperty("registry.host", registryHost);
+            final String portS = props.getProperty("registry.port", String.valueOf(registryPort));
+            setRegistryPort(portS);
         }
         catch (final IOException e)
         {
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/CacheListeners.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/CacheListeners.java
index 2b7563d..789f279 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/CacheListeners.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/CacheListeners.java
@@ -58,22 +58,12 @@ public class CacheListeners<K, V>
     {
         final StringBuilder buffer = new StringBuilder();
         buffer.append( "\n CacheListeners" );
-        if ( cache != null )
-        {
-            buffer.append( "\n Region = " + cache.getCacheName() );
-        }
-        if ( eventQMap != null )
-        {
-            buffer.append( "\n Event Queue Map " );
-            buffer.append( "\n size = " + eventQMap.size() );
-            eventQMap.forEach((key, value)
-                    -> buffer.append( "\n Entry: key: ").append(key)
-                        .append(", value: ").append(value));
-        }
-        else
-        {
-            buffer.append( "\n No Listeners. " );
-        }
+        buffer.append( "\n Region = " + cache.getCacheName() );
+        buffer.append( "\n Event Queue Map " );
+        buffer.append( "\n size = " + eventQMap.size() );
+        eventQMap.forEach((key, value)
+                -> buffer.append( "\n Entry: key: ").append(key)
+                    .append(", value: ").append(value));
         return buffer.toString();
     }
 }
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheConfigurator.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheConfigurator.java
index 7cc7169..167e858 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheConfigurator.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCacheConfigurator.java
@@ -243,7 +243,7 @@ public class CompositeCacheConfigurator
             while ( st.hasMoreTokens() )
             {
                 auxName = st.nextToken().trim();
-                if ( auxName == null || auxName.equals( "," ) )
+                if (auxName.equals( "," ))
                 {
                     continue;
                 }
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/log/MessageFormatter.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/log/MessageFormatter.java
index 8c93f05..2257f5d 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/log/MessageFormatter.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/log/MessageFormatter.java
@@ -68,7 +68,7 @@ public class MessageFormatter
                             .map(Supplier::get)
                             .toArray();
 
-        final int length = parameters == null ? 0 : parameters.length;
+        final int length = parameters.length;
         if (length > 0 && parameters[length - 1] instanceof Throwable)
         {
             this.throwable = (Throwable) parameters[length - 1];
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/config/OptionConverter.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/config/OptionConverter.java
index 1f3b44b..29179b0 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/config/OptionConverter.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/config/OptionConverter.java
@@ -253,17 +253,14 @@ public class OptionConverter
             multiplier = 1024 * 1024 * 1024;
             s = s.substring( 0, index );
         }
-        if ( s != null )
+        try
         {
-            try
-            {
-                return Long.parseLong(s) * multiplier;
-            }
-            catch ( final NumberFormatException e )
-            {
-                log.error( "[{0}] is not in proper int form.", s);
-                log.error( "[{0}] not in expected format", value, e );
-            }
+            return Long.parseLong(s) * multiplier;
+        }
+        catch ( final NumberFormatException e )
+        {
+            log.error( "[{0}] is not in proper int form.", s);
+            log.error( "[{0}] not in expected format", value, e );
         }
         return defaultValue;
     }
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/net/HostNameUtil.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/net/HostNameUtil.java
index ee6882a..f06cab3 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/net/HostNameUtil.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/utils/net/HostNameUtil.java
@@ -124,37 +124,34 @@ public class HostNameUtil
             InetAddress candidateAddress = null;
             // Iterate all NICs (network interface cards)...
             final Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
-            if ( ifaces != null )
+            while ( ifaces.hasMoreElements() )
             {
-                while ( ifaces.hasMoreElements() )
-                {
-                    final NetworkInterface iface = ifaces.nextElement();
+                final NetworkInterface iface = ifaces.nextElement();
 
-                    // Skip loopback interfaces
-                    if (iface.isLoopback() || !iface.isUp())
-                    {
-                        continue;
-                    }
+                // Skip loopback interfaces
+                if (iface.isLoopback() || !iface.isUp())
+                {
+                    continue;
+                }
 
-                    // Iterate all IP addresses assigned to each card...
-                    for ( final Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); )
+                // Iterate all IP addresses assigned to each card...
+                for ( final Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); )
+                {
+                    final InetAddress inetAddr = inetAddrs.nextElement();
+                    if ( !inetAddr.isLoopbackAddress() )
                     {
-                        final InetAddress inetAddr = inetAddrs.nextElement();
-                        if ( !inetAddr.isLoopbackAddress() )
+                        if (inetAddr.isSiteLocalAddress())
                         {
-                            if (inetAddr.isSiteLocalAddress())
-                            {
-                                // Found non-loopback site-local address.
-                                addresses.add(inetAddr);
-                            }
-                            if ( candidateAddress == null )
-                            {
-                                // Found non-loopback address, but not necessarily site-local.
-                                // Store it as a candidate to be returned if site-local address is not subsequently found...
-                                candidateAddress = inetAddr;
-                                // Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates,
-                                // only the first. For subsequent iterations, candidate will be non-null.
-                            }
+                            // Found non-loopback site-local address.
+                            addresses.add(inetAddr);
+                        }
+                        if ( candidateAddress == null )
+                        {
+                            // Found non-loopback address, but not necessarily site-local.
+                            // Store it as a candidate to be returned if site-local address is not subsequently found...
+                            candidateAddress = inetAddr;
+                            // Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates,
+                            // only the first. For subsequent iterations, candidate will be non-null.
                         }
                     }
                 }
@@ -199,22 +196,20 @@ public class HostNameUtil
     public static NetworkInterface getMulticastNetworkInterface() throws SocketException
     {
         final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
-        if (networkInterfaces != null) {
-            while (networkInterfaces.hasMoreElements())
+        while (networkInterfaces.hasMoreElements())
+        {
+            final NetworkInterface networkInterface = networkInterfaces.nextElement();
+            final Enumeration<InetAddress> addressesFromNetworkInterface = networkInterface.getInetAddresses();
+            while (addressesFromNetworkInterface.hasMoreElements())
             {
-                final NetworkInterface networkInterface = networkInterfaces.nextElement();
-                final Enumeration<InetAddress> addressesFromNetworkInterface = networkInterface.getInetAddresses();
-                while (addressesFromNetworkInterface.hasMoreElements())
+                final InetAddress inetAddress = addressesFromNetworkInterface.nextElement();
+                if (inetAddress.isSiteLocalAddress()
+                        && !inetAddress.isAnyLocalAddress()
+                        && !inetAddress.isLinkLocalAddress()
+                        && !inetAddress.isLoopbackAddress()
+                        && !inetAddress.isMulticastAddress())
                 {
-                    final InetAddress inetAddress = addressesFromNetworkInterface.nextElement();
-                    if (inetAddress.isSiteLocalAddress()
-                            && !inetAddress.isAnyLocalAddress()
-                            && !inetAddress.isLinkLocalAddress()
-                            && !inetAddress.isLoopbackAddress()
-                            && !inetAddress.isMulticastAddress())
-                    {
-                        return networkInterface;
-                    }
+                    return networkInterface;
                 }
             }
         }
diff --git a/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/JCSCachingManager.java b/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/JCSCachingManager.java
index 90ec8f0..886d3c0 100644
--- a/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/JCSCachingManager.java
+++ b/commons-jcs-jcache/src/main/java/org/apache/commons/jcs3/jcache/JCSCachingManager.java
@@ -189,8 +189,8 @@ public class JCSCachingManager implements CacheManager
         assertNotClosed();
         assertNotNull(cacheName, "cacheName");
         assertNotNull(configuration, "configuration");
-        final Class<?> keyType = configuration == null ? Object.class : configuration.getKeyType();
-        final Class<?> valueType = configuration == null ? Object.class : configuration.getValueType();
+        final Class<?> keyType = configuration.getKeyType();
+        final Class<?> valueType = configuration.getValueType();
         if (caches.containsKey(cacheName)) {
             throw new javax.cache.CacheException("cache " + cacheName + " already exists");
         }