Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 66 additions & 18 deletions src/main/java/iterator/RichIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,60 @@ public interface RichIterator<A> extends Iterator<A> {
* @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<A> tmpIter = new Wrapper<>( this.toList().iterator());
while (tmpIter.hasNext()) {
count++;
tmpIter.next();
}
return count;
}

/**
* @return the last element of the iterator
* @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<A> lastOptional() {
throw new NotImplementedException();
if (this.hasNext()) {
return Optional.of(this.last());
}
return Optional.empty();
}

/**
* @param elem the element
* @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;
}

/**
Expand All @@ -50,14 +73,23 @@ default int indexOf(A elem) {
* @return Optional.of(A) if exists, empty() otherwise.
*/
default Optional<A> find(Predicate<? super A> 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<A> nextOptional() {
throw new NotImplementedException();
if (this.hasNext()) {
return Optional.of(this.next());
}
return Optional.empty();
}

/**
Expand All @@ -66,15 +98,22 @@ default Optional<A> nextOptional() {
* @param f the Consumer
*/
default void foreach(Consumer<? super A> f) {
throw new NotImplementedException();
while (this.hasNext()){
f.accept(this.next());
}
}

/**
* @param elem checks if the iterator has the element
* @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;
}

/**
Expand All @@ -88,30 +127,40 @@ default boolean contains(A elem) {
* @return the collection
*/
default <C extends Collection<A>> C toCollection(Supplier<C> collectionFactory) {
throw new NotImplementedException();
C newCol = collectionFactory.get();
newCol.addAll(toList());
return newCol;
}

/**
* @return a list built from the iterator's elements
*/
default List<A> toList() {
throw new NotImplementedException();
List<A> list = new ArrayList<>();
while (this.hasNext()) {
list.add(this.next());
}
return list;
}

/**
* @return a set built from the iterator's elements
*/
default Set<A> toSet() {
throw new NotImplementedException();
return this.toCollection(HashSet::new);
}

/**
* @param that other iterator
* @return true if this and that have the same elements in the same order
*/
default boolean sameElements(Iterator<A> 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
Expand All @@ -124,8 +173,7 @@ default boolean sameElements(Iterator<A> that) {
* @return an iterator that contains only this element
*/
static <A> RichIterator<A> pure(A elem) {
// TODO: implement this method
throw new NotImplementedException();
return apply(elem);
}

/**
Expand All @@ -136,7 +184,7 @@ static <A> RichIterator<A> pure(A elem) {
* @throws NoSuchElementException if the iterator is empty
*/
default A max(Comparator<? super A> comparator) {
throw new NotImplementedException();
return this.toList().stream().max(comparator).orElse(null);
}

/**
Expand All @@ -147,7 +195,7 @@ default A max(Comparator<? super A> comparator) {
* @throws NoSuchElementException if the iterator is empty
*/
default A min(Comparator<? super A> comparator) {
throw new NotImplementedException();
return this.toList().stream().min(comparator).orElse(null);
}

/**
Expand Down