No need to nest in else.
diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java
index b5820aa..1176477 100644
--- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java
+++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java
@@ -1437,25 +1437,29 @@
final Map<?, ?> map = (Map<?, ?>) object;
final Iterator<?> iterator = map.entrySet().iterator();
return IteratorUtils.get(iterator, i);
- } else if (object instanceof Object[]) {
+ }
+ if (object instanceof Object[]) {
return ((Object[]) object)[i];
- } else if (object instanceof Iterator<?>) {
+ }
+ if (object instanceof Iterator<?>) {
final Iterator<?> it = (Iterator<?>) object;
return IteratorUtils.get(it, i);
- } else if (object instanceof Iterable<?>) {
+ }
+ if (object instanceof Iterable<?>) {
final Iterable<?> iterable = (Iterable<?>) object;
return IterableUtils.get(iterable, i);
- } else if (object instanceof Enumeration<?>) {
+ }
+ if (object instanceof Enumeration<?>) {
final Enumeration<?> it = (Enumeration<?>) object;
return EnumerationUtils.get(it, i);
- } else if (object == null) {
+ }
+ if (object == null) {
throw new IllegalArgumentException("Unsupported object type: null");
- } else {
- try {
- return Array.get(object, i);
- } catch (final IllegalArgumentException ex) {
- throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
- }
+ }
+ try {
+ return Array.get(object, i);
+ } catch (final IllegalArgumentException ex) {
+ throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
}
}
@@ -1550,24 +1554,29 @@
public static boolean sizeIsEmpty(final Object object) {
if (object == null) {
return true;
- } else if (object instanceof Collection<?>) {
+ }
+ if (object instanceof Collection<?>) {
return ((Collection<?>) object).isEmpty();
- } else if (object instanceof Iterable<?>) {
+ }
+ if (object instanceof Iterable<?>) {
return IterableUtils.isEmpty((Iterable<?>) object);
- } else if (object instanceof Map<?, ?>) {
+ }
+ if (object instanceof Map<?, ?>) {
return ((Map<?, ?>) object).isEmpty();
- } else if (object instanceof Object[]) {
+ }
+ if (object instanceof Object[]) {
return ((Object[]) object).length == 0;
- } else if (object instanceof Iterator<?>) {
+ }
+ if (object instanceof Iterator<?>) {
return ((Iterator<?>) object).hasNext() == false;
- } else if (object instanceof Enumeration<?>) {
+ }
+ if (object instanceof Enumeration<?>) {
return ((Enumeration<?>) object).hasMoreElements() == false;
- } else {
- try {
- return Array.getLength(object) == 0;
- } catch (final IllegalArgumentException ex) {
- throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
- }
+ }
+ try {
+ return Array.getLength(object) == 0;
+ } catch (final IllegalArgumentException ex) {
+ throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
}
}
diff --git a/src/main/java/org/apache/commons/collections4/IteratorUtils.java b/src/main/java/org/apache/commons/collections4/IteratorUtils.java
index d11ceef..a091144 100644
--- a/src/main/java/org/apache/commons/collections4/IteratorUtils.java
+++ b/src/main/java/org/apache/commons/collections4/IteratorUtils.java
@@ -1119,7 +1119,8 @@
}
if (obj instanceof Dictionary) {
return new EnumerationIterator<>(((Dictionary<?, ?>) obj).elements());
- } else if (obj.getClass().isArray()) {
+ }
+ if (obj.getClass().isArray()) {
return new ArrayIterator<>(obj);
}
try {
@@ -1178,11 +1179,10 @@
if (iterator != null) {
while (iterator.hasNext()) {
final E element = iterator.next();
- if (iterator.hasNext()) {
- closure.execute(element);
- } else {
+ if (!iterator.hasNext()) {
return element;
}
+ closure.execute(element);
}
}
return 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 d7048d5..20094be 100644
--- a/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java
+++ b/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java
@@ -529,10 +529,9 @@
if (cmp == 0) {
// shouldn't happen
throw new IllegalArgumentException("Cannot store a duplicate key (\"" + key + "\") in this Map");
- } else if (cmp < 0) {
- if (node.getLeft(KEY) != null) {
- node = node.getLeft(KEY);
- } else {
+ }
+ if (cmp < 0) {
+ if (node.getLeft(KEY) == null) {
final Node<K, V> newNode = new Node<>(key, value);
insertValue(newNode);
@@ -543,10 +542,9 @@
break;
}
+ node = node.getLeft(KEY);
} else { // cmp > 0
- if (node.getRight(KEY) != null) {
- node = node.getRight(KEY);
- } else {
+ if (node.getRight(KEY) == null) {
final Node<K, V> newNode = new Node<>(key, value);
insertValue(newNode);
@@ -557,6 +555,7 @@
break;
}
+ node = node.getRight(KEY);
}
}
}
@@ -1327,26 +1326,25 @@
if (cmp == 0) {
throw new IllegalArgumentException(
"Cannot store a duplicate value (\"" + newNode.getData(VALUE) + "\") in this Map");
- } else if (cmp < 0) {
- if (node.getLeft(VALUE) != null) {
- node = node.getLeft(VALUE);
- } else {
+ }
+ if (cmp < 0) {
+ if (node.getLeft(VALUE) == null) {
node.setLeft(newNode, VALUE);
newNode.setParent(node, VALUE);
doRedBlackInsert(newNode, VALUE);
break;
}
+ node = node.getLeft(VALUE);
} else { // cmp > 0
- if (node.getRight(VALUE) != null) {
- node = node.getRight(VALUE);
- } else {
+ if (node.getRight(VALUE) == null) {
node.setRight(newNode, VALUE);
newNode.setParent(node, VALUE);
doRedBlackInsert(newNode, VALUE);
break;
}
+ node = node.getRight(VALUE);
}
}
}
diff --git a/src/main/java/org/apache/commons/collections4/iterators/EnumerationIterator.java b/src/main/java/org/apache/commons/collections4/iterators/EnumerationIterator.java
index d99ef66..315a41b 100644
--- a/src/main/java/org/apache/commons/collections4/iterators/EnumerationIterator.java
+++ b/src/main/java/org/apache/commons/collections4/iterators/EnumerationIterator.java
@@ -106,15 +106,13 @@
*/
@Override
public void remove() {
- if (collection != null) {
- if (last != null) {
- collection.remove(last);
- } else {
- throw new IllegalStateException("next() must have been called for remove() to function");
- }
- } else {
+ if (collection == null) {
throw new UnsupportedOperationException("No Collection associated with this Iterator");
}
+ if (last == null) {
+ throw new IllegalStateException("next() must have been called for remove() to function");
+ }
+ collection.remove(last);
}
// Properties
diff --git a/src/main/java/org/apache/commons/collections4/iterators/SingletonIterator.java b/src/main/java/org/apache/commons/collections4/iterators/SingletonIterator.java
index cba28f4..0817709 100644
--- a/src/main/java/org/apache/commons/collections4/iterators/SingletonIterator.java
+++ b/src/main/java/org/apache/commons/collections4/iterators/SingletonIterator.java
@@ -104,15 +104,14 @@
*/
@Override
public void remove() {
- if (removeAllowed) {
- if (removed || beforeFirst) {
- throw new IllegalStateException();
- }
- object = null;
- removed = true;
- } else {
+ if (!removeAllowed) {
throw new UnsupportedOperationException();
}
+ if (removed || beforeFirst) {
+ throw new IllegalStateException();
+ }
+ object = null;
+ removed = true;
}
/**
diff --git a/src/main/java/org/apache/commons/collections4/list/LazyList.java b/src/main/java/org/apache/commons/collections4/list/LazyList.java
index c511b75..ffc4322 100644
--- a/src/main/java/org/apache/commons/collections4/list/LazyList.java
+++ b/src/main/java/org/apache/commons/collections4/list/LazyList.java
@@ -172,21 +172,21 @@
final List<E> sub = decorated().subList(fromIndex, toIndex);
if (factory != null) {
return new LazyList<>(sub, factory);
- } else if (transformer != null) {
- return new LazyList<>(sub, transformer);
- } else {
- throw new IllegalStateException("Factory and Transformer are both null!");
}
+ if (transformer != null) {
+ return new LazyList<>(sub, transformer);
+ }
+ throw new IllegalStateException("Factory and Transformer are both null!");
}
private E element(final int index) {
if (factory != null) {
return factory.create();
- } else if (transformer != null) {
- return transformer.transform(index);
- } else {
- throw new IllegalStateException("Factory and Transformer are both null!");
}
+ if (transformer != null) {
+ return transformer.transform(index);
+ }
+ throw new IllegalStateException("Factory and Transformer are both null!");
}
}
diff --git a/src/main/java/org/apache/commons/collections4/trie/AbstractBitwiseTrie.java b/src/main/java/org/apache/commons/collections4/trie/AbstractBitwiseTrie.java
index ab5ffe2..c8cae43 100644
--- a/src/main/java/org/apache/commons/collections4/trie/AbstractBitwiseTrie.java
+++ b/src/main/java/org/apache/commons/collections4/trie/AbstractBitwiseTrie.java
@@ -125,7 +125,8 @@
final boolean compareKeys(final K key, final K other) {
if (key == null) {
return other == null;
- } else if (other == null) {
+ }
+ if (other == null) {
return false;
}
@@ -194,7 +195,8 @@
public boolean equals(final Object o) {
if (o == this) {
return true;
- } else if (!(o instanceof Map.Entry)) {
+ }
+ if (!(o instanceof Map.Entry)) {
return false;
}
diff --git a/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java b/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java
index 92bb7a8..244095e 100644
--- a/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java
+++ b/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java
@@ -160,7 +160,8 @@
addEntry(t, lengthInBits);
incrementSize();
return null;
- } else if (KeyAnalyzer.isNullBitKey(bitIndex)) {
+ }
+ if (KeyAnalyzer.isNullBitKey(bitIndex)) {
// A bits of the Key are zero. The only place to
// store such a Key is the root Node!
@@ -172,7 +173,7 @@
}
return root.setKeyValue(key, value);
- } else /* REPLACE OLD KEY+VALUE */
+ }
if (KeyAnalyzer.isEqualBitKey(bitIndex) && found != root) { // NOPMD
incrementModCount();
return found.setKeyValue(key, value);
@@ -488,7 +489,8 @@
private void removeExternalEntry(final TrieEntry<K, V> h) {
if (h == root) {
throw new IllegalArgumentException("Cannot delete root Entry!");
- } else if (!h.isExternalNode()) {
+ }
+ if (!h.isExternalNode()) {
throw new IllegalArgumentException(h + " is not an external Entry!");
}
@@ -520,7 +522,8 @@
private void removeInternalEntry(final TrieEntry<K, V> h) {
if (h == root) {
throw new IllegalArgumentException("Cannot delete root Entry!");
- } else if (!h.isInternalNode()) {
+ }
+ if (!h.isInternalNode()) {
throw new IllegalArgumentException(h + " is not an internal Entry!");
}
@@ -903,15 +906,17 @@
removeEntry(added);
modCount -= 2; // we didn't really modify it.
return ceil;
- } else if (KeyAnalyzer.isNullBitKey(bitIndex)) {
+ }
+ if (KeyAnalyzer.isNullBitKey(bitIndex)) {
if (!root.isEmpty()) {
return firstEntry();
- } else if (size() > 1) {
- return nextEntry(firstEntry());
- } else {
- return null;
}
- } else if (KeyAnalyzer.isEqualBitKey(bitIndex)) {
+ if (size() > 1) {
+ return nextEntry(firstEntry());
+ }
+ return null;
+ }
+ if (KeyAnalyzer.isEqualBitKey(bitIndex)) {
return nextEntry(found);
}
@@ -965,12 +970,14 @@
removeEntry(added);
modCount -= 2; // we didn't really modify it.
return ceil;
- } else if (KeyAnalyzer.isNullBitKey(bitIndex)) {
+ }
+ if (KeyAnalyzer.isNullBitKey(bitIndex)) {
if (!root.isEmpty()) {
return root;
}
return firstEntry();
- } else if (KeyAnalyzer.isEqualBitKey(bitIndex)) {
+ }
+ if (KeyAnalyzer.isEqualBitKey(bitIndex)) {
return found;
}
@@ -1020,9 +1027,11 @@
removeEntry(added);
modCount -= 2; // we didn't really modify it.
return prior;
- } else if (KeyAnalyzer.isNullBitKey(bitIndex)) {
+ }
+ if (KeyAnalyzer.isNullBitKey(bitIndex)) {
return null;
- } else if (KeyAnalyzer.isEqualBitKey(bitIndex)) {
+ }
+ if (KeyAnalyzer.isEqualBitKey(bitIndex)) {
return previousEntry(found);
}
@@ -1061,12 +1070,14 @@
removeEntry(added);
modCount -= 2; // we didn't really modify it.
return floor;
- } else if (KeyAnalyzer.isNullBitKey(bitIndex)) {
+ }
+ if (KeyAnalyzer.isNullBitKey(bitIndex)) {
if (!root.isEmpty()) {
return root;
}
return null;
- } else if (KeyAnalyzer.isEqualBitKey(bitIndex)) {
+ }
+ if (KeyAnalyzer.isEqualBitKey(bitIndex)) {
return found;
}
@@ -2287,11 +2298,11 @@
if (prefixStart == null) {
final Set<Map.Entry<K, V>> empty = Collections.emptySet();
return empty.iterator();
- } else if (delegate.lengthInBits > prefixStart.bitIndex) {
- return new SingletonIterator(prefixStart);
- } else {
- return new EntryIterator(prefixStart, delegate.prefix, delegate.offsetInBits, delegate.lengthInBits);
}
+ if (delegate.lengthInBits > prefixStart.bitIndex) {
+ return new SingletonIterator(prefixStart);
+ }
+ return new EntryIterator(prefixStart, delegate.prefix, delegate.offsetInBits, delegate.lengthInBits);
}
/**
diff --git a/src/main/java/org/apache/commons/collections4/trie/KeyAnalyzer.java b/src/main/java/org/apache/commons/collections4/trie/KeyAnalyzer.java
index 63569c8..5f54bd8 100644
--- a/src/main/java/org/apache/commons/collections4/trie/KeyAnalyzer.java
+++ b/src/main/java/org/apache/commons/collections4/trie/KeyAnalyzer.java
@@ -140,7 +140,8 @@
public int compare(final K o1, final K o2) {
if (o1 == null) {
return o2 == null ? 0 : -1;
- } else if (o2 == null) {
+ }
+ if (o2 == null) {
return 1;
}
diff --git a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
index 80bdb5d..e6aa766 100644
--- a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
+++ b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
@@ -1195,9 +1195,8 @@
assertEquals(val.intValue(), MapUtils.getNumber(in, "noKey", key -> {
if (true) {
return val;
- } else {
- return null;
}
+ return null;
}).intValue(), 0);
}
@@ -1214,9 +1213,8 @@
assertEquals("default", MapUtils.getString(in, "noKey", key -> {
if ("noKey".equals(key)) {
return "default";
- } else {
- return "";
}
+ return "";
}));
assertEquals("default", MapUtils.getString(null, "noKey", "default"));
}