More Cases covered
This commit is contained in:
parent
602dba71be
commit
31b23630ec
|
@ -48,7 +48,16 @@
|
||||||
<artifactId>protonpack</artifactId>
|
<artifactId>protonpack</artifactId>
|
||||||
<version>1.16</version>
|
<version>1.16</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-collections4</artifactId>
|
||||||
|
<version>4.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>one.util</groupId>
|
||||||
|
<artifactId>streamex</artifactId>
|
||||||
|
<version>0.8.1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -5,42 +5,35 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ListIterator;
|
import java.util.ListIterator;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.OptionalInt;
|
import java.util.OptionalInt;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
import org.apache.commons.collections4.IterableUtils;
|
||||||
import org.assertj.core.util.Lists;
|
import org.assertj.core.util.Lists;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.codepoetics.protonpack.StreamUtils;
|
import com.codepoetics.protonpack.StreamUtils;
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
|
|
||||||
|
import io.vavr.collection.Stream;
|
||||||
|
import one.util.streamex.EntryStream;
|
||||||
|
|
||||||
public class FirstMatchingElementUnitTest {
|
public class FirstMatchingElementUnitTest {
|
||||||
|
|
||||||
@Test
|
private final List<Object> dataList = Lists.newArrayList("String", Boolean.TRUE, Integer.valueOf(10), Boolean.FALSE, Double.valueOf(20.0));
|
||||||
public void testWithForLoop() {
|
|
||||||
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE);
|
|
||||||
int index = -1;
|
|
||||||
for (int i = 0; i < dataList.size(); i++) {
|
|
||||||
if (dataList.get(i) instanceof Boolean) {
|
|
||||||
index = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assertEquals(2, index);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithIndexOf() {
|
public void whenCalled_thenFindIndexUsingStream() {
|
||||||
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE);
|
int index = dataList.stream()
|
||||||
Optional<Object> booleanData = dataList.stream()
|
|
||||||
.filter(data -> data instanceof Boolean)
|
.filter(data -> data instanceof Boolean)
|
||||||
.findFirst();
|
.mapToInt(data -> dataList.indexOf(data))
|
||||||
int index = booleanData.isPresent() ? dataList.indexOf(booleanData.get()) : -1;
|
.findFirst()
|
||||||
assertEquals(2, index);
|
.orElse(-1);
|
||||||
|
assertEquals(1, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithIterator() {
|
public void whenCalled_thenFindIndexUsingStreamIterator() {
|
||||||
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE);
|
|
||||||
int index = -1;
|
int index = -1;
|
||||||
Iterator<Object> iterator = dataList.iterator();
|
Iterator<Object> iterator = dataList.iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
|
@ -50,12 +43,11 @@ public class FirstMatchingElementUnitTest {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assertEquals(2, index);
|
assertEquals(1, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithListIterator() {
|
public void whenCalled_thenFindIndexUsingStreamListIterator() {
|
||||||
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE);
|
|
||||||
int index = -1;
|
int index = -1;
|
||||||
ListIterator<Object> listIterator = dataList.listIterator();
|
ListIterator<Object> listIterator = dataList.listIterator();
|
||||||
while (listIterator.hasNext()) {
|
while (listIterator.hasNext()) {
|
||||||
|
@ -64,41 +56,70 @@ public class FirstMatchingElementUnitTest {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assertEquals(2, index);
|
assertEquals(1, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithTakeAWhile() {
|
public void whenCalled_thenFindIndexUsingIntStream() {
|
||||||
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE, 2, 3);
|
int index = IntStream.range(0, dataList.size() - 1)
|
||||||
|
.filter(streamIndex -> dataList.get(streamIndex) instanceof Boolean)
|
||||||
|
.findFirst()
|
||||||
|
.orElse(-1);
|
||||||
|
assertEquals(1, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCalled_thenFindIndexUsingStreamTakeWhile() {
|
||||||
OptionalInt booleanIndex = dataList.stream()
|
OptionalInt booleanIndex = dataList.stream()
|
||||||
.takeWhile(data -> !(data instanceof Boolean))
|
.takeWhile(data -> !(data instanceof Boolean))
|
||||||
.mapToInt(dataList::indexOf)
|
.mapToInt(dataList::indexOf)
|
||||||
.max();
|
.max();
|
||||||
|
|
||||||
if (booleanIndex.isPresent() && dataList.get(booleanIndex.getAsInt()) instanceof Boolean) {
|
if (booleanIndex.isPresent() && dataList.get(booleanIndex.getAsInt()) instanceof Boolean) {
|
||||||
assertEquals(2, booleanIndex.getAsInt());
|
assertEquals(1, booleanIndex.getAsInt());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithIntStream() {
|
public void whenCalled_thenFindIndexUsingZipWithIndex() {
|
||||||
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE);
|
|
||||||
int index = IntStream.of(0, dataList.size() - 1)
|
|
||||||
.filter(streamIndex -> dataList.get(streamIndex) instanceof Boolean)
|
|
||||||
.findFirst()
|
|
||||||
.orElse(-1);
|
|
||||||
assertEquals(2, index);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testWithZipWithIndex() {
|
|
||||||
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE, 2, 3);
|
|
||||||
int index = StreamUtils.zipWithIndex(dataList.stream())
|
int index = StreamUtils.zipWithIndex(dataList.stream())
|
||||||
.filter(i -> i.getValue() instanceof Boolean)
|
.filter(i -> i.getValue() instanceof Boolean)
|
||||||
.mapToInt(i -> Long.valueOf(i.getIndex())
|
.mapToInt(i -> Long.valueOf(i.getIndex())
|
||||||
.intValue())
|
.intValue())
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(-1);
|
.orElse(-1);
|
||||||
assertEquals(2, index);
|
assertEquals(1, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCalled_thenFindIndexUsingGoogleGuava() {
|
||||||
|
int index = Iterables.indexOf(dataList, data -> data instanceof Boolean);
|
||||||
|
assertEquals(1, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCalled_thenFindIndexUsingApacheCommons() {
|
||||||
|
int index = IterableUtils.indexOf(dataList, data -> data instanceof Boolean);
|
||||||
|
assertEquals(1, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCalled_thenFindIndexUsingStreamEx() {
|
||||||
|
Integer foundIndex = EntryStream.of(dataList)
|
||||||
|
.filterKeyValue((index, data) -> data instanceof Boolean)
|
||||||
|
.keys()
|
||||||
|
.findFirst()
|
||||||
|
.orElse(-1);
|
||||||
|
assertEquals(1, foundIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCalled_thenFindIndexUsingVavrStream() {
|
||||||
|
Integer foundIndex = Stream.of(dataList.toArray())
|
||||||
|
.zipWithIndex()
|
||||||
|
.find(tuple -> tuple._1() instanceof Boolean)
|
||||||
|
.map(tuple -> tuple._2())
|
||||||
|
.getOrElse(-1);
|
||||||
|
assertEquals(1, foundIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue