From 8b66a577f44a16e259ad5ba11695d20e7c3a9cde Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 28 Dec 2017 13:17:33 -0700 Subject: [PATCH] No need to nest in an else clause. --- .../commons/collections4/ArrayStack.java | 9 +-- .../commons/collections4/CollectionUtils.java | 66 +++++++++---------- .../collections4/EnumerationUtils.java | 3 +- .../commons/collections4/FluentIterable.java | 3 +- .../commons/collections4/IterableUtils.java | 15 ++--- .../collections4/bag/CollectionBag.java | 10 ++- .../collections4/bag/CollectionSortedBag.java | 10 ++- .../collections4/bidimap/TreeBidiMap.java | 3 +- .../collections4/functors/EqualPredicate.java | 3 +- .../collections4/functors/IfTransformer.java | 3 +- .../iterators/ZippingIterator.java | 5 +- .../multimap/AbstractMultiValuedMap.java | 11 ++-- .../collections4/CollectionUtilsTest.java | 3 +- .../bidimap/DualTreeBidiMap2Test.java | 3 +- .../bidimap/DualTreeBidiMapTest.java | 3 +- .../iterators/NodeListIteratorTest.java | 11 ++-- .../collections4/list/AbstractListTest.java | 3 +- .../commons/collections4/map/LRUMapTest.java | 3 +- .../map/ReferenceIdentityMapTest.java | 9 ++- .../collections4/map/ReferenceMapTest.java | 9 ++- .../map/TransformedSortedMapTest.java | 3 +- 21 files changed, 78 insertions(+), 110 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/ArrayStack.java b/src/main/java/org/apache/commons/collections4/ArrayStack.java index e346ed0ce..5a6e1f4b5 100644 --- a/src/main/java/org/apache/commons/collections4/ArrayStack.java +++ b/src/main/java/org/apache/commons/collections4/ArrayStack.java @@ -87,9 +87,8 @@ public class ArrayStack extends ArrayList { final int n = size(); if (n <= 0) { throw new EmptyStackException(); - } else { - return get(n - 1); } + return get(n - 1); } /** @@ -105,9 +104,8 @@ public class ArrayStack extends ArrayList { final int m = (size() - n) - 1; if (m < 0) { throw new EmptyStackException(); - } else { - return get(m); } + return get(m); } /** @@ -120,9 +118,8 @@ public class ArrayStack extends ArrayList { final int n = size(); if (n <= 0) { throw new EmptyStackException(); - } else { - return remove(n - 1); } + return remove(n - 1); } /** diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index 5b974cbcb..7e6bc58e1 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -368,30 +368,29 @@ public class CollectionUtils { public static boolean containsAll(final Collection coll1, final Collection coll2) { if (coll2.isEmpty()) { return true; - } else { - final Iterator it = coll1.iterator(); - final Set elementsAlreadySeen = new HashSet<>(); - for (final Object nextElement : coll2) { - if (elementsAlreadySeen.contains(nextElement)) { - continue; - } + } + final Iterator it = coll1.iterator(); + final Set elementsAlreadySeen = new HashSet<>(); + for (final Object nextElement : coll2) { + if (elementsAlreadySeen.contains(nextElement)) { + continue; + } - boolean foundCurrentElement = false; - while (it.hasNext()) { - final Object p = it.next(); - elementsAlreadySeen.add(p); - if (nextElement == null ? p == null : nextElement.equals(p)) { - foundCurrentElement = true; - break; - } - } - - if (!foundCurrentElement) { - return false; + boolean foundCurrentElement = false; + while (it.hasNext()) { + final Object p = it.next(); + elementsAlreadySeen.add(p); + if (nextElement == null ? p == null : nextElement.equals(p)) { + foundCurrentElement = true; + break; } } - return true; + + if (!foundCurrentElement) { + return false; + } } + return true; } /** @@ -1584,21 +1583,20 @@ public class CollectionUtils { final Iterator iterator = new CollatingIterator<>(c, a.iterator(), b.iterator()); if (includeDuplicates) { return IteratorUtils.toList(iterator, totalSize); - } else { - final ArrayList mergedList = new ArrayList<>(totalSize); - - O lastItem = null; - while (iterator.hasNext()) { - final O item = iterator.next(); - if (lastItem == null || !lastItem.equals(item)) { - mergedList.add(item); - } - lastItem = item; - } - - mergedList.trimToSize(); - return mergedList; } + final ArrayList mergedList = new ArrayList<>(totalSize); + + O lastItem = null; + while (iterator.hasNext()) { + final O item = iterator.next(); + if (lastItem == null || !lastItem.equals(item)) { + mergedList.add(item); + } + lastItem = item; + } + + mergedList.trimToSize(); + return mergedList; } //----------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/collections4/EnumerationUtils.java b/src/main/java/org/apache/commons/collections4/EnumerationUtils.java index 7e726d0d5..9c5fabede 100644 --- a/src/main/java/org/apache/commons/collections4/EnumerationUtils.java +++ b/src/main/java/org/apache/commons/collections4/EnumerationUtils.java @@ -57,9 +57,8 @@ public class EnumerationUtils { i--; if (i == -1) { return e.nextElement(); - } else { - e.nextElement(); } + e.nextElement(); } throw new IndexOutOfBoundsException("Entry does not exist: " + i); } diff --git a/src/main/java/org/apache/commons/collections4/FluentIterable.java b/src/main/java/org/apache/commons/collections4/FluentIterable.java index 46acdbf05..7893e7a90 100644 --- a/src/main/java/org/apache/commons/collections4/FluentIterable.java +++ b/src/main/java/org/apache/commons/collections4/FluentIterable.java @@ -124,9 +124,8 @@ public class FluentIterable implements Iterable { IterableUtils.checkNotNull(iterable); if (iterable instanceof FluentIterable) { return (FluentIterable) iterable; - } else { - return new FluentIterable<>(iterable); } + return new FluentIterable<>(iterable); } // Constructor diff --git a/src/main/java/org/apache/commons/collections4/IterableUtils.java b/src/main/java/org/apache/commons/collections4/IterableUtils.java index ad24eeb6a..178364142 100644 --- a/src/main/java/org/apache/commons/collections4/IterableUtils.java +++ b/src/main/java/org/apache/commons/collections4/IterableUtils.java @@ -175,9 +175,8 @@ public class IterableUtils { protected Iterator nextIterator(int count) { if (count > iterables.length) { return null; - } else { - return iterables[count - 1].iterator(); } + return iterables[count - 1].iterator(); } }; } @@ -328,9 +327,8 @@ public class IterableUtils { protected Iterator nextIterator(int count) { if (IterableUtils.isEmpty(iterable)) { return null; - } else { - return iterable.iterator(); } + return iterable.iterator(); } }; } @@ -690,9 +688,8 @@ public class IterableUtils { public static boolean isEmpty(final Iterable iterable) { if (iterable instanceof Collection) { return ((Collection) iterable).isEmpty(); - } else { - return IteratorUtils.isEmpty(emptyIteratorIfNull(iterable)); } + return IteratorUtils.isEmpty(emptyIteratorIfNull(iterable)); } /** @@ -708,9 +705,8 @@ public class IterableUtils { public static boolean contains(final Iterable iterable, final Object object) { if (iterable instanceof Collection) { return ((Collection) iterable).contains(object); - } else { - return IteratorUtils.contains(emptyIteratorIfNull(iterable), object); } + return IteratorUtils.contains(emptyIteratorIfNull(iterable), object); } /** @@ -788,9 +784,8 @@ public class IterableUtils { public static int size(final Iterable iterable) { if (iterable instanceof Collection) { return ((Collection) iterable).size(); - } else { - return IteratorUtils.size(emptyIteratorIfNull(iterable)); } + return IteratorUtils.size(emptyIteratorIfNull(iterable)); } /** diff --git a/src/main/java/org/apache/commons/collections4/bag/CollectionBag.java b/src/main/java/org/apache/commons/collections4/bag/CollectionBag.java index 5ea08f2ff..7fbe3460f 100644 --- a/src/main/java/org/apache/commons/collections4/bag/CollectionBag.java +++ b/src/main/java/org/apache/commons/collections4/bag/CollectionBag.java @@ -177,10 +177,9 @@ public final class CollectionBag extends AbstractBagDecorator { result = result || changed; } return result; - } else { - // let the decorated bag handle the case of null argument - return decorated().removeAll(null); } + // let the decorated bag handle the case of null argument + return decorated().removeAll(null); } /** @@ -212,10 +211,9 @@ public final class CollectionBag extends AbstractBagDecorator { } } return modified; - } else { - // let the decorated bag handle the case of null argument - return decorated().retainAll(null); } + // let the decorated bag handle the case of null argument + return decorated().retainAll(null); } //----------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/collections4/bag/CollectionSortedBag.java b/src/main/java/org/apache/commons/collections4/bag/CollectionSortedBag.java index 94598343e..88a4c76da 100644 --- a/src/main/java/org/apache/commons/collections4/bag/CollectionSortedBag.java +++ b/src/main/java/org/apache/commons/collections4/bag/CollectionSortedBag.java @@ -130,10 +130,9 @@ public final class CollectionSortedBag extends AbstractSortedBagDecorator result = result || changed; } return result; - } else { - // let the decorated bag handle the case of null argument - return decorated().removeAll(null); } + // let the decorated bag handle the case of null argument + return decorated().removeAll(null); } @Override @@ -148,10 +147,9 @@ public final class CollectionSortedBag extends AbstractSortedBagDecorator } } return modified; - } else { - // let the decorated bag handle the case of null argument - return decorated().retainAll(null); } + // let the decorated bag handle the case of null argument + return decorated().retainAll(null); } //----------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java b/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java index 1f48b9c73..5622cc175 100644 --- a/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java +++ b/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java @@ -590,9 +590,8 @@ public class TreeBidiMap, V extends Comparable> if (cmp == 0) { rval = node; break; - } else { - node = cmp < 0 ? node.getLeft(dataElement) : node.getRight(dataElement); } + node = cmp < 0 ? node.getLeft(dataElement) : node.getRight(dataElement); } return rval; diff --git a/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java b/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java index 04b5530ee..90ed03757 100644 --- a/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java @@ -104,9 +104,8 @@ public final class EqualPredicate implements Predicate, Serializable { public boolean evaluate(final T object) { if (equator != null) { return equator.equate(iValue, object); - } else { - return iValue.equals(object); } + return iValue.equals(object); } /** diff --git a/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java b/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java index 081513c2b..75dc7b901 100644 --- a/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java +++ b/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java @@ -120,9 +120,8 @@ public class IfTransformer implements Transformer, Serializable { public O transform(final I input) { if(iPredicate.evaluate(input)){ return iTrueTransformer.transform(input); - } else { - return iFalseTransformer.transform(input); } + return iFalseTransformer.transform(input); } /** diff --git a/src/main/java/org/apache/commons/collections4/iterators/ZippingIterator.java b/src/main/java/org/apache/commons/collections4/iterators/ZippingIterator.java index 6bdf20aca..e82ee9c7c 100644 --- a/src/main/java/org/apache/commons/collections4/iterators/ZippingIterator.java +++ b/src/main/java/org/apache/commons/collections4/iterators/ZippingIterator.java @@ -116,10 +116,9 @@ public class ZippingIterator implements Iterator { if (childIterator.hasNext()) { nextIterator = childIterator; return true; - } else { - // iterator is exhausted, remove it - iterators.remove(); } + // iterator is exhausted, remove it + iterators.remove(); } return false; } diff --git a/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java b/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java index 620667b59..47724685e 100644 --- a/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java +++ b/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java @@ -259,12 +259,10 @@ public abstract class AbstractMultiValuedMap implements MultiValuedMap implements MultiValuedMap) { Collection valueCollection = (Collection) values; return !valueCollection.isEmpty() && get(key).addAll(valueCollection); - } else { - Iterator it = values.iterator(); - return it.hasNext() && CollectionUtils.addAll(get(key), it); } + Iterator it = values.iterator(); + return it.hasNext() && CollectionUtils.addAll(get(key), it); } @Override diff --git a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java index dcfa8724a..d61aa42a6 100644 --- a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java @@ -568,9 +568,8 @@ public class CollectionUtilsTest extends MockTestCase { public boolean equate(final Integer o1, final Integer o2) { if (o1.intValue() % 2 == 0 ^ o2.intValue() % 2 == 0) { return false; - } else { - return true; } + return true; } @Override diff --git a/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMap2Test.java b/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMap2Test.java index d4d8461cf..2c3978d7d 100644 --- a/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMap2Test.java +++ b/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMap2Test.java @@ -171,9 +171,8 @@ public class DualTreeBidiMap2Test, V extends Comparable< preTail + "bulkTestMapValues.testCollectionRemoveAll", preTail + "bulkTestMapValues.testCollectionRetainAll" }; - } else { - return new String[] { recursiveTest }; } + return new String[] { recursiveTest }; } diff --git a/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMapTest.java b/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMapTest.java index ed56dda20..fbd34bf2d 100644 --- a/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMapTest.java +++ b/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMapTest.java @@ -65,9 +65,8 @@ public class DualTreeBidiMapTest, V extends Comparable { if (createIteratorWithStandardConstr) { return new NodeListIterator(emptyNodeList); - } else { - final Node parentNode = createMock(Node.class); - expect(parentNode.getChildNodes()).andStubReturn(emptyNodeList); - replay(parentNode); - - return new NodeListIterator(parentNode); } + final Node parentNode = createMock(Node.class); + expect(parentNode.getChildNodes()).andStubReturn(emptyNodeList); + replay(parentNode); + + return new NodeListIterator(parentNode); } @Override diff --git a/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java b/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java index bb6741faa..ccd53cd0f 100644 --- a/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java @@ -1337,9 +1337,8 @@ public abstract class AbstractListTest extends AbstractCollectionTest { if (t instanceof ConcurrentModificationException) { // expected return; - } else { - fail(m.getName() + " raised unexpected " + e); } + fail(m.getName() + " raised unexpected " + e); } } diff --git a/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java b/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java index 49b365a07..46368c81e 100644 --- a/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java @@ -447,9 +447,8 @@ public class LRUMapTest extends AbstractOrderedMapTest { protected boolean removeLRU(final LinkEntry entry) { if ("a".equals(entry.getValue())) { return false; - } else { - return true; } + return true; } } diff --git a/src/test/java/org/apache/commons/collections4/map/ReferenceIdentityMapTest.java b/src/test/java/org/apache/commons/collections4/map/ReferenceIdentityMapTest.java index 04210b7a5..1ccc8ec97 100644 --- a/src/test/java/org/apache/commons/collections4/map/ReferenceIdentityMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/ReferenceIdentityMapTest.java @@ -310,12 +310,11 @@ public class ReferenceIdentityMapTest extends AbstractIterableMapTest extends AbstractIterableMapTest { if (keyReference.get() == null && valueReference.get() == null) { break; - } else { - // create garbage: - @SuppressWarnings("unused") - final byte[] b = new byte[bytz]; - bytz = bytz * 2; } + // create garbage: + @SuppressWarnings("unused") + final byte[] b = new byte[bytz]; + bytz = bytz * 2; } } diff --git a/src/test/java/org/apache/commons/collections4/map/TransformedSortedMapTest.java b/src/test/java/org/apache/commons/collections4/map/TransformedSortedMapTest.java index bf7ba53c8..7be48c9c5 100644 --- a/src/test/java/org/apache/commons/collections4/map/TransformedSortedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/TransformedSortedMapTest.java @@ -62,9 +62,8 @@ public class TransformedSortedMapTest extends AbstractSortedMapTest preTailMap + "bulkTestMapValues.testCollectionRemoveAll", preTailMap + "bulkTestMapValues.testCollectionRetainAll" }; - } else { - return null; } + return null; } //-----------------------------------------------------------------------