Skip to content

Commit

Permalink
Merge pull request #1390 from motlin/OrderedIterable.detectIndex
Browse files Browse the repository at this point in the history
Change OrderedIterable.indexOf() to have a default implementation.
  • Loading branch information
donraab committed Sep 14, 2022
2 parents ac96a96 + 2574f63 commit 2ab2bf5
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,10 @@ default ImmutableList<T> toImmutableList()
{
return this.toImmutable();
}

@Override
default int indexOf(Object o)
{
return ListIterable.super.indexOf(o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
package org.eclipse.collections.api.ordered;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.collections.api.RichIterable;
Expand Down Expand Up @@ -59,7 +61,20 @@ public interface OrderedIterable<T> extends RichIterable<T>
*
* @see List#indexOf(Object)
*/
int indexOf(Object object);
default int indexOf(Object object)
{
int i = 0;
Iterator<T> iterator = this.iterator();
while (iterator.hasNext())
{
if (Objects.equals(iterator.next(), object))
{
return i;
}
i++;
}
return -1;
}

/**
* Returns the first element of an iterable. In the case of a List it is the element at the first index. In the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,6 @@ public MutableStack<T> toStack()
return Stacks.mutable.empty();
}

@Override
public int indexOf(Object object)
{
return -1;
}

@Override
public int compareTo(SortedBag<T> o)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,6 @@ public T getOnly()
throw new IllegalStateException("Size must be 1 but was " + this.size());
}

@Override
public int indexOf(Object object)
{
return -1;
}

@Override
public boolean equals(Object otherList)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,12 +582,6 @@ public int detectLastIndex(Predicate<? super V> predicate)
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".detectLastIndex() not implemented yet");
}

@Override
public int indexOf(Object object)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".indexOf() not implemented yet");
}

@Override
public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super V, ? super S> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,12 +547,6 @@ public ImmutableList<V> distinct()
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".distinct() not implemented yet");
}

@Override
public int indexOf(Object object)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".indexOf() not implemented yet");
}

@Override
public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super V, ? super S> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,6 @@ public int detectLastIndex(Predicate<? super V> predicate)
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".detectLastIndex() not implemented yet");
}

@Override
public int indexOf(Object object)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".indexOf() not implemented yet");
}

@Override
public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super V, ? super S> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1332,12 +1332,6 @@ public int detectLastIndex(Predicate<? super V> predicate)
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".detectLastIndex() not implemented yet");
}

@Override
public int indexOf(Object object)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".indexOf() not implemented yet");
}

@Override
public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super V, ? super S> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,6 @@ public T last()
throw new NoSuchElementException();
}

@Override
public int indexOf(Object object)
{
return -1;
}

@Override
public Comparator<? super T> comparator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1077,12 +1077,6 @@ public ImmutableStack<T> distinct()
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".distinct() not implemented yet");
}

@Override
public int indexOf(Object object)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".indexOf() not implemented yet");
}

@Override
public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super T, ? super S> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -743,12 +743,6 @@ public ImmutableStack<T> distinct()
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".distinct() not implemented yet");
}

@Override
public int indexOf(Object object)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".indexOf() not implemented yet");
}

@Override
public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super T, ? super S> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,6 @@ public ImmutableStack<T> distinct()
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".distinct() not implemented yet");
}

@Override
public int indexOf(Object object)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".indexOf() not implemented yet");
}

@Override
public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super T, ? super S> predicate)
{
Expand Down

0 comments on commit 2ab2bf5

Please sign in to comment.