diff --git a/clients/src/main/java/org/apache/kafka/common/utils/FlattenedIterator.java b/clients/src/main/java/org/apache/kafka/common/utils/FlattenedIterator.java deleted file mode 100644 index 4e28bb35c66..00000000000 --- a/clients/src/main/java/org/apache/kafka/common/utils/FlattenedIterator.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.kafka.common.utils; - -import java.util.Iterator; -import java.util.function.Function; - -/** - * Provides a flattened iterator over the inner elements of an outer iterator. - */ -public final class FlattenedIterator extends AbstractIterator { - private final Iterator outerIterator; - private final Function> innerIteratorFunction; - private Iterator innerIterator; - - public FlattenedIterator(Iterator outerIterator, Function> innerIteratorFunction) { - this.outerIterator = outerIterator; - this.innerIteratorFunction = innerIteratorFunction; - } - - @Override - protected I makeNext() { - while (innerIterator == null || !innerIterator.hasNext()) { - if (outerIterator.hasNext()) - innerIterator = innerIteratorFunction.apply(outerIterator.next()); - else - return allDone(); - } - return innerIterator.next(); - } -} diff --git a/clients/src/test/java/org/apache/kafka/common/utils/FlattenedIteratorTest.java b/clients/src/test/java/org/apache/kafka/common/utils/FlattenedIteratorTest.java deleted file mode 100644 index 057b6118e07..00000000000 --- a/clients/src/test/java/org/apache/kafka/common/utils/FlattenedIteratorTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.kafka.common.utils; - -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -import static java.util.Arrays.asList; -import static java.util.Collections.emptyList; -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class FlattenedIteratorTest { - - @Test - public void testNestedLists() { - List> list = asList( - asList("foo", "a", "bc"), - Collections.singletonList("ddddd"), - asList("", "bar2", "baz45")); - - Iterable flattenedIterable = () -> new FlattenedIterator<>(list.iterator(), List::iterator); - List flattened = new ArrayList<>(); - flattenedIterable.forEach(flattened::add); - - assertEquals(list.stream().flatMap(Collection::stream).collect(Collectors.toList()), flattened); - - // Ensure we can iterate multiple times - List flattened2 = new ArrayList<>(); - flattenedIterable.forEach(flattened2::add); - - assertEquals(flattened, flattened2); - } - - @Test - public void testEmptyList() { - List> list = emptyList(); - - Iterable flattenedIterable = () -> new FlattenedIterator<>(list.iterator(), List::iterator); - List flattened = new ArrayList<>(); - flattenedIterable.forEach(flattened::add); - - assertEquals(emptyList(), flattened); - } - - @Test - public void testNestedSingleEmptyList() { - List> list = Collections.singletonList(emptyList()); - - Iterable flattenedIterable = () -> new FlattenedIterator<>(list.iterator(), List::iterator); - List flattened = new ArrayList<>(); - flattenedIterable.forEach(flattened::add); - - assertEquals(emptyList(), flattened); - } - - @Test - public void testEmptyListFollowedByNonEmpty() { - List> list = asList( - emptyList(), - asList("boo", "b", "de")); - - Iterable flattenedIterable = () -> new FlattenedIterator<>(list.iterator(), List::iterator); - List flattened = new ArrayList<>(); - flattenedIterable.forEach(flattened::add); - - assertEquals(list.stream().flatMap(Collection::stream).collect(Collectors.toList()), flattened); - } - - @Test - public void testEmptyListInBetweenNonEmpty() { - List> list = asList( - Collections.singletonList("aadwdwdw"), - emptyList(), - asList("ee", "aa", "dd")); - - Iterable flattenedIterable = () -> new FlattenedIterator<>(list.iterator(), List::iterator); - List flattened = new ArrayList<>(); - flattenedIterable.forEach(flattened::add); - - assertEquals(list.stream().flatMap(Collection::stream).collect(Collectors.toList()), flattened); - } - - @Test - public void testEmptyListAtTheEnd() { - List> list = asList( - asList("ee", "dd"), - Collections.singletonList("e"), - emptyList()); - - Iterable flattenedIterable = () -> new FlattenedIterator<>(list.iterator(), List::iterator); - List flattened = new ArrayList<>(); - flattenedIterable.forEach(flattened::add); - - assertEquals(list.stream().flatMap(Collection::stream).collect(Collectors.toList()), flattened); - } - -}