Skip to content

Commit

Permalink
PIVOT-282 :: Unify illegal item modification checks in LinkedList
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/incubator/pivot/trunk@812586 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Todd Volkert committed Sep 8, 2009
1 parent 30bb3ae commit e2a6976
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions core/src/org/apache/pivot/collections/LinkedList.java
Expand Up @@ -119,6 +119,9 @@ public void insert(T item) {
throw new IllegalStateException();
}

// Verify that the item is allowed at the specified index
verifyLocation(item, current.previous, current);

// Insert a new node immediately prior to the current node
Node<T> node = new Node<T>(current.previous, current, item);

Expand All @@ -144,19 +147,8 @@ public void update(T item) {

T previousItem = current.item;
if (previousItem != item) {
if (comparator != null) {
// Ensure that the new item is greater or equal to its
// predecessor and less than or equal to its successor
T predecessorItem = (current.previous != null ? current.previous.item : null);
T successorItem = (current.next != null ? current.next.item : null);

if ((predecessorItem != null
&& comparator.compare(item, predecessorItem) == -1)
|| (successorItem != null
&& comparator.compare(item, successorItem) == 1)) {
throw new IllegalArgumentException("Illegal item modification.");
}
}
// Verify that the item is allowed at the specified index
verifyLocation(item, current.previous, current.next);

current.item = item;

Expand Down Expand Up @@ -284,16 +276,8 @@ private void insert(T item, int index, boolean validate) {
Node<T> next = (index == length) ? null : getNode(index);
Node<T> previous = (next == null) ? last : next.previous;

if (comparator != null) {
// Ensure that the new item is greater or equal to its
// predecessor and less than or equal to its successor
if ((previous != null
&& comparator.compare(item, previous.item) == -1)
|| (next != null
&& comparator.compare(item, next.item) == 1)) {
throw new IllegalArgumentException("Illegal item modification.");
}
}
// Verify that the item is allowed at the specified index
verifyLocation(item, previous, next);

Node<T> node = new Node<T>(previous, next, item);
if (previous == null) {
Expand Down Expand Up @@ -330,16 +314,8 @@ public T update(int index, T item) {
T previousItem = node.item;

if (previousItem != item) {
if (comparator != null) {
// Ensure that the new item is greater or equal to its
// predecessor and less than or equal to its successor
if ((node.previous != null
&& comparator.compare(item, node.previous.item) == -1)
|| (node.next != null
&& comparator.compare(item, node.next.item) == 1)) {
throw new IllegalArgumentException("Illegal item modification.");
}
}
// Verify that the item is allowed at the specified index
verifyLocation(item, node.previous, node.next);

// Update the item
node.item = item;
Expand All @@ -353,6 +329,38 @@ public T update(int index, T item) {
return previousItem;
}

/**
* Verifies that the specified item may be placed in this list in between
* the two specified nodes by throwing an exception if the placement is not
* valid.
*
* @param item
* The item to verify
*
* @param predecessor
* The node that will become the item's predecessor, or <tt>null</tt> if
* the item is being placed at the head of the list
*
* @param successor
* The node that will become the item's successor, or <tt>null</tt> if the
* item is being placed at the tail of the list
*
* @throws IllegalArgumentException
* If the location is not valid
*/
private void verifyLocation(T item, Node<T> predecessor, Node<T> successor) {
if (comparator != null) {
// Ensure that the new item is greater or equal to its
// predecessor and less than or equal to its successor
if ((predecessor != null
&& comparator.compare(item, predecessor.item) == -1)
|| (successor != null
&& comparator.compare(item, successor.item) == 1)) {
throw new IllegalArgumentException("Illegal item modification.");
}
}
}

@Override
public int remove(T item) {
int index = 0;
Expand Down

0 comments on commit e2a6976

Please sign in to comment.