License Public Domain
Lines 86
Keywords
datastructure (1) java (9)
Permissions
Owner: jlugocp
Viewable by Everyone
Editable by All Siafoo Users
Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More

Two Way List Iterator Atom Feed 0

In Brief Wraps a ListIterator so that we don't have to worry about whether we need to proceed to the next or previous item in the iterator. This class figures that out. As long as there are items remaining in the ListIterator, hasNext() will return true. This iterator will move forward first until it reaches the end of the list. Then it will move forward until it reaches the beginning of the list. It repeats these steps until the iterator is empty.
# 's
 1package com.jorgelugo;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Iterator;
6import java.util.ListIterator;
7
8import org.junit.Assert;
9import org.junit.Test;
10
11/**
12 * Wraps a <code>ListIterator</code> so that we don't have to worry about whether we need to proceed to the
13 * next or previous item in the iterator. This class figures that out. As long as there are items remaining in
14 * the ListIterator, <code>hasNext()</code> will return true. This iterator will move forward first until it reaches the
15 * end of the list. Then it will move backwards until it reaches the beginning of the list. It repeats these
16 * steps until the iterator is empty.
17 *
18 * @author jorge
19 *
20 */
21public class TwoWayListIterator implements Iterator {
22
23 public ListIterator li;
24
25 public boolean forwardDirection = true;
26
27 public TwoWayListIterator(ListIterator li) {
28 this.li = li;
29 }
30
31 @Override
32 public boolean hasNext() {
33 boolean hasNext = false;
34 if (forwardDirection) {
35 hasNext = li.hasNext();
36 if (!hasNext) {
37 hasNext = li.hasPrevious();
38 forwardDirection = false;
39 }
40 } else {
41 hasNext = li.hasPrevious();
42 if (!hasNext) {
43 hasNext = li.hasNext();
44 forwardDirection = true;
45 }
46 }
47 return hasNext;
48 }
49
50 @Override
51 public Object next() {
52 if (forwardDirection) {
53 return li.next();
54 } else {
55 return li.previous();
56 }
57 }
58
59 @Override
60 public void remove() {
61 li.remove();
62 }
63}
64
65class TestTwoWayListIterator {
66
67 @Test
68 public void test(){
69 List<Integer> list = new ArrayList<Integer>();
70 list.add(1);
71 list.add(2);
72 list.add(3);
73
74 ListIterator<Integer> li = list.listIterator();
75 TwoWayListIterator it = new TwoWayListIterator(li);
76
77 Assert.assertTrue(it.hasNext());
78 Integer i = (Integer)it.next();
79 Assert.assertEquals(new Integer(1), i);
80 it.remove();
81
82 Assert.assertTrue(it.hasNext());
83 i = (Integer)it.next();
84 Assert.assertEquals(new Integer(2), i);
85 it.remove();
86
87 Assert.assertTrue(it.hasNext());
88 i = (Integer)it.next();
89 Assert.assertEquals(new Integer(3), i);
90 //li.remove();
91
92 Assert.assertTrue(it.hasNext());
93 i = (Integer)it.next();
94 Assert.assertEquals(new Integer(3), i);
95 it.remove();
96
97 Assert.assertFalse(it.hasNext());
98 }
99}

Wraps a ListIterator so that we don't have to worry about whether we need to proceed to the next or previous item in the iterator. This class figures that out. As long as there are items remaining in the ListIterator, hasNext() will return true. This iterator will move forward first until it reaches the end of the list. Then it will move forward until it reaches the beginning of the list. It repeats these steps until the iterator is empty.