diff --git a/src/main/java/iterator/RichIterator.java b/src/main/java/iterator/RichIterator.java index dfc87f2..5ec354c 100644 --- a/src/main/java/iterator/RichIterator.java +++ b/src/main/java/iterator/RichIterator.java @@ -10,14 +10,20 @@ public interface RichIterator extends Iterator { * @return true if there are no more elements, false otherwise */ default boolean isEmpty() { - throw new NotImplementedException(); + return !hasNext(); } /** * @return number of elements */ default int length() { - throw new NotImplementedException(); + int count = 0; + RichIterator tmpIter = new Wrapper<>( this.toList().iterator()); + while (tmpIter.hasNext()) { + count++; + tmpIter.next(); + } + return count; } /** @@ -25,14 +31,24 @@ default int length() { * @throws NoSuchElementException if the iterator is empty */ default A last() { - throw new NotImplementedException(); + if (this.isEmpty()) { + throw new NoSuchElementException(); + } + A next = null; + while (this.hasNext()) { + next = this.next(); + } + return next; } /** * @return the last element if not empty. */ default Optional lastOptional() { - throw new NotImplementedException(); + if (this.hasNext()) { + return Optional.of(this.last()); + } + return Optional.empty(); } /** @@ -40,7 +56,14 @@ default Optional lastOptional() { * @return the index of the element if exists, -1 otherwise */ default int indexOf(A elem) { - throw new NotImplementedException(); + int idx = 0; + while (this.hasNext()) { + if (Objects.equals(elem, this.next())) { + return idx; + } + idx++; + } + return -1; } /** @@ -50,14 +73,23 @@ default int indexOf(A elem) { * @return Optional.of(A) if exists, empty() otherwise. */ default Optional find(Predicate f) { - throw new NotImplementedException(); + while (this.hasNext()) { + A currElement = this.next(); + if (f.test(currElement)) { + return Optional.of(currElement); + } + } + return Optional.empty(); } /** * @return Optional.of(next) if exists or empty() otherwise */ default Optional nextOptional() { - throw new NotImplementedException(); + if (this.hasNext()) { + return Optional.of(this.next()); + } + return Optional.empty(); } /** @@ -66,7 +98,9 @@ default Optional nextOptional() { * @param f the Consumer */ default void foreach(Consumer f) { - throw new NotImplementedException(); + while (this.hasNext()){ + f.accept(this.next()); + } } /** @@ -74,7 +108,12 @@ default void foreach(Consumer f) { * @return true if exists, false otherwise */ default boolean contains(A elem) { - throw new NotImplementedException(); + while (this.hasNext()) { + if (elem.equals(this.next())) { + return true; + } + } + return false; } /** @@ -88,21 +127,27 @@ default boolean contains(A elem) { * @return the collection */ default > C toCollection(Supplier collectionFactory) { - throw new NotImplementedException(); + C newCol = collectionFactory.get(); + newCol.addAll(toList()); + return newCol; } /** * @return a list built from the iterator's elements */ default List toList() { - throw new NotImplementedException(); + List list = new ArrayList<>(); + while (this.hasNext()) { + list.add(this.next()); + } + return list; } /** * @return a set built from the iterator's elements */ default Set toSet() { - throw new NotImplementedException(); + return this.toCollection(HashSet::new); } /** @@ -110,8 +155,12 @@ default Set toSet() { * @return true if this and that have the same elements in the same order */ default boolean sameElements(Iterator that) { - // TODO: implement this method - throw new NotImplementedException(); + while (that.hasNext() && this.hasNext()) { + if (!this.next().equals(that.next())) { + return false; + } + } + return !(that.hasNext() || this.hasNext()); } // hard @@ -124,8 +173,7 @@ default boolean sameElements(Iterator that) { * @return an iterator that contains only this element */ static RichIterator pure(A elem) { - // TODO: implement this method - throw new NotImplementedException(); + return apply(elem); } /** @@ -136,7 +184,7 @@ static RichIterator pure(A elem) { * @throws NoSuchElementException if the iterator is empty */ default A max(Comparator comparator) { - throw new NotImplementedException(); + return this.toList().stream().max(comparator).orElse(null); } /** @@ -147,7 +195,7 @@ default A max(Comparator comparator) { * @throws NoSuchElementException if the iterator is empty */ default A min(Comparator comparator) { - throw new NotImplementedException(); + return this.toList().stream().min(comparator).orElse(null); } /**