Commit a3dae1bd5fa1a635b0fe24657a028cea0c44d1d0

Authored by Adrian Chirita
1 parent b128f184

fjernet noen iterator metoder med tilhørende iterator

Signed-off-by: AdrianC <ach050@post.uit.no>
src/oblig_2/Main.java
... ... @@ -12,11 +12,6 @@ public class Main {
12 12 twoWayLinkedList.add("Vladimir");
13 13 twoWayLinkedList.add("Nikolay");
14 14 System.out.println(twoWayLinkedList.toString());
15   -
16   -
17   - Iterator itr = twoWayLinkedList.listIterator(twoWayLinkedList.size()-1);
18   - itr.next();
19   - itr.remove();
20   - System.out.println(twoWayLinkedList.toString());
  15 +
21 16 }
22 17 }
... ...
src/oblig_2/TwoWayLinkedList.java
1 1 package oblig_2;
2 2 import java.util.Iterator;
  3 +
3 4 import java.util.ListIterator;
  5 +import oblig_2.TwoWayLinkedList;
4 6  
5 7 public class TwoWayLinkedList<E> implements MyList<E> {
6 8 private Node<E> head, tail;
... ... @@ -245,14 +247,13 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
245 247 return new LinkedListIterator();
246 248 }
247 249 private class LinkedListIterator implements ListIterator<E> {
248   - private Node<E> current;
249   - int index;
  250 + private Node<E> current;
250 251 private boolean removeCalled,addCalled;
251 252  
252   - public LinkedListIterator() {
253   - index = 0;
  253 + public LinkedListIterator() {
254 254 current = head;
255   - removeCalled = false;
  255 + removeCalled = false;
  256 + addCalled = false;
256 257 }
257 258 public LinkedListIterator(int index) {
258 259 if (index < 0 || index >= size) {
... ... @@ -267,11 +268,9 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
267 268 else {
268 269 for (int i = 1; i < index; i++)
269 270 current = current.next;
270   - }
271   - this.index = index;
  271 + }
272 272 }
273   - public void setLast() {
274   - index = size;
  273 + public void setLast() {
275 274 current = tail;
276 275 }
277 276 @Override
... ... @@ -283,16 +282,12 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
283 282 if(current == null) {
284 283 throw new java.util.NoSuchElementException();
285 284 }
286   - E e = current.element;
  285 + E e = current.element;
287 286 current = current.next;
288   -
  287 +
289 288 removeCalled = false;
290 289 addCalled = false;
291 290  
292   - if (index == size) {}
293   - else
294   - index++;
295   -
296 291 return e;
297 292 }
298 293 @Override
... ... @@ -304,15 +299,12 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
304 299 if(current == null) {
305 300 throw new java.util.NoSuchElementException();
306 301 }
307   - E e = current.element;
308   - current = current.previous;
  302 + E e = current.element;
  303 + current = current.previous;
  304 +
309 305 removeCalled = false;
310 306 addCalled = false;
311 307  
312   - if (index == 0) {}
313   - else
314   - index--;
315   -
316 308 return e;
317 309 }
318 310 @Override
... ... @@ -320,59 +312,20 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
320 312 if (!addCalled && !removeCalled) {
321 313 current.element = e;
322 314 }
323   - }
324   -
325   - // Extra uimplementerte metoder
326   -
  315 + }
327 316 @Override
328 317 public int nextIndex() {
329   -
330   - if (index == size)
331   - return size;
332   - else
333   - return index+1;
  318 + return -1;
334 319 }
335 320 @Override
336 321 public int previousIndex() {
337   -
338   - if (index == 0)
339   - return -1;
340   - else
341   - return index-1;
  322 + return -1;
342 323 }
343 324 @Override
344   - public void remove() {
345   - if (current.previous == null || removeCalled || addCalled) {
346   - throw new IllegalStateException();
347   - }
348   - else if(current == null) {
349   -
350   - }
351   - else if (current.previous.previous == null) {
352   - head = current;
353   - current.previous.next = null;
354   - }
355   - else {
356   - current.previous.previous.next = current;
357   - current.previous = current.previous.previous;
358   - }
359   - size--;
360   - removeCalled = true;
  325 + public void remove() {
361 326 }
362 327 @Override
363   - public void add(E e) {
364   - if(index > 0 && index < size-1) {
365   - Node<E> newNode = new Node<>(e);
366   -
367   - current.previous.next = newNode;
368   - newNode.previous = current.previous;
369   - current.previous = newNode;
370   - newNode.next = current;
371   -
372   - index++;
373   - size++;
374   - addCalled = true;
375   - }
  328 + public void add(E e) {
376 329 }
377 330 }
378 331 private class Node<E> {
... ...
src/test/TwoWayLinkedListTest.java
... ... @@ -98,113 +98,105 @@ public class TwoWayLinkedListTest {
98 98 public void LastindexOf() {
99 99 assertEquals(4,data.lastIndexOf("Vitkat"));
100 100 assertEquals(5,data.lastIndexOf("Pie"));
101   - }
102   -
103   - @Test
104   - public void firstCallNextMethodInIteratorReturnsFirstElement() {
105   - itr = data.iterator();
106   - assertThat(itr.next(),is("Vitkat"));
107   - }
  101 + }
  102 +
108 103  
109 104 @Test
110 105 public void firstCallNextIteratorMethodReturnsFirstElement() {
111 106 data.addFirst("a");
112 107 itr = data.iterator();
113 108 assertThat(itr.next(), is("a"));
114   - }
115   -
116   - @Test
117   - public void hasNextIteratorMethodIsOk() {
118   - itr = data.listIterator(data.size()-1);
119   - assertTrue(itr.hasNext());
120   -
121   - itr.next();
122   - assertFalse(itr.hasNext());
123   -
124   - itr = data.iterator();
125   - assertTrue(itr.hasNext());
126   -
127   - data.clear();
128   - itr = data.iterator();
129   - assertFalse(itr.hasNext());
130   - }
131   - @Test
132   - public void nextIndexIteratorMethodIsOk() {
133   - itr = data.iterator();
134   - itr.next();
135   - itr.next();
136   - assertThat(((ListIterator<String>) itr).nextIndex(),is(3));
137   - }
  109 + }
138 110 @Test
139   - public void newListIteratorReturnsIteratorPointingBeforeFirstElement() {
  111 + public void defaultListIteratorReturnsIteratorPointingBeforeFirstElement() {
140 112 itr = data.listIterator();
141 113 assertThat(itr.next(),is(data.getFirst()));
142 114 }
143 115 @Test
144   - public void newListIteratorWithMaxIndexIsOk_setLastMethodIsOk() {
  116 + public void newListIteratorAtMaxIndexIsOk_setLastMethodIsOk() {
145 117 itr = data.listIterator(data.size()-1);
146   - assertThat(itr.next(),is(data.getLast()));
147   - assertThat(((ListIterator<String>) itr).nextIndex(),is(data.size()));
  118 + assertThat(itr.next(),is(data.getLast()));
148 119 }
149 120 @Test
150 121 public void newListIteratorWithValidIndexReturnsValidIterator() {
151 122 itr = data.listIterator(0);
152 123 assertThat(itr.next(),is(data.getFirst()));
153 124  
154   - itr = data.listIterator(5);
  125 + itr = data.listIterator(data.size()-1);
155 126 assertThat(itr.next(),is(data.getLast()));
156 127 }
157 128 @Test(expected = IndexOutOfBoundsException.class)
158 129 public void newListIteratorWithInvalidIndexThrowsException() {
159 130 itr = data.listIterator(6);
160 131 }
  132 + @Test
  133 + public void hasNextIteratorMethodIsOk() {
  134 + itr = data.listIterator(data.size()-1);
  135 + assertTrue(itr.hasNext());
  136 +
  137 + itr.next();
  138 + assertFalse(itr.hasNext());
  139 +
  140 + itr = data.iterator();
  141 + assertTrue(itr.hasNext());
  142 +
  143 + data.clear();
  144 + itr = data.iterator();
  145 + assertFalse(itr.hasNext());
  146 + }
  147 +
  148 + @Test(expected = java.util.NoSuchElementException.class)
  149 + public void callingNextIteratorMethodOutsideListThrowsException() {
  150 + itr = data.listIterator(data.size()-1);
  151 + itr.next();
  152 + itr.next();
  153 + }
161 154 @Test(expected = java.util.NoSuchElementException.class)
162   - public void callNextMethodFromIteratorOnEmptyListThrowsException() {
  155 + public void callingNextIteratorMethodOnEmptyListThrowsException() {
163 156 data.clear();
164 157 itr = data.iterator();
165 158 itr.next();
166 159 }
167   -
168 160 @Test
169   - public void removeIteratorMethodFromLastIndexIsOk() {
170   - String elementBeforeLastElement = data.get(data.size()-2);
171   - itr = data.listIterator(data.size()-1);
172   -
173   - itr.remove();
174   - assertThat(data.getLast(),is(elementBeforeLastElement));
  161 + public void nextIteratorMethodIsOk() {
  162 + itr = data.iterator();
  163 + itr.next();
  164 + itr.next();
  165 + assertThat(itr.next(),is(data.get(2)));
175 166 }
176 167 @Test
177   - public void removeIteratorMethodAfterFirstNextIsOk() {
178   - int initialListSize = data.size();
  168 + public void firstCallNextMethodInIteratorReturnsFirstElement() {
179 169 itr = data.iterator();
180   - itr.next();
181   - itr.remove();
182   - assertThat(data.getFirst(),is("Kitkat"));
183   - assertThat(data.size(),is(initialListSize-1));
  170 + assertThat(itr.next(),is("Vitkat"));
  171 + }
  172 + @Test(expected = java.util.NoSuchElementException.class)
  173 + public void callPreviousOnEmptyListThrowsException() {
  174 + data.clear();
  175 + itr = data.iterator();
  176 + ((ListIterator<String>)itr).previous();
184 177 }
185   - @Test(expected = IllegalStateException.class)
186   - public void callingRemoveIteratorMethodTwiceThrowsException() {
  178 + @Test(expected = java.util.NoSuchElementException.class)
  179 + public void callPreviousOutsideListThrowsException() {
187 180 itr = data.iterator();
188   - itr.next();
189   - itr.remove();
190   - itr.remove();
191   - assertThat(data.getFirst(),is("Kitkat"));
  181 + ((ListIterator<String>)itr).previous();
  182 + ((ListIterator<String>)itr).previous();
  183 + }
  184 + @Test
  185 + public void firstCallPreviousMethodFromIteratorIsOk() {
  186 + itr = data.iterator();
  187 + assertThat(((ListIterator<String>) itr).previous(),is(data.getFirst()));
192 188 }
193   - @Test(expected = IllegalStateException.class)
194   - public void callRemoveIteratorMethodAfterAddThrowsException() {
  189 + @Test
  190 + public void hasPreviousIteratorMethodIsOk() {
195 191 itr = data.iterator();
196   - itr.next();
197   - ((ListIterator<String>) itr).add("new");
198   - itr.remove();
  192 + ((ListIterator<String>)itr).previous();
  193 + assertFalse(((ListIterator<String>)itr).hasPrevious());
199 194 }
200 195 @Test
201   - public void addIteratorMethodIsOk() {
202   - int initialListSize = data.size();
  196 + public void setMethodFromIteratorIsOk() {
203 197 itr = data.iterator();
204   - itr.next();
205   - ((ListIterator<String>) itr).add("new");
206   - assertEquals(1,data.indexOf("new"));
207   - assertThat(data.size(),is(initialListSize+1));
  198 + ((ListIterator<String>)itr).set("new");
  199 + assertThat(data.getFirst(),is("new"));
208 200 }
209 201 @Test
210 202 public void toObjectArrayMethodInMyListIsOk() {
... ...