From 5819a966c7709b111d2c0947d266e5ef50414502 Mon Sep 17 00:00:00 2001 From: Chen <50514813+dota17@users.noreply.github.com> Date: Wed, 8 Apr 2020 00:06:44 +0800 Subject: [PATCH] Fixed the typo and deal the NPE with Objects.requireNonNull (#118) * Fixed the typo and deal the NPE with Objects.requireNonNull in AbstractLinkedList * Add a space after the comma and remove the unused `o1` and `o2` * Fixtypo * Keep it simple. --- .../collections4/list/AbstractLinkedList.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/list/AbstractLinkedList.java b/src/main/java/org/apache/commons/collections4/list/AbstractLinkedList.java index dae0eb654..2d8e182ec 100644 --- a/src/main/java/org/apache/commons/collections4/list/AbstractLinkedList.java +++ b/src/main/java/org/apache/commons/collections4/list/AbstractLinkedList.java @@ -27,6 +27,7 @@ import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.NoSuchElementException; +import java.util.Objects; import org.apache.commons.collections4.OrderedIterator; @@ -385,9 +386,7 @@ public abstract class AbstractLinkedList implements List { final ListIterator it1 = listIterator(); final ListIterator it2 = other.listIterator(); while (it1.hasNext() && it2.hasNext()) { - final Object o1 = it1.next(); - final Object o2 = it2.next(); - if (!(o1 == null ? o2 == null : o1.equals(o2))) { + if (!(Objects.equals(it1.next(), it2.next()))) { return false; } } @@ -436,7 +435,7 @@ public abstract class AbstractLinkedList implements List { * @return true if equal */ protected boolean isEqualValue(final Object value1, final Object value2) { - return value1 == value2 || (value1 != null && value1.equals(value2)); + return Objects.equals(value1, value2); } /** @@ -514,6 +513,8 @@ public abstract class AbstractLinkedList implements List { * @throws NullPointerException if either node is null */ protected void addNode(final Node nodeToInsert, final Node insertBeforeNode) { + Objects.requireNonNull(nodeToInsert, "nodeToInsert"); + Objects.requireNonNull(insertBeforeNode, "insertBeforeNode"); nodeToInsert.next = insertBeforeNode; nodeToInsert.previous = insertBeforeNode.previous; insertBeforeNode.previous.next = nodeToInsert; @@ -529,6 +530,7 @@ public abstract class AbstractLinkedList implements List { * @throws NullPointerException if {@code node} is null */ protected void removeNode(final Node node) { + Objects.requireNonNull(node, "node"); node.previous.next = node.next; node.next.previous = node.previous; size--; @@ -925,7 +927,7 @@ public abstract class AbstractLinkedList implements List { */ protected static class LinkedSubListIterator extends LinkedListIterator { - /** The parent list */ + /** The sub list */ protected final LinkedSubList sub; protected LinkedSubListIterator(final LinkedSubList sub, final int startIndex) {