Commit b128f18444b29e0f0cb7b776856342c60d426cd9

Authored by Adrian Chirita
1 parent 5db3514e

flere mods, må fjerne en del metoder i neste ver.

src/oblig_2/Main.java
1 1 package oblig_2;
2 2  
  3 +import java.util.Iterator;
  4 +
3 5 public class Main {
4 6  
5 7 public static void main(String[] args) {
6 8 TwoWayLinkedList<String> twoWayLinkedList = new TwoWayLinkedList<>();
7   - twoWayLinkedList.addLast("Alexey");
8   - twoWayLinkedList.addFirst("Oleg");
9   - twoWayLinkedList.addFirst("Andrii");
10   - twoWayLinkedList.addFirst("Vladimir");
11   - twoWayLinkedList.addLast("Nikolay");
  9 + twoWayLinkedList.add("Alexey");
  10 + twoWayLinkedList.add("Oleg");
  11 + twoWayLinkedList.add("Andrii");
  12 + twoWayLinkedList.add("Vladimir");
  13 + twoWayLinkedList.add("Nikolay");
  14 + System.out.println(twoWayLinkedList.toString());
  15 +
  16 +
  17 + Iterator itr = twoWayLinkedList.listIterator(twoWayLinkedList.size()-1);
  18 + itr.next();
  19 + itr.remove();
12 20 System.out.println(twoWayLinkedList.toString());
13   - System.out.println(twoWayLinkedList.contains("Nikolay"));
14   -
15   - Object[] arrtest = twoWayLinkedList.toArray();
16   - for (int i= 0; i< arrtest.length;i++)
17   - System.out.println(arrtest[i]);
18   -
19 21 }
20 22 }
... ...
src/oblig_2/TwoWayLinkedList.java
... ... @@ -5,11 +5,11 @@ import java.util.ListIterator;
5 5 public class TwoWayLinkedList<E> implements MyList<E> {
6 6 private Node<E> head, tail;
7 7 private int size;
8   -
  8 +
9 9 public TwoWayLinkedList() {
10   - size = 0;
11   - head = null;
  10 + head = null;
12 11 tail = null;
  12 + size = 0;
13 13 }
14 14 public TwoWayLinkedList(E[] objects) {
15 15 for (int i = 0; i < objects.length; i++)
... ... @@ -153,8 +153,9 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
153 153 return result.toString();
154 154 }
155 155 public void clear() {
156   - size = 0;
157   - head = tail = null;
  156 + head = null;
  157 + tail = null;
  158 + size = 0;
158 159 }
159 160 public boolean contains(Object e) {
160 161 return indexOf(e) >= 0 ? true:false ;
... ... @@ -231,25 +232,19 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
231 232 }
232 233 }
233 234 public ListIterator<E> listIterator() {
234   - return new LinkedListIterator();
  235 + return listIterator(0);
235 236 }
236 237 public ListIterator<E> listIterator(int index) {
237 238 if (index < 0 || index >= size) {
238 239 throw new IndexOutOfBoundsException();
239   - }
240   - /*ListIterator<E> twoWayLinkedListIterator = new LinkedListIterator(index);
241   - for(int i = 0; i < index; i++) {
242   - twoWayLinkedListIterator.next();
243   - }
244   - return twoWayLinkedListIterator;
245   - */
  240 + }
246 241 return new LinkedListIterator(index);
247 242 }
248 243 @Override
249 244 public Iterator<E> iterator() {
250 245 return new LinkedListIterator();
251   - }
252   - private class LinkedListIterator implements java.util.ListIterator<E> {
  246 + }
  247 + private class LinkedListIterator implements ListIterator<E> {
253 248 private Node<E> current;
254 249 int index;
255 250 private boolean removeCalled,addCalled;
... ... @@ -267,7 +262,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
267 262 current = head;
268 263 }
269 264 else if(index == size-1) {
270   - current = tail;
  265 + setLast();
271 266 }
272 267 else {
273 268 for (int i = 1; i < index; i++)
... ... @@ -276,6 +271,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
276 271 this.index = index;
277 272 }
278 273 public void setLast() {
  274 + index = size;
279 275 current = tail;
280 276 }
281 277 @Override
... ... @@ -289,9 +285,14 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
289 285 }
290 286 E e = current.element;
291 287 current = current.next;
292   - removeCalled = false;
  288 +
  289 + removeCalled = false;
293 290 addCalled = false;
294   - index++;
  291 +
  292 + if (index == size) {}
  293 + else
  294 + index++;
  295 +
295 296 return e;
296 297 }
297 298 @Override
... ... @@ -307,7 +308,11 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
307 308 current = current.previous;
308 309 removeCalled = false;
309 310 addCalled = false;
310   - index--;
  311 +
  312 + if (index == 0) {}
  313 + else
  314 + index--;
  315 +
311 316 return e;
312 317 }
313 318 @Override
... ... @@ -321,30 +326,28 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
321 326  
322 327 @Override
323 328 public int nextIndex() {
324   - //if (!hasNext())
325   - // return size;
326   - if (index == size-1) {
  329 +
  330 + if (index == size)
327 331 return size;
328   - }
329 332 else
330 333 return index+1;
331 334 }
332 335 @Override
333 336 public int previousIndex() {
334   - //if (!hasPrevious())
335   - // return -1;
336   - if (index == 0) {
  337 +
  338 + if (index == 0)
337 339 return -1;
338   - }
339   - else {
340   - return index-1;
341   - }
  340 + else
  341 + return index-1;
342 342 }
343 343 @Override
344 344 public void remove() {
345   - if (current.previous == null || removeCalled) {
  345 + if (current.previous == null || removeCalled || addCalled) {
346 346 throw new IllegalStateException();
347 347 }
  348 + else if(current == null) {
  349 +
  350 + }
348 351 else if (current.previous.previous == null) {
349 352 head = current;
350 353 current.previous.next = null;
... ... @@ -357,27 +360,29 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
357 360 removeCalled = true;
358 361 }
359 362 @Override
360   - public void add(E e) {
361   -
362   - Node<E> newNode = new Node<>(e);
363   - current.previous.next = newNode;
364   - newNode.previous = current.previous;
365   - current.previous = newNode;
366   - newNode.next = current;
  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;
367 371  
368   - index++;
369   - size++;
370   - addCalled = true;
  372 + index++;
  373 + size++;
  374 + addCalled = true;
  375 + }
371 376 }
372   - }
  377 + }
373 378 private class Node<E> {
374   - E element;
375   - Node<E> next;
376   - Node<E> previous;
  379 + E element;
  380 + Node<E> next;
  381 + Node<E> previous;
377 382  
378   - public Node(E o) {
379   - element = o;
380   - }
  383 + public Node(E o) {
  384 + element = o;
  385 + }
381 386 }
382 387 @Override
383 388 public int size() {
... ...
src/test/TwoWayLinkedListTest.java
1 1 package test;
2 2  
3 3 import java.util.Iterator;
  4 +import java.util.ListIterator;
  5 +
4 6 import static org.junit.Assert.*;
5 7 import static org.hamcrest.CoreMatchers.*;
6 8 import org.junit.Before;
... ... @@ -16,7 +18,7 @@ public class TwoWayLinkedListTest {
16 18  
17 19 @Before
18 20 public void setUp(){
19   - data = new TwoWayLinkedList<>();
  21 + data = new TwoWayLinkedList<String>();
20 22 data.add("Vitkat");
21 23 data.add("Kitkat");
22 24 data.add("Pie");
... ... @@ -97,32 +99,113 @@ public class TwoWayLinkedListTest {
97 99 assertEquals(4,data.lastIndexOf("Vitkat"));
98 100 assertEquals(5,data.lastIndexOf("Pie"));
99 101 }
  102 +
100 103 @Test
101 104 public void firstCallNextMethodInIteratorReturnsFirstElement() {
102 105 itr = data.iterator();
103 106 assertThat(itr.next(),is("Vitkat"));
104 107 }
105   - @Test(expected = IllegalStateException.class)
106   - public void callingRemoveMethodTwiceFromIteratorThrowsException() {
107   - itr = data.iterator();
108   - itr.next();
109   - itr.remove();
110   - itr.remove();
111   - assertThat(data.getFirst(),is("Kitkat"));
112   - }
  108 +
113 109 @Test
114 110 public void firstCallNextIteratorMethodReturnsFirstElement() {
115 111 data.addFirst("a");
116   - itr = data.iterator();
117   - String result = itr.next();
118   - assertThat(result, is("a"));
  112 + itr = data.iterator();
  113 + 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 + }
  138 + @Test
  139 + public void newListIteratorReturnsIteratorPointingBeforeFirstElement() {
  140 + itr = data.listIterator();
  141 + assertThat(itr.next(),is(data.getFirst()));
  142 + }
  143 + @Test
  144 + public void newListIteratorWithMaxIndexIsOk_setLastMethodIsOk() {
  145 + itr = data.listIterator(data.size()-1);
  146 + assertThat(itr.next(),is(data.getLast()));
  147 + assertThat(((ListIterator<String>) itr).nextIndex(),is(data.size()));
  148 + }
  149 + @Test
  150 + public void newListIteratorWithValidIndexReturnsValidIterator() {
  151 + itr = data.listIterator(0);
  152 + assertThat(itr.next(),is(data.getFirst()));
  153 +
  154 + itr = data.listIterator(5);
  155 + assertThat(itr.next(),is(data.getLast()));
  156 + }
  157 + @Test(expected = IndexOutOfBoundsException.class)
  158 + public void newListIteratorWithInvalidIndexThrowsException() {
  159 + itr = data.listIterator(6);
119 160 }
120 161 @Test(expected = java.util.NoSuchElementException.class)
121   - public void callNextIteratorMethodOnEmptyListThrowsException() {
  162 + public void callNextMethodFromIteratorOnEmptyListThrowsException() {
122 163 data.clear();
123 164 itr = data.iterator();
124 165 itr.next();
125 166 }
  167 +
  168 + @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));
  175 + }
  176 + @Test
  177 + public void removeIteratorMethodAfterFirstNextIsOk() {
  178 + int initialListSize = data.size();
  179 + itr = data.iterator();
  180 + itr.next();
  181 + itr.remove();
  182 + assertThat(data.getFirst(),is("Kitkat"));
  183 + assertThat(data.size(),is(initialListSize-1));
  184 + }
  185 + @Test(expected = IllegalStateException.class)
  186 + public void callingRemoveIteratorMethodTwiceThrowsException() {
  187 + itr = data.iterator();
  188 + itr.next();
  189 + itr.remove();
  190 + itr.remove();
  191 + assertThat(data.getFirst(),is("Kitkat"));
  192 + }
  193 + @Test(expected = IllegalStateException.class)
  194 + public void callRemoveIteratorMethodAfterAddThrowsException() {
  195 + itr = data.iterator();
  196 + itr.next();
  197 + ((ListIterator<String>) itr).add("new");
  198 + itr.remove();
  199 + }
  200 + @Test
  201 + public void addIteratorMethodIsOk() {
  202 + int initialListSize = data.size();
  203 + 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));
  208 + }
126 209 @Test
127 210 public void toObjectArrayMethodInMyListIsOk() {
128 211 Object[] dataArray = data.toArray();
... ...