amarkevich closed pull request #464: CollectionUtils improvements URL: https://github.com/apache/cxf/pull/464
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): 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 c0a7e088b69..547fdefacae 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 @@ private 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 @@ private 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 @@ private 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 @@ private 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 T remove(Object key) { } 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 int size() { } 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 49475739c87..e290f1ca046 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 @@ 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 @@ @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 void testDiff() throws Exception { @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()); + } } ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services