This is an automated email from the ASF dual-hosted git repository. daim pushed a commit to branch OAK-11073 in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit a91d3165e205e341b20fdf8ca76bd8aab01132c2 Author: Rishabh Kumar <[email protected]> AuthorDate: Mon Sep 2 16:24:07 2024 +0530 OAK-11073 : added conversion utility methods to convert iterable/iterator to set/list --- oak-commons/pom.xml | 5 ++ .../oak/commons/collections/CollectionUtils.java | 82 ++++++++++++++++++++++ .../oak/commons/collections/package-info.java | 27 +++++++ 3 files changed, 114 insertions(+) diff --git a/oak-commons/pom.xml b/oak-commons/pom.xml index 14d3e8e929..cad655c2b1 100644 --- a/oak-commons/pom.xml +++ b/oak-commons/pom.xml @@ -103,6 +103,11 @@ <artifactId>oak-shaded-guava</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-collections4</artifactId> + <version>4.4</version> + </dependency> <!-- Test dependencies --> <dependency> diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java new file mode 100644 index 0000000000..9c0d9db2a2 --- /dev/null +++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java @@ -0,0 +1,82 @@ +/* + * 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.apache.commons.collections4.IterableUtils; +import org.apache.commons.collections4.IteratorUtils; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +/** + * Utility methods for collections conversions. + */ +public class CollectionUtils { + + private CollectionUtils() { + // no instances for you + } + + /** + * Convert an iterable to a list.The retuning list is mutable and supports all optional operations. + * @param iterable the iterable to convert + * @return the list + * @param <T> the type of the elements + */ + public static <T> List<T> toList(final Iterable<T> iterable) { + return IterableUtils.toList(iterable); + } + + /** + * Convert an iterator to a list. The retuning list is mutable and supports all optional operations. + * @param iterator the iterator to convert + * @return the list + * @param <T> the type of the elements + */ + public static <T> List<T> toList(final Iterator<T> iterator) { + return IteratorUtils.toList(iterator); + } + + /** + * Convert an iterable to a set. The retuning set is mutable and supports all optional operations. + * @param iterable the iterable to convert + * @return the set + * @param <T> the type of the elements + */ + public static <T> Set<T> toSet(final Iterable<T> iterable) { + return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toSet()); + } + + /** + * Convert an iterator to a set. The retuning set is mutable and supports all optional operations. + * @param iterator the iterator to convert + * @return the set + * @param <T> the type of the elements + */ + public static <T> Set<T> toSet(final Iterator<T> iterator) { + final Set<T> set = new HashSet<>(); + iterator.forEachRemaining(set::add); + return set; + } + +} diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/package-info.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/package-info.java new file mode 100644 index 0000000000..1b3c2e647a --- /dev/null +++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/package-info.java @@ -0,0 +1,27 @@ +/* + * 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. + */ + + +/** + * Annotations for internal use in Oak. + */ +@Internal +package org.apache.jackrabbit.oak.commons.collections; +import org.apache.jackrabbit.oak.commons.annotations.Internal; +
