From 953b629264ccd4fc4e736b5b4f81bc8f639da4ce Mon Sep 17 00:00:00 2001 From: Theophile Morin Date: Thu, 12 Jun 2014 13:57:37 +0200 Subject: [PATCH 1/6] Removed abstract classes. --- ...t.java => ImmutableBaseInductiveList.java} | 22 +++++++++++- ...t.java => ImmutableBaseIterativeList.java} | 35 ++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) rename Immutable_collections_java8_project/src/collections/implementations/{ImmutableAbstractInductiveList.java => ImmutableBaseInductiveList.java} (75%) rename Immutable_collections_java8_project/src/collections/implementations/{ImmutableAbstractIterativeList.java => ImmutableBaseIterativeList.java} (52%) diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractInductiveList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java similarity index 75% rename from Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractInductiveList.java rename to Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java index 0de4cb0..64b2fc3 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractInductiveList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java @@ -1,11 +1,13 @@ package collections.implementations; +import java.util.Collection; +import java.util.Iterator; import java.util.NoSuchElementException; import collections.interfaces.ImmutableCoreList; import collections.interfaces.InductiveList; -public abstract class ImmutableAbstractInductiveList implements InductiveList { +public class ImmutableBaseInductiveList implements InductiveList { @Override public boolean isEmpty() { @@ -46,4 +48,22 @@ public InductiveList tail() throws UnsupportedOperationException { //TODO ERR public E last() throws NoSuchElementException { //TODO implementation class call an accessor method, check it. return null; } + + @Override + public ImmutableCoreList create(E[] elems) { + // TODO Auto-generated method stub + return null; + } + + @Override + public ImmutableCoreList create(Collection elems) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Iterator iterator() { + // TODO Auto-generated method stub + return null; + } } diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractIterativeList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java similarity index 52% rename from Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractIterativeList.java rename to Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java index 89736c9..36913af 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractIterativeList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java @@ -1,9 +1,12 @@ package collections.implementations; +import java.util.Collection; +import java.util.Iterator; + import collections.interfaces.ImmutableCoreList; import collections.interfaces.IterativeList; -public abstract class ImmutableAbstractIterativeList implements IterativeList +public class ImmutableBaseIterativeList implements IterativeList { @Override @@ -26,4 +29,34 @@ public int hashCode() { return ImmutableCoreList.hashCode(this); //TODO check "this" validity (abstract class) } + @Override + public ImmutableCoreList create(E[] elems) { + // TODO Auto-generated method stub + return null; + } + + @Override + public ImmutableCoreList create(Collection elems) { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean isEmpty() { + // TODO Auto-generated method stub + return false; + } + + @Override + public Iterator iterator() { + // TODO Auto-generated method stub + return null; + } + + @Override + public int size() { + // TODO Auto-generated method stub + return 0; + } + } From d3a7d84b2fcc740de5a7f8e2c9570a607f1f2826 Mon Sep 17 00:00:00 2001 From: Thibault Date: Thu, 12 Jun 2014 15:01:01 +0200 Subject: [PATCH 2/6] Implementation de BaseInductive --- .../ImmutableBaseInductiveList.java | 174 ++++++++++++++++-- .../ImmutableBaseIterativeList.java | 1 + .../implementations/ImmutableLinkedList.java | 110 ++--------- 3 files changed, 178 insertions(+), 107 deletions(-) diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java index 64b2fc3..f3d5032 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java @@ -6,12 +6,87 @@ import collections.interfaces.ImmutableCoreList; import collections.interfaces.InductiveList; +class Node { + + /** The element in the list */ + private final E element; + + /** The next list node, null if this is last */ + private final Node next; + + public Node(E element, Node next) { + this.element=element; + this.next=next; + } + + public Node(E element) { + this(element, null); + } + + /** + * Returns the element in the node. + * + * @returns the element in the node. + */ + public E getElement() { + return this.element; + } + + /** Returns the next list node, null if this is last. + * + * @returns the next list node, null if this is last + */ + public Node getNext() { + return next; + } + + /** Returns whether this node is followed by another one. + * + * @returns true if this node is followed by another one. + */ + public boolean hasNext() { + return getNext() != null; + } +} public class ImmutableBaseInductiveList implements InductiveList { + + /** The first node element of the list. */ + private final Node head; - @Override - public boolean isEmpty() { - return false; //TODO find why this method is not implemented in concrete class (ImmutableLinkedList). + /** The last node element of the list. */ + private final Node last; + + /** The number of elements in this list. */ + private final int size; + + + public ImmutableBaseInductiveList(Node head, Node last, int size) { + this.head = head; + this.last = last; + this.size = size; + } + + /** + * Returns the first node element of the list. + * + * @returns the first node element of the list. + **/ + private Node headNode() { + return head; + } + + /** + * Returns the last node element of the list. + * + * @returns the last node element of the list. + **/ + private Node lastNode() { + return last; + } + + private int size() { + return size; } @Override @@ -31,39 +106,106 @@ public int hashCode() { @Override public InductiveList cons(E elem) { - return null; // new ImmutableLinkedList(new Node(elem, head()), last(), size() + 1); //TODO add size in Core ? + return new ImmutableBaseInductiveList(new Node(elem, headNode()), + lastNode(), + size() + 1); } @Override - public E head() throws NoSuchElementException { //TODO implementation class call an accessor method, check it. - return null; + public E head() throws NoSuchElementException { + if (isEmpty()) + throw new NoSuchElementException(); + else + return headNode().getElement(); } + @SuppressWarnings("unchecked") @Override - public InductiveList tail() throws UnsupportedOperationException { //TODO ERROR : check why tail is not implemented in the implementation class... - return null; + public InductiveList tail() { + + int i = 1; + Node node = headNode().getNext(); + Node subListHead = headNode().getNext(); + + while (i != this.size-1) { + node = node.getNext(); + ++i; + } + Node subListLast = node; + + return new ImmutableBaseInductiveList(subListHead, + subListLast, + this.size - 1); } @Override - public E last() throws NoSuchElementException { //TODO implementation class call an accessor method, check it. - return null; + public E last() throws NoSuchElementException { + if (isEmpty()) + throw new NoSuchElementException(); + else + return lastNode().getElement(); } @Override public ImmutableCoreList create(E[] elems) { - // TODO Auto-generated method stub - return null; + return new ImmutableLinkedList(elems); } @Override public ImmutableCoreList create(Collection elems) { - // TODO Auto-generated method stub - return null; + return new ImmutableLinkedList(elems); } @Override public Iterator iterator() { - // TODO Auto-generated method stub - return null; + return new ImmutableLinkedListIterator(); + } + + @Override + public boolean isEmpty() { + return head==null; + } + + // Iterators & streams + + class ImmutableLinkedListIterator implements Iterator { + + /** Current node pointed by the iterator */ + private Node currentNode; + + /** Tell whether the iterator can continue or not */ + private boolean hasNext; + + /** + * Create a new iterator starting from the beginning of the linked list. + */ + public ImmutableLinkedListIterator() { + currentNode = headNode(); + hasNext = (size() != 0); + } + + public boolean hasNext() { + return hasNext; + } + + public E next() throws NoSuchElementException { + if (!hasNext()) + throw new NoSuchElementException(); + + E elem = currentNode.getElement(); + if (currentNode == lastNode()) + hasNext = false; + else + currentNode = currentNode.getNext(); + return elem; + } + + public void remove() throws + UnsupportedOperationException, + IllegalStateException { + throw new UnsupportedOperationException(); + } } } + + diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java index 36913af..5f552ab 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java @@ -6,6 +6,7 @@ import collections.interfaces.ImmutableCoreList; import collections.interfaces.IterativeList; + public class ImmutableBaseIterativeList implements IterativeList { diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java index 2013b09..c7ee6df 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java @@ -6,51 +6,9 @@ import collections.interfaces.ImmutableCoreList; import collections.interfaces.ImmutableList; +import collections.interfaces.InductiveList; import collections.interfaces.IterativeList; -class Node { - - /** The element in the list */ - private final E element; - - /** The next list node, null if this is last */ - private final Node next; - - public Node(E element, Node next) { - this.element=element; - this.next=next; - } - - public Node(E element) { - this(element, null); - } - - /** - * Returns the element in the node. - * - * @returns the element in the node. - */ - public E getElement() { - return this.element; - } - - /** Returns the next list node, null if this is last. - * - * @returns the next list node, null if this is last - */ - public Node getNext() { - return next; - } - - /** Returns whether this node is followed by another one. - * - * @returns true if this node is followed by another one. - */ - public boolean hasNext() { - return getNext() != null; - } -} - public class ImmutableLinkedList implements ImmutableList { /** The first node element of the list. */ @@ -244,60 +202,30 @@ public ImmutableLinkedList remove(int index) { return new ImmutableLinkedList(newElems); } - // Iterators & streams - - class ImmutableLinkedListIterator implements Iterator { - - /** Current node pointed by the iterator */ - private Node currentNode; - - /** Tell whether the iterator can continue or not */ - private boolean hasNext; - - /** - * Create a new iterator starting from the beginning of the linked list. - */ - public ImmutableLinkedListIterator() { - currentNode = headNode(); - hasNext = (size() != 0); - } - - public boolean hasNext() { - return hasNext; - } - - public E next() throws NoSuchElementException { - if (!hasNext()) - throw new NoSuchElementException(); - - E elem = currentNode.getElement(); - if (currentNode == lastNode()) - hasNext = false; - else - currentNode = currentNode.getNext(); - return elem; - } - - public void remove() throws - UnsupportedOperationException, - IllegalStateException { - throw new UnsupportedOperationException(); - } + @Override + public ImmutableList tail() throws UnsupportedOperationException { + // TODO Auto-generated method stub + return null; } - public Iterator iterator() { - return new ImmutableLinkedListIterator(); + @Override + public boolean isEmpty() { + // TODO Auto-generated method stub + return false; } - public int hashCode() { - return ImmutableCoreList.hashCode(this); //TODO check call to first background interface while ImmutableList + @Override + public ImmutableCoreList clone() { + // TODO Auto-generated method stub + return null; } - public ImmutableList clone() { - return ImmutableList.clone(this); + @Override + public Iterator iterator() { + // TODO Auto-generated method stub + return null; } - public boolean equals(Object o) { - return IterativeList.equals(this, o); - } + + } From c92765eb4ba35df5a18f81b9ba54bc8cf0ec8e60 Mon Sep 17 00:00:00 2001 From: Thibault Date: Thu, 12 Jun 2014 15:09:34 +0200 Subject: [PATCH 3/6] Implementation BaseIterative --- .../ImmutableBaseIterativeList.java | 75 +++++++++++++++---- .../implementations/ImmutableLinkedList.java | 67 ++++++++++++----- 2 files changed, 108 insertions(+), 34 deletions(-) diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java index 5f552ab..6e554da 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java @@ -2,6 +2,7 @@ import java.util.Collection; import java.util.Iterator; +import java.util.NoSuchElementException; import collections.interfaces.ImmutableCoreList; import collections.interfaces.IterativeList; @@ -9,55 +10,99 @@ public class ImmutableBaseIterativeList implements IterativeList { + + private final E[] _array; + private final int _length; + + @SuppressWarnings("unchecked") + public ImmutableBaseIterativeList() + { + _array = (E[]) new Object[0]; + _length = 0; + } @Override public E get(int index) throws IndexOutOfBoundsException { - return null; //TODO refers to the implementation class + if (index < 0 || index >= size()) + throw new IndexOutOfBoundsException(); + if(index >=0 && index < size()) + return _array[index]; + else + return null; } @Override public boolean equals(Object o) { - return IterativeList.equals(this, o); //TODO check "this" validity (abstract class) + return IterativeList.equals(this, o); } @Override - public ImmutableCoreList clone() { //TODO check static returned type - return ImmutableCoreList.clone(this); //TODO check "this" validity (abstract class) + public ImmutableCoreList clone() { + return ImmutableCoreList.clone(this); } @Override public int hashCode() { - return ImmutableCoreList.hashCode(this); //TODO check "this" validity (abstract class) + return ImmutableCoreList.hashCode(this); } @Override public ImmutableCoreList create(E[] elems) { - // TODO Auto-generated method stub - return null; + return new ImmutableArrayList(elems); } @Override public ImmutableCoreList create(Collection elems) { - // TODO Auto-generated method stub - return null; + return new ImmutableArrayList(elems); } @Override public boolean isEmpty() { - // TODO Auto-generated method stub - return false; + return this._length==0; } @Override public Iterator iterator() { - // TODO Auto-generated method stub - return null; + return new ImmutableArrayListIterator(); } @Override public int size() { - // TODO Auto-generated method stub - return 0; + return _length; } + class ImmutableArrayListIterator implements Iterator { + + /** Current node pointed by the iterator */ + private int index; + private int size; + + /** + * Create a new iterator starting from the beginning of the list. + */ + public ImmutableArrayListIterator() { + index = 0; + size = size(); + } + + public boolean hasNext() { + return index <= size-1 ? true : false; + } + + public E next() throws NoSuchElementException { + if(index >= size) + throw new NoSuchElementException(); + + E elem = _array[index]; + ++index; + return elem; + } + + public void remove() throws + UnsupportedOperationException, + IllegalStateException { + throw new UnsupportedOperationException(); + } + } + } diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java index c7ee6df..8da54cf 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java @@ -6,7 +6,6 @@ import collections.interfaces.ImmutableCoreList; import collections.interfaces.ImmutableList; -import collections.interfaces.InductiveList; import collections.interfaces.IterativeList; public class ImmutableLinkedList implements ImmutableList { @@ -202,30 +201,60 @@ public ImmutableLinkedList remove(int index) { return new ImmutableLinkedList(newElems); } - @Override - public ImmutableList tail() throws UnsupportedOperationException { - // TODO Auto-generated method stub - return null; - } + // Iterators & streams - @Override - public boolean isEmpty() { - // TODO Auto-generated method stub - return false; - } + class ImmutableLinkedListIterator implements Iterator { + + /** Current node pointed by the iterator */ + private Node currentNode; + + /** Tell whether the iterator can continue or not */ + private boolean hasNext; + + /** + * Create a new iterator starting from the beginning of the linked list. + */ + public ImmutableLinkedListIterator() { + currentNode = headNode(); + hasNext = (size() != 0); + } + + public boolean hasNext() { + return hasNext; + } - @Override - public ImmutableCoreList clone() { - // TODO Auto-generated method stub - return null; + public E next() throws NoSuchElementException { + if (!hasNext()) + throw new NoSuchElementException(); + + E elem = currentNode.getElement(); + if (currentNode == lastNode()) + hasNext = false; + else + currentNode = currentNode.getNext(); + return elem; + } + + public void remove() throws + UnsupportedOperationException, + IllegalStateException { + throw new UnsupportedOperationException(); + } } - @Override public Iterator iterator() { - // TODO Auto-generated method stub - return null; + return new ImmutableLinkedListIterator(); } + public int hashCode() { + return ImmutableCoreList.hashCode(this); //TODO check call to first background interface while ImmutableList + } + + public ImmutableList clone() { + return ImmutableList.clone(this); + } - + public boolean equals(Object o) { + return IterativeList.equals(this, o); + } } From e61af35a1260caf102e55bbf802037e954c8ceaa Mon Sep 17 00:00:00 2001 From: Theophile Morin Date: Thu, 12 Jun 2014 16:19:09 +0200 Subject: [PATCH 4/6] ImmutableLinkedList refactoring --- .../ImmutableBaseInductiveList.java | 169 +++++++++++++----- .../implementations/ImmutableLinkedList.java | 136 ++++---------- 2 files changed, 154 insertions(+), 151 deletions(-) diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java index f3d5032..805ee5c 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java @@ -6,6 +6,9 @@ import collections.interfaces.ImmutableCoreList; import collections.interfaces.InductiveList; + + + class Node { /** The element in the list */ @@ -49,30 +52,87 @@ public boolean hasNext() { } } -public class ImmutableBaseInductiveList implements InductiveList { - +public abstract class ImmutableBaseInductiveList implements InductiveList { + /** The first node element of the list. */ - private final Node head; + protected final Node head; /** The last node element of the list. */ - private final Node last; - + protected final Node last; + /** The number of elements in this list. */ - private final int size; + protected final int size; + /** + * Internal constructor which create a linked list given its attributes. + * + * @param head The first node of the list + * @param last The last node of the list + * @param size The size of the list + */ public ImmutableBaseInductiveList(Node head, Node last, int size) { this.head = head; this.last = last; this.size = size; } - + + /** + * Create a empty linked list. + */ + public ImmutableBaseInductiveList() { + this(null, null, 0); + } + + /** + * Create a linked list containing the given elements in order. + * + * @param elems collection of elements to populate this list from + * @throws NullPointerException if elems is null + */ + @SuppressWarnings("unchecked") + public ImmutableBaseInductiveList(Collection elems) { + this((E[])elems.toArray()); + } + + /** + * Create a linked list containing the given elements in order. + * + * @param elems the elements to populate this list from + * @throws NullPointerException if elems is null + */ + @SuppressWarnings({"unchecked"}) + public ImmutableBaseInductiveList(E... elems) { + if (elems == null) + throw new NullPointerException(); + + if (elems.length == 0) { + this.head = null; + this.last = null; + this.size = 0; + return; + } + + Node head = new Node(elems[elems.length - 1]); + this.last = head; + + for (int i = elems.length - 2 ; i >=0 ; --i) + head = new Node(elems[i], head); + + this.head = head; + this.size = elems.length; + } + + + + + /** * Returns the first node element of the list. * * @returns the first node element of the list. **/ - private Node headNode() { + protected Node headNode() { return head; } @@ -81,36 +141,36 @@ private Node headNode() { * * @returns the last node element of the list. **/ - private Node lastNode() { + protected Node lastNode() { return last; } - private int size() { + protected int size() { return size; } - + @Override - public ImmutableCoreList clone() { //TODO check static returned type. - return ImmutableCoreList.clone(this); //TODO check call + public InductiveList clone() { + return (InductiveList) ImmutableCoreList.clone(this); } - + @Override public boolean equals(Object o) { - return ImmutableCoreList.equals(this, o);//TODO check call to first background interface while InductiveList + return ImmutableCoreList.equals(this, o); } - + @Override public int hashCode() { - return ImmutableCoreList.hashCode(this); //TODO check call to first background interface while InductiveList + return ImmutableCoreList.hashCode(this); } - + @Override public InductiveList cons(E elem) { - return new ImmutableBaseInductiveList(new Node(elem, headNode()), - lastNode(), - size() + 1); + return create(new Node(elem, headNode()), + lastNode(), + size() + 1); } - + @Override public E head() throws NoSuchElementException { if (isEmpty()) @@ -118,26 +178,45 @@ public E head() throws NoSuchElementException { else return headNode().getElement(); } - - @SuppressWarnings("unchecked") - @Override - public InductiveList tail() { - int i = 1; - Node node = headNode().getNext(); - Node subListHead = headNode().getNext(); - while (i != this.size-1) { - node = node.getNext(); - ++i; + @Override + public InductiveList tail() { + if (isEmpty()) + throw new UnsupportedOperationException(); + else + { + final int fromIndex = 1; + final int toIndex = size(); + if (fromIndex < 0 || toIndex > size()) + throw new IndexOutOfBoundsException(); + if (fromIndex > toIndex) + throw new IllegalArgumentException(); + if (fromIndex == toIndex) + return new ImmutableLinkedList(); + + int i = 0; + Node node = headNode(); + while (i != fromIndex) { + node = node.getNext(); + ++i; + } + Node subListHead = node; + + while (i != toIndex-1) { + node = node.getNext(); + ++i; + } + Node subListLast = node; + + return create(subListHead, + subListLast, + toIndex - fromIndex); } - Node subListLast = node; - - return new ImmutableBaseInductiveList(subListHead, - subListLast, - this.size - 1); } - + + public abstract ImmutableLinkedList create(Node from, Node head, int size); + @Override public E last() throws NoSuchElementException { if (isEmpty()) @@ -146,29 +225,23 @@ public E last() throws NoSuchElementException { return lastNode().getElement(); } - @Override - public ImmutableCoreList create(E[] elems) { - return new ImmutableLinkedList(elems); - } - @Override - public ImmutableCoreList create(Collection elems) { - return new ImmutableLinkedList(elems); - } @Override public Iterator iterator() { return new ImmutableLinkedListIterator(); } + + @Override public boolean isEmpty() { return head==null; } - + // Iterators & streams - class ImmutableLinkedListIterator implements Iterator { + class ImmutableLinkedListIterator implements Iterator { //TODO change name ? /** Current node pointed by the iterator */ private Node currentNode; diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java index c7ee6df..6db34c4 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java @@ -1,46 +1,12 @@ package collections.implementations; import java.util.Collection; -import java.util.Iterator; -import java.util.NoSuchElementException; -import collections.interfaces.ImmutableCoreList; import collections.interfaces.ImmutableList; -import collections.interfaces.InductiveList; -import collections.interfaces.IterativeList; -public class ImmutableLinkedList implements ImmutableList { +public class ImmutableLinkedList extends ImmutableBaseInductiveList implements ImmutableList{ - /** The first node element of the list. */ - private final Node head; - /** The last node element of the list. */ - private final Node last; - - /** The number of elements in this list. */ - private final int size; - - /** - * Returns the first node element of the list. - * - * @returns the first node element of the list. - **/ - private Node headNode() { - return head; - } - - /** - * Returns the last node element of the list. - * - * @returns the last node element of the list. - **/ - private Node lastNode() { - return last; - } - - public int size() { - return size; - } // Constructors @@ -52,9 +18,7 @@ public int size() { * @param size The size of the list */ private ImmutableLinkedList(Node head, Node last, int size) { - this.head = head; - this.last = last; - this.size = size; + super(head, last, size); } /** @@ -71,7 +35,7 @@ public ImmutableLinkedList() { * @throws NullPointerException if elems is null */ @SuppressWarnings("unchecked") - public ImmutableLinkedList(Collection elems) { + public ImmutableLinkedList(Collection elems) { this((E[])elems.toArray()); } @@ -83,36 +47,36 @@ public ImmutableLinkedList(Collection elems) { */ @SuppressWarnings({"unchecked"}) public ImmutableLinkedList(E... elems) { - if (elems == null) - throw new NullPointerException(); - - if (elems.length == 0) { - this.head = null; - this.last = null; - this.size = 0; - return; - } - - Node head = new Node(elems[elems.length - 1]); - this.last = head; + super(elems); + } - for (int i = elems.length - 2 ; i >=0 ; --i) - head = new Node(elems[i], head); - this.head = head; - this.size = elems.length; - } + @Override public ImmutableLinkedList create(E[] elems) { return new ImmutableLinkedList(elems); } + @Override public ImmutableLinkedList create(Collection elems) { return new ImmutableLinkedList(elems); } + @Override + public ImmutableLinkedList create(Node from, Node head, int size){ + return new ImmutableLinkedList(from, head, size); + } + + + // Operations + + public int size() { + return size; + } + + public E get(int index) throws IndexOutOfBoundsException { if (index < 0 || index >= size()) throw new IndexOutOfBoundsException(); @@ -127,23 +91,9 @@ public E get(int index) throws IndexOutOfBoundsException { return null; // Never happens } - public E head() throws NoSuchElementException { - if (isEmpty()) - throw new NoSuchElementException(); - else - return headNode().getElement(); - } - - public E last() throws NoSuchElementException { - if (isEmpty()) - throw new NoSuchElementException(); - else - return lastNode().getElement(); - } - public ImmutableLinkedList subList(int fromIndex, int toIndex) throws - IndexOutOfBoundsException, - IllegalArgumentException { + IndexOutOfBoundsException, + IllegalArgumentException { if (fromIndex < 0 || toIndex > size()) throw new IndexOutOfBoundsException(); @@ -167,17 +117,10 @@ public ImmutableLinkedList subList(int fromIndex, int toIndex) throws Node subListLast = node; return new ImmutableLinkedList(subListHead, - subListLast, - toIndex - fromIndex); + subListLast, + toIndex - fromIndex); } - // Factories - - public ImmutableLinkedList cons(E elem) { - return new ImmutableLinkedList(new Node(elem, headNode()), - lastNode(), - size() + 1); - } @SuppressWarnings("unchecked") public ImmutableLinkedList remove(int index) { @@ -202,30 +145,17 @@ public ImmutableLinkedList remove(int index) { return new ImmutableLinkedList(newElems); } - @Override - public ImmutableList tail() throws UnsupportedOperationException { - // TODO Auto-generated method stub - return null; - } - - @Override - public boolean isEmpty() { - // TODO Auto-generated method stub - return false; - } - - @Override - public ImmutableCoreList clone() { - // TODO Auto-generated method stub - return null; + + @Override + public ImmutableLinkedList tail() { + return (ImmutableLinkedList) super.tail(); } + + @Override - public Iterator iterator() { - // TODO Auto-generated method stub - return null; - } - + public ImmutableLinkedList cons(E elem) { + return (ImmutableLinkedList) super.cons(elem); + } - } From cf0d8a125188caa899491f18f0eec6f6ee444d10 Mon Sep 17 00:00:00 2001 From: Theophile Morin Date: Thu, 12 Jun 2014 16:43:55 +0200 Subject: [PATCH 5/6] IterativeList refactoring --- .../implementations/ImmutableArrayList.java | 122 +++--------------- .../ImmutableBaseIterativeList.java | 80 +++++++++--- 2 files changed, 78 insertions(+), 124 deletions(-) diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableArrayList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableArrayList.java index e1f005b..54e11ab 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableArrayList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableArrayList.java @@ -1,27 +1,18 @@ package collections.implementations; import java.util.Collection; -import java.util.Iterator; -import java.util.NoSuchElementException; -import collections.interfaces.ImmutableCoreList; import collections.interfaces.ImmutableList; -import collections.interfaces.IterativeList; -public class ImmutableArrayList implements ImmutableList +public class ImmutableArrayList extends ImmutableBaseIterativeList implements ImmutableList { - private final E[] _array; - private final int _length; - /** * Constructs an empty list with an initial capacity of 0. */ - @SuppressWarnings("unchecked") public ImmutableArrayList () { - _array = (E[]) new Object[0]; - _length = 0; + super(); } /** @@ -32,26 +23,11 @@ public ImmutableArrayList () @SuppressWarnings("unchecked") public ImmutableArrayList (E... elems) { - if (elems == null) - throw new NullPointerException(); - else if (elems.length == 0) { - this._array = (E[]) new Object[0]; - this._length = 0; - } - else - { - _array = elems; - _length = _array.length; - } - } - - public ImmutableArrayList create(E[] elems) { - return new ImmutableArrayList(elems); - } - - public ImmutableArrayList create(Collection elems) { - return new ImmutableArrayList(elems); + super(elems); } + + + /** *

Constructs a list containing the elements of the specified @@ -62,77 +38,24 @@ public ImmutableArrayList create(Collection elems) { * * @param elems - the collection whose elements are to be placed into this list */ - @SuppressWarnings("unchecked") public ImmutableArrayList(Collection elems) { - if (elems == null) - throw new NullPointerException(); - else - { - _array = (E[])elems.toArray(); - _length = _array.length; - } + super(elems); } - /** - * Private accessor to get the inner array. - * @return the inner array. - */ - private E[] getArray() { - return _array; - } - - class ImmutableArrayListIterator implements Iterator { - - /** Current node pointed by the iterator */ - private int index; - private int size; - - /** - * Create a new iterator starting from the beginning of the list. - */ - public ImmutableArrayListIterator() { - index = 0; - size = size(); - } + + - public boolean hasNext() { - return index <= size-1 ? true : false; - } - - public E next() throws NoSuchElementException { - if(index >= size) - throw new NoSuchElementException(); - - E elem = getArray()[index]; - ++index; - return elem; - } - - public void remove() throws - UnsupportedOperationException, - IllegalStateException { - throw new UnsupportedOperationException(); - } + public ImmutableArrayList create(E[] elems) { + return new ImmutableArrayList(elems); } - public Iterator iterator() { - return new ImmutableArrayListIterator(); - + public ImmutableArrayList create(Collection elems) { + return new ImmutableArrayList(elems); } - public int size() { - return _length; - } - public E get(int index) { //TODO create an accessor for the _array and put the code into the absract class. - if (index < 0 || index >= size()) - throw new IndexOutOfBoundsException(); - if(index >=0 && index < size()) - return _array[index]; - else - return null; - } + @Override public ImmutableArrayList subList(int fromIndex, int toIndex) @@ -183,10 +106,7 @@ public ImmutableArrayList remove(int index) { return new ImmutableArrayList(newElems); } - public E[] toArray() { - return getArray(); - } - + public ImmutableArrayList cons(E elem) { @SuppressWarnings("unchecked") E[] elems = (E[]) new Object[size()+1]; @@ -202,15 +122,5 @@ public ImmutableArrayList cons(E elem) { } - public int hashCode() { - return ImmutableCoreList.hashCode(this); - } - - public ImmutableList clone() { - return ImmutableList.clone(this); - } - - public boolean equals(Object o) { - return IterativeList.equals(this, o); - } + } diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java index 6e554da..855e54a 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java @@ -8,12 +8,17 @@ import collections.interfaces.IterativeList; -public class ImmutableBaseIterativeList implements IterativeList +public abstract class ImmutableBaseIterativeList implements IterativeList { - private final E[] _array; - private final int _length; + protected final E[] _array; + protected final int _length; + + + /** + * Constructs an empty list with an initial capacity of 0. + */ @SuppressWarnings("unchecked") public ImmutableBaseIterativeList() { @@ -21,6 +26,60 @@ public ImmutableBaseIterativeList() _length = 0; } + /** + *

Constructs a list containing the given elements + *

If the specified collection is null, constructs an empty ArrayList with a capacity of 0. + * @param elems - the collection whose elements are to be placed into this list + */ + @SuppressWarnings("unchecked") + public ImmutableBaseIterativeList (E... elems) + { + if (elems == null) + throw new NullPointerException(); + else if (elems.length == 0) { + this._array = (E[]) new Object[0]; + this._length = 0; + } + else + { + _array = elems; + _length = _array.length; + } + } + + + /** + *

Constructs a list containing the elements of the specified + * collection, in the order they are returned by the collection's + * iterator. + *

If the specified collection is null, constructs an empty ArrayList + * with a capacity of 0. + * + * @param elems - the collection whose elements are to be placed into this list + */ + @SuppressWarnings("unchecked") + public ImmutableBaseIterativeList(Collection elems) + { + if (elems == null) + throw new NullPointerException(); + else + { + _array = (E[])elems.toArray(); + _length = _array.length; + } + } + + + /** + * Private accessor to get the inner array. + * @return the inner array. + */ + protected E[] getArray() { + return _array; + } + + + @Override public E get(int index) throws IndexOutOfBoundsException { if (index < 0 || index >= size()) @@ -46,21 +105,6 @@ public int hashCode() { return ImmutableCoreList.hashCode(this); } - @Override - public ImmutableCoreList create(E[] elems) { - return new ImmutableArrayList(elems); - } - - @Override - public ImmutableCoreList create(Collection elems) { - return new ImmutableArrayList(elems); - } - - @Override - public boolean isEmpty() { - return this._length==0; - } - @Override public Iterator iterator() { return new ImmutableArrayListIterator(); From 20489f7fbaeec60a182667caf9635b5b2457f42f Mon Sep 17 00:00:00 2001 From: Theophile Morin Date: Thu, 12 Jun 2014 16:46:50 +0200 Subject: [PATCH 6/6] Warnings removed --- .../implementations/ImmutableReversedArrayList.java | 4 ++-- .../src/collections/interfaces/ImmutableCoreList.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableReversedArrayList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableReversedArrayList.java index 38feff9..19a5ca5 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableReversedArrayList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableReversedArrayList.java @@ -52,7 +52,7 @@ public E last() throws NoSuchElementException { return list.head(); } - @SuppressWarnings("unchecked") + public ImmutableArrayList subList(int fromIndex, int toIndex) throws IndexOutOfBoundsException, IllegalArgumentException { @@ -67,7 +67,7 @@ public ImmutableArrayList reverse() { return list; } - @SuppressWarnings("unchecked") + public ImmutableArrayList remove(int index) { return list.remove(reverseIndex(index)).reverse(); } diff --git a/Immutable_collections_java8_project/src/collections/interfaces/ImmutableCoreList.java b/Immutable_collections_java8_project/src/collections/interfaces/ImmutableCoreList.java index f6b660b..2c06f5d 100644 --- a/Immutable_collections_java8_project/src/collections/interfaces/ImmutableCoreList.java +++ b/Immutable_collections_java8_project/src/collections/interfaces/ImmutableCoreList.java @@ -237,7 +237,7 @@ default Stream parallelStream() { @SuppressWarnings("unchecked") default E[] toArray() { //TODO unused E elem int size = 0; - for ( E elem : this) + for ( @SuppressWarnings("unused") E elem : this) ++size; return toArray((E[]) new Object[size]); }