From 4082c6389a63692eae3136f35b12f87386991dbb Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 8 Oct 2015 19:59:38 +0300 Subject: [PATCH] Fix LinkedList.push and .element methods --- .../java/org/teavm/classlib/java/util/TLinkedList.java | 9 ++++++--- .../org/teavm/classlib/java/util/LinkedListTest.java | 9 +++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java b/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java index cdf096d66..ff98a6b97 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java @@ -140,7 +140,10 @@ public class TLinkedList extends TAbstractSequentialList implements TDeque @Override public E element() { - return null; + if (firstEntry == null) { + throw new TNoSuchElementException(); + } + return firstEntry.item; } @Override @@ -273,12 +276,12 @@ public class TLinkedList extends TAbstractSequentialList implements TDeque @Override public void push(E e) { - add(e); + addFirst(e); } @Override public E pop() { - return removeLast(); + return removeFirst(); } @Override diff --git a/tests/src/test/java/org/teavm/classlib/java/util/LinkedListTest.java b/tests/src/test/java/org/teavm/classlib/java/util/LinkedListTest.java index 7457810ca..9e69bb374 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/LinkedListTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/LinkedListTest.java @@ -214,4 +214,13 @@ public class LinkedListTest { assertEquals(4, list.size()); assertArrayEquals(new String[] { "1", "2", "3", "1" }, list.toArray(new String[0])); } + + @Test + public void pushes() { + LinkedList list = new LinkedList<>(); + list.push("foo"); + assertEquals("foo", list.peek()); + list.push("bar"); + assertEquals("bar", list.peek()); + } }