You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by bu...@apache.org on 2018/10/23 10:55:48 UTC

[cxf] branch master updated: CollectionUtils improvements

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

buhhunyx pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new fe8c618  CollectionUtils improvements
fe8c618 is described below

commit fe8c6187ade3699e085623c92ddd3abcfe75a2a0
Author: amarkevich <am...@talend.com>
AuthorDate: Tue Oct 23 13:04:38 2018 +0300

    CollectionUtils improvements
---
 .../apache/cxf/common/util/CollectionUtils.java    | 56 +++++++---------------
 .../cxf/common/util/CollectionUtilsTest.java       | 42 +++++++++++-----
 2 files changed, 48 insertions(+), 50 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/common/util/CollectionUtils.java b/core/src/main/java/org/apache/cxf/common/util/CollectionUtils.java
index c0a7e08..547fdef 100644
--- a/core/src/main/java/org/apache/cxf/common/util/CollectionUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/util/CollectionUtils.java
@@ -33,7 +33,7 @@ public final class CollectionUtils {
     }
 
     public static <T> Collection<T> diff(Collection<T> c1, Collection<T> c2) {
-        if (c1 == null || c1.isEmpty() || c2 == null || c2.size() == 0) {
+        if (c1 == null || c1.isEmpty() || c2 == null || c2.isEmpty()) {
             return c1;
         }
         Collection<T> difference = new ArrayList<>();
@@ -46,12 +46,11 @@ public final class CollectionUtils {
     }
 
     public static <T> boolean isEmpty(Collection<T> c) {
-        if (c == null || c.isEmpty()) {
-            return true;
-        }
-        for (Iterator<T> iter = c.iterator(); iter.hasNext();) {
-            if (iter.next() != null) {
-                return false;
+        if (c != null && !c.isEmpty()) {
+            for (T item : c) {
+                if (item != null) {
+                    return false;
+                }
             }
         }
         return true;
@@ -60,15 +59,16 @@ public final class CollectionUtils {
     public static <S, T> Dictionary<S, T> singletonDictionary(S s, T t) {
         return toDictionary(Collections.singletonMap(s, t));
     }
+
     public static <S, T> Dictionary<S, T> toDictionary(Map<S, T> map) {
         return new MapToDictionary<S, T>(map);
     }
-    
+
     static class MapToDictionary<S, T> extends Dictionary<S, T> {
         /**
          * Map source.
          **/
-        private Map<S, T> map;
+        private final Map<S, T> map;
 
         MapToDictionary(Map<S, T> map) {
             this.map = map;
@@ -76,31 +76,19 @@ public final class CollectionUtils {
 
 
         public Enumeration<T> elements() {
-            if (map == null) {
-                return null;
-            }
-            return new IteratorToEnumeration<T>(map.values().iterator());
+            return map != null ? new IteratorToEnumeration<T>(map.values().iterator()) : null;
         }
 
         public T get(Object key) {
-            if (map == null)  {
-                return null;
-            }
-            return map.get(key);
+            return map != null ? map.get(key) : null;
         }
 
         public boolean isEmpty() {
-            if (map == null) {
-                return true;
-            }
-            return map.isEmpty();
+            return map != null ? map.isEmpty() : true;
         }
 
         public Enumeration<S> keys() {
-            if (map == null) {
-                return null;
-            }
-            return new IteratorToEnumeration<S>(map.keySet().iterator());
+            return map != null ? new IteratorToEnumeration<S>(map.keySet().iterator()) : null;
         }
 
         public T put(S key, T value) {
@@ -112,13 +100,9 @@ public final class CollectionUtils {
         }
 
         public int size() {
-            if (map == null) {
-                return 0;
-            }
-            return map.size();
+            return map != null ? map.size() : 0;
         }
-        
-        
+
         static class IteratorToEnumeration<X> implements Enumeration<X> {
             private final Iterator<X> iter;
 
@@ -127,17 +111,11 @@ public final class CollectionUtils {
             }
 
             public boolean hasMoreElements() {
-                if (iter == null) {
-                    return false;
-                }
-                return iter.hasNext();
+                return iter != null ? iter.hasNext() : false;
             }
 
             public X nextElement() {
-                if (iter == null) {
-                    return null;
-                }
-                return iter.next();
+                return iter != null ? iter.next() : null;
             }
         }
     }
diff --git a/core/src/test/java/org/apache/cxf/common/util/CollectionUtilsTest.java b/core/src/test/java/org/apache/cxf/common/util/CollectionUtilsTest.java
index 4947573..e290f1c 100644
--- a/core/src/test/java/org/apache/cxf/common/util/CollectionUtilsTest.java
+++ b/core/src/test/java/org/apache/cxf/common/util/CollectionUtilsTest.java
@@ -21,8 +21,7 @@ package org.apache.cxf.common.util;
 
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.List;
-
+import java.util.Dictionary;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -31,18 +30,16 @@ public class CollectionUtilsTest extends Assert {
 
     @Test
     public void testDiff() throws Exception {
-        List<String> l1 = Arrays.asList(new String[]{"1", "2", "3"});
-        List<String> l2 = Arrays.asList(new String[]{"2", "4", "5"});
+        Collection<String> l1 = Arrays.asList("1", "2", "3");
+        Collection<String> l2 = Arrays.asList("2", "4", "5");
         Collection<String> l3 = CollectionUtils.diff(l1, l2);
-        assertTrue(l3.size() == 2);
+        assertEquals(2, l3.size());
         assertTrue(l3.contains("1"));
         assertTrue(l3.contains("3"));
 
         l3 = CollectionUtils.diff(l1, null);
-        assertTrue(l3.size() == 3);
-        assertTrue(l3.contains("1"));
-        assertTrue(l3.contains("2"));
-        assertTrue(l3.contains("3"));
+        assertEquals(3, l3.size());
+        assertTrue(l3.containsAll(l1));
 
         l3 = CollectionUtils.diff(null, null);
         assertNull(l3);
@@ -50,8 +47,31 @@ public class CollectionUtilsTest extends Assert {
 
     @Test
     public void testIsEmpty() throws Exception {
-        List<String> l = Arrays.asList(new String[]{null, null});
-        assertNotNull(l);
+        assertTrue(CollectionUtils.isEmpty(null));
+
+        Collection<String> l = Arrays.asList(null, null);
         assertTrue(CollectionUtils.isEmpty(l));
     }
+
+    @Test
+    public void testToDictionaryNull() throws Exception {
+        Dictionary<?, ?> d = CollectionUtils.toDictionary(null);
+        assertNull(d.elements());
+        assertNull(d.get(""));
+        assertTrue(d.isEmpty());
+        assertNull(d.keys());
+        assertEquals(0, d.size());
+    }
+
+    @Test
+    public void testSingletonDictionary() throws Exception {
+        String key = "k";
+        String value = "v";
+        Dictionary<String, String> d = CollectionUtils.singletonDictionary(key, value);
+        assertNotNull(d.elements());
+        assertEquals(value, d.get(key));
+        assertFalse(d.isEmpty());
+        assertNotNull(d.keys());
+        assertEquals(1, d.size());
+    }
 }