Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAK-11408: Extract IteratorUtils from CollectionUtils #2003

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.jackrabbit.oak.commons.collections;

import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.Objects;

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -52,39 +51,6 @@ public static <T> ArrayDeque<T> toArrayDeque(@NotNull Iterable<? extends T> iter
return arrayDeque;
}

/**
* Convert an {@code Iterator} to an {@code Iterable}.
* <p>
* This method is not thread-safe
*
* @param iterator
* iterator to convert
* @return a single-use iterable for the iterator (representing the remaining
* elements in the iterator)
* @throws IllegalStateException
* when {@linkplain Iterable#iterator()} is called more than
* once
*/
@NotNull
public static <T> Iterable<T> toIterable(@NotNull final Iterator<T> iterator) {
Objects.requireNonNull(iterator);

return new Iterable<>() {

private boolean consumed = false;

@Override
public @NotNull Iterator<T> iterator() {
if (consumed) {
throw new IllegalStateException("Iterator already returned once");
} else {
consumed = true;
return iterator;
}
}
};
}

/**
* Ensure the capacity of a map or set given the expected number of elements.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.jackrabbit.oak.commons.collections;

import org.jetbrains.annotations.NotNull;

import java.util.Iterator;
import java.util.Objects;
import java.util.stream.Stream;

/**
* Utility methods for {@link Iterator} conversions.
*/
public class IteratorUtils {

private IteratorUtils() {
// no instances for you
}
/**
* Convert an {@code Iterator} to an {@code Iterable}.
* <p>
* This method is not thread-safe
*
* @param iterator iterator to convert
* @return a single-use iterable for the iterator (representing the remaining
* elements in the iterator)
* @throws IllegalStateException when {@linkplain Iterable#iterator()} is called more than
* once
*/
@NotNull
public static <T> Iterable<T> toIterable(@NotNull final Iterator<T> iterator) {
Objects.requireNonNull(iterator);

return new Iterable<>() {

private boolean consumed = false;

@Override
public @NotNull Iterator<T> iterator() {
if (consumed) {
throw new IllegalStateException("Iterator already returned once");
} else {
consumed = true;
return iterator;
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public static <T> Stream<T> toStream(@NotNull Iterable<T> iterable) {
*/
@NotNull
public static <T> Stream<T> toStream(@NotNull Iterator<T> iterator) {
return StreamSupport.stream(CollectionUtils.toIterable(iterator).spliterator(), false);
return StreamSupport.stream(IteratorUtils.toIterable(iterator).spliterator(), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import static org.junit.Assert.fail;
Expand All @@ -50,24 +49,6 @@ public void toArrayDequeWithEmptyIterable() {
Assert.assertNotNull(result);
Assert.assertTrue(result.isEmpty());
}

@Test
public void iteratorToIIteratable() {
Iterator<String> iterator = List.of("a", "b", "c").iterator();
iterator.next();
Iterable<String> iterable = CollectionUtils.toIterable(iterator);
Iterator<String> testit = iterable.iterator();
Assert.assertEquals("b", testit.next());
Assert.assertEquals("c", testit.next());
Assert.assertFalse(testit.hasNext());
try {
testit = iterable.iterator();
fail("should only work once");
} catch (IllegalStateException expected) {
// that's what we want
}
}

@Test
public void ensureCapacity() {
int capacity = CollectionUtils.ensureCapacity(8);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.jackrabbit.oak.commons.collections;

import org.junit.Assert;
import org.junit.Test;

import java.util.Iterator;
import java.util.List;

import static org.junit.Assert.fail;

public class IteratorUtilsTest {

@Test
public void iteratorToIIteratable() {
Iterator<String> iterator = List.of("a", "b", "c").iterator();
iterator.next();
Iterable<String> iterable = IteratorUtils.toIterable(iterator);
Iterator<String> testit = iterable.iterator();
Assert.assertEquals("b", testit.next());
Assert.assertEquals("c", testit.next());
Assert.assertFalse(testit.hasNext());
try {
testit = iterable.iterator();
fail("should only work once");
} catch (IllegalStateException expected) {
// that's what we want
}
}

reschke marked this conversation as resolved.
Show resolved Hide resolved
}
Loading