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

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

commit a91ae587a27e6f6156bb368c4e3f832695fd1d80
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Sep 1 16:12:14 2024 -0400

    Use Java 8's Map.getOrDefault()
    
    Javadoc
---
 .../commons/collections4/CollectionUtils.java      | 24 +++++++++++-----------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java 
b/src/main/java/org/apache/commons/collections4/CollectionUtils.java
index cf7d7c984..b5feffd72 100644
--- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java
+++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java
@@ -63,6 +63,8 @@ public class CollectionUtils {
      */
     private static class CardinalityHelper<O> {
 
+        private static final Integer ZERO = Integer.valueOf(0);
+
         /** Contains the cardinality for each object in collection A. */
         final Map<O, Integer> cardinalityA;
 
@@ -81,28 +83,26 @@ public class CollectionUtils {
 
         /**
          * Returns the frequency of this object in collection A.
-         * @param obj  the object
+         *
+         * @param key the key whose associated frequency is to be returned.
          * @return the frequency of the object in collection A
          */
-        public int freqA(final Object obj) {
-            return getFreq(obj, cardinalityA);
+        public int freqA(final Object key) {
+            return getFreq(key, cardinalityA);
         }
 
         /**
          * Returns the frequency of this object in collection B.
-         * @param obj  the object
+         *
+         * @param key the key whose associated frequency is to be returned.
          * @return the frequency of the object in collection B
          */
-        public int freqB(final Object obj) {
-            return getFreq(obj, cardinalityB);
+        public int freqB(final Object key) {
+            return getFreq(key, cardinalityB);
         }
 
-        private int getFreq(final Object obj, final Map<?, Integer> freqMap) {
-            final Integer count = freqMap.get(obj);
-            if (count != null) {
-                return count.intValue();
-            }
-            return 0;
+        private int getFreq(final Object key, final Map<?, Integer> freqMap) {
+            return freqMap.getOrDefault(key, ZERO).intValue();
         }
 
         /**

Reply via email to