diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractInductiveList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractInductiveList.java deleted file mode 100644 index 0de4cb0..0000000 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractInductiveList.java +++ /dev/null @@ -1,49 +0,0 @@ -package collections.implementations; - -import java.util.NoSuchElementException; - -import collections.interfaces.ImmutableCoreList; -import collections.interfaces.InductiveList; - -public abstract class ImmutableAbstractInductiveList implements InductiveList { - - @Override - public boolean isEmpty() { - return false; //TODO find why this method is not implemented in concrete class (ImmutableLinkedList). - } - - @Override - public ImmutableCoreList clone() { //TODO check static returned type. - return ImmutableCoreList.clone(this); //TODO check call - } - - @Override - public boolean equals(Object o) { - return ImmutableCoreList.equals(this, o);//TODO check call to first background interface while InductiveList - } - - @Override - public int hashCode() { - return ImmutableCoreList.hashCode(this); //TODO check call to first background interface while InductiveList - } - - @Override - public InductiveList cons(E elem) { - return null; // new ImmutableLinkedList(new Node(elem, head()), last(), size() + 1); //TODO add size in Core ? - } - - @Override - public E head() throws NoSuchElementException { //TODO implementation class call an accessor method, check it. - return null; - } - - @Override - public InductiveList tail() throws UnsupportedOperationException { //TODO ERROR : check why tail is not implemented in the implementation class... - return null; - } - - @Override - public E last() throws NoSuchElementException { //TODO implementation class call an accessor method, check it. - return null; - } -} diff --git a/Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractIterativeList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractIterativeList.java deleted file mode 100644 index 89736c9..0000000 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableAbstractIterativeList.java +++ /dev/null @@ -1,29 +0,0 @@ -package collections.implementations; - -import collections.interfaces.ImmutableCoreList; -import collections.interfaces.IterativeList; - -public abstract class ImmutableAbstractIterativeList implements IterativeList -{ - - @Override - public E get(int index) throws IndexOutOfBoundsException { - return null; //TODO refers to the implementation class - } - - @Override - public boolean equals(Object o) { - return IterativeList.equals(this, o); //TODO check "this" validity (abstract class) - } - - @Override - public ImmutableCoreList clone() { //TODO check static returned type - return ImmutableCoreList.clone(this); //TODO check "this" validity (abstract class) - } - - @Override - public int hashCode() { - return ImmutableCoreList.hashCode(this); //TODO check "this" validity (abstract class) - } - -} 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/ImmutableBaseInductiveList.java b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java new file mode 100644 index 0000000..805ee5c --- /dev/null +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseInductiveList.java @@ -0,0 +1,284 @@ +package collections.implementations; + +import java.util.Collection; +import java.util.Iterator; +import java.util.NoSuchElementException; + +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 abstract class ImmutableBaseInductiveList implements InductiveList { + + /** The first node element of the list. */ + protected final Node head; + + /** The last node element of the list. */ + protected final Node last; + + /** The number of elements in this list. */ + 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. + **/ + protected Node headNode() { + return head; + } + + /** + * Returns the last node element of the list. + * + * @returns the last node element of the list. + **/ + protected Node lastNode() { + return last; + } + + protected int size() { + return size; + } + + @Override + public InductiveList clone() { + return (InductiveList) ImmutableCoreList.clone(this); + } + + @Override + public boolean equals(Object o) { + return ImmutableCoreList.equals(this, o); + } + + @Override + public int hashCode() { + return ImmutableCoreList.hashCode(this); + } + + @Override + public InductiveList cons(E elem) { + return create(new Node(elem, headNode()), + lastNode(), + size() + 1); + } + + @Override + public E head() throws NoSuchElementException { + if (isEmpty()) + throw new NoSuchElementException(); + else + return headNode().getElement(); + } + + + @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); + } + } + + public abstract ImmutableLinkedList create(Node from, Node head, int size); + + @Override + public E last() throws NoSuchElementException { + if (isEmpty()) + throw new NoSuchElementException(); + else + return lastNode().getElement(); + } + + + + @Override + public Iterator iterator() { + return new ImmutableLinkedListIterator(); + } + + + + @Override + public boolean isEmpty() { + return head==null; + } + + // Iterators & streams + + class ImmutableLinkedListIterator implements Iterator { //TODO change name ? + + /** 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 new file mode 100644 index 0000000..855e54a --- /dev/null +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableBaseIterativeList.java @@ -0,0 +1,152 @@ +package collections.implementations; + +import java.util.Collection; +import java.util.Iterator; +import java.util.NoSuchElementException; + +import collections.interfaces.ImmutableCoreList; +import collections.interfaces.IterativeList; + + +public abstract class ImmutableBaseIterativeList implements IterativeList +{ + + protected final E[] _array; + protected final int _length; + + + + /** + * Constructs an empty list with an initial capacity of 0. + */ + @SuppressWarnings("unchecked") + public ImmutableBaseIterativeList() + { + _array = (E[]) new Object[0]; + _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()) + 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); + } + + @Override + public ImmutableCoreList clone() { + return ImmutableCoreList.clone(this); + } + + @Override + public int hashCode() { + return ImmutableCoreList.hashCode(this); + } + + @Override + public Iterator iterator() { + return new ImmutableArrayListIterator(); + } + + @Override + public int size() { + 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 2013b09..6db34c4 100644 --- a/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java +++ b/Immutable_collections_java8_project/src/collections/implementations/ImmutableLinkedList.java @@ -1,88 +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.IterativeList; -class Node { +public class ImmutableLinkedList extends ImmutableBaseInductiveList implements ImmutableList{ - /** 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. */ - 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 @@ -94,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); } /** @@ -113,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()); } @@ -125,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(); @@ -169,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(); @@ -209,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) { @@ -244,60 +145,17 @@ 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(); - } - } - - public Iterator iterator() { - return new ImmutableLinkedListIterator(); + + @Override + public ImmutableLinkedList tail() { + return (ImmutableLinkedList) super.tail(); } - public int hashCode() { - return ImmutableCoreList.hashCode(this); //TODO check call to first background interface while ImmutableList - } - - public ImmutableList clone() { - return ImmutableList.clone(this); - } + + + @Override + public ImmutableLinkedList cons(E elem) { + return (ImmutableLinkedList) super.cons(elem); + } - public boolean equals(Object o) { - return IterativeList.equals(this, o); - } } 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]); }