No need to nest in else.

This commit is contained in:
Gary Gregory 2021-03-05 13:38:04 -05:00
parent 86733aae3a
commit 26b0f41a09
10 changed files with 106 additions and 90 deletions

View File

@ -1437,25 +1437,29 @@ public class CollectionUtils {
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 class CollectionUtils {
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());
}
}

View File

@ -1119,7 +1119,8 @@ public class IteratorUtils {
}
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 @@ public class IteratorUtils {
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;

View File

@ -529,10 +529,9 @@ public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>>
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 @@ public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>>
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 @@ public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>>
break;
}
node = node.getRight(KEY);
}
}
}
@ -1327,26 +1326,25 @@ public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>>
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);
}
}
}

View File

@ -106,15 +106,13 @@ public class EnumerationIterator<E> implements Iterator<E> {
*/
@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

View File

@ -104,15 +104,14 @@ public class SingletonIterator<E>
*/
@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;
}
/**

View File

@ -172,21 +172,21 @@ public class LazyList<E> extends AbstractSerializableListDecorator<E> {
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!");
}
}

View File

@ -125,7 +125,8 @@ public abstract class AbstractBitwiseTrie<K, V> extends AbstractMap<K, V>
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 abstract class AbstractBitwiseTrie<K, V> extends AbstractMap<K, V>
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Map.Entry)) {
}
if (!(o instanceof Map.Entry)) {
return false;
}

View File

@ -160,7 +160,8 @@ abstract class AbstractPatriciaTrie<K, V> extends AbstractBitwiseTrie<K, V> {
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 @@ abstract class AbstractPatriciaTrie<K, V> extends AbstractBitwiseTrie<K, V> {
}
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 @@ abstract class AbstractPatriciaTrie<K, V> extends AbstractBitwiseTrie<K, V> {
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 @@ abstract class AbstractPatriciaTrie<K, V> extends AbstractBitwiseTrie<K, V> {
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 @@ abstract class AbstractPatriciaTrie<K, V> extends AbstractBitwiseTrie<K, V> {
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 @@ abstract class AbstractPatriciaTrie<K, V> extends AbstractBitwiseTrie<K, V> {
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 @@ abstract class AbstractPatriciaTrie<K, V> extends AbstractBitwiseTrie<K, V> {
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 @@ abstract class AbstractPatriciaTrie<K, V> extends AbstractBitwiseTrie<K, V> {
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 @@ abstract class AbstractPatriciaTrie<K, V> extends AbstractBitwiseTrie<K, V> {
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);
}
/**

View File

@ -140,7 +140,8 @@ public abstract class KeyAnalyzer<K> implements Comparator<K>, Serializable {
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;
}

View File

@ -1195,9 +1195,8 @@ public class MapUtilsTest {
assertEquals(val.intValue(), MapUtils.getNumber(in, "noKey", key -> {
if (true) {
return val;
} else {
return null;
}
return null;
}).intValue(), 0);
}
@ -1214,9 +1213,8 @@ public class MapUtilsTest {
assertEquals("default", MapUtils.getString(in, "noKey", key -> {
if ("noKey".equals(key)) {
return "default";
} else {
return "";
}
return "";
}));
assertEquals("default", MapUtils.getString(null, "noKey", "default"));
}