Skip to content

Commit

Permalink
OAK-11357: Extract StreamUtils from CollectionUtils (#1980)
Browse files Browse the repository at this point in the history
  • Loading branch information
reschke authored Jan 13, 2025
1 parent b363d95 commit 8dca855
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
import org.apache.jackrabbit.oak.spi.security.authentication.external.AbstractExternalAuthTest;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentity;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalUser;
Expand Down Expand Up @@ -116,7 +116,7 @@ protected void sync(@NotNull ExternalIdentity externalIdentity, @NotNull SyncRes

@NotNull
static List<String> getIds(@NotNull Iterator<? extends Authorizable> authorizables) {
return CollectionUtils.toStream(authorizables).map(authorizable -> {
return StreamUtils.toStream(authorizables).map(authorizable -> {
try {
return authorizable.getID();
} catch (RepositoryException repositoryException) {
Expand All @@ -127,6 +127,6 @@ static List<String> getIds(@NotNull Iterator<? extends Authorizable> authorizabl

@NotNull
static List<String> getPrincipalNames(@NotNull Iterator<Principal> groupPrincipals) {
return CollectionUtils.toStream(groupPrincipals).map(Principal::getName).collect(Collectors.toList());
return StreamUtils.toStream(groupPrincipals).map(Principal::getName).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.jackrabbit.oak.api.Tree;
import org.apache.jackrabbit.oak.commons.PathUtils;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
import org.apache.jackrabbit.oak.plugins.tree.TreeAware;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalGroup;
Expand Down Expand Up @@ -89,7 +90,7 @@ void sync(@NotNull ExternalUser externalUser) throws Exception {
@NotNull
Set<Principal> getExpectedGroupPrincipals(@NotNull String userId) throws Exception {
if (syncConfig.user().getMembershipNestingDepth() == 1) {
return CollectionUtils.toStream(idp.getUser(userId).getDeclaredGroups()).map(externalIdentityRef -> {
return StreamUtils.toStream(idp.getUser(userId).getDeclaredGroups()).map(externalIdentityRef -> {
try {
return new PrincipalImpl(idp.getIdentity(externalIdentityRef).getPrincipalName());
} catch (ExternalIdentityException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.jackrabbit.oak.api.QueryEngine;
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentity;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalUser;
Expand Down Expand Up @@ -81,7 +81,7 @@ public void testNotIsMember() throws Exception {
public void testIsMemberExternalGroup() throws Exception {
GroupPrincipal principal = getGroupPrincipal();

List<String> exGroupPrincNames = CollectionUtils.toStream(
List<String> exGroupPrincNames = StreamUtils.toStream(
idp.listGroups()).map(ExternalIdentity::getPrincipalName).collect(Collectors.toList());
for (String principalName : exGroupPrincNames) {
assertFalse(principal.isMember(new PrincipalImpl(principalName)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -519,32 +518,6 @@ public static <T> Iterable<T> toIterable(@NotNull final Iterator<T> iterator) {
};
}

/**
* Generates a (non-parallel) {@linkplain Stream} for the {@linkplain Iterable}
* @param iterable iterable to convert
* @return the stream
*/
@NotNull
public static <T> Stream<T> toStream(@NotNull Iterable<T> iterable) {
Objects.requireNonNull(iterable);
return StreamSupport.stream(iterable.spliterator(), false);
}

/**
* Generates a (non-parallel) {@linkplain Stream} for the
* {@linkplain Iterable}
* <p>
* This method is not thread-safe
*
* @param iterator
* iterator to convert
* @return the stream (representing the remaining elements in the iterator)
*/
@NotNull
public static <T> Stream<T> toStream(@NotNull Iterator<T> iterator) {
return StreamSupport.stream(toIterable(iterator).spliterator(), false);
}

/**
* 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,59 @@
/*
* 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;
import java.util.stream.StreamSupport;

public class StreamUtils {

private StreamUtils() {
// no instances for you
}

/**
* Generates a (non-parallel) {@linkplain Stream} for the {@linkplain Iterable}
* @param iterable iterable to convert
* @return the stream
*/
@NotNull
public static <T> Stream<T> toStream(@NotNull Iterable<T> iterable) {
Objects.requireNonNull(iterable);
return StreamSupport.stream(iterable.spliterator(), false);
}

/**
* Generates a (non-parallel) {@linkplain Stream} for the
* {@linkplain Iterable}
* <p>
* This method is not thread-safe
*
* @param iterator
* iterator to convert
* @return the stream (representing the remaining elements in the iterator)
*/
@NotNull
public static <T> Stream<T> toStream(@NotNull Iterator<T> iterator) {
return StreamSupport.stream(CollectionUtils.toIterable(iterator).spliterator(), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Utilities for Java collections and streams.
*/
@Internal(since = "1.0.0")
@Version("1.2.0")
@Version("2.0.0")
package org.apache.jackrabbit.oak.commons.collections;
import org.apache.jackrabbit.oak.commons.annotations.Internal;
import org.osgi.annotation.versioning.Version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,6 @@ public void iteratorToIIteratable() {
}
}

@Test
public void iteratorToStream() {
List<String> input = List.of("a", "b", "c");
Iterator<String> iterator = input.iterator();
Stream<String> stream = CollectionUtils.toStream(iterator);
List<String> result = stream.collect(Collectors.toList());
Assert.assertEquals(input.toString(), result.toString());
}

@Test
public void testFromProperties() {
final Properties properties = new Properties();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamUtilsTest {

@Test
public void iteratorToStream() {
List<String> input = List.of("a", "b", "c");
Iterator<String> iterator = input.iterator();
Stream<String> stream = StreamUtils.toStream(iterator);
List<String> result = stream.collect(Collectors.toList());
Assert.assertEquals(input.toString(), result.toString());
}

@Test(expected = NullPointerException.class)
public void iteratorToStreamNull() {
StreamUtils.toStream((Iterator)null);
}

@Test
public void iterableToStream() {
List<String> input = List.of("a", "b", "c");
Stream<String> stream = StreamUtils.toStream(input);
List<String> result = stream.collect(Collectors.toList());
Assert.assertEquals(input.toString(), result.toString());
}

@Test(expected = NullPointerException.class)
public void iterableStreamNull() {
StreamUtils.toStream((Iterable)null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import java.util.stream.Stream;

import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
import org.apache.jackrabbit.oak.json.JsopDiff;
import org.apache.jackrabbit.oak.plugins.memory.PropertyBuilder;
import org.apache.jackrabbit.oak.plugins.tree.TreeConstants;
Expand Down Expand Up @@ -134,8 +134,8 @@ private void resolveConflict(ConflictType conflictType, NodeState conflictInfo)
NodeState oursNS = conflictInfo.getChildNode(OURS);
NodeState baseNS = conflictInfo.getChildNode(BASE);

Set<String> candidates = Stream.concat(CollectionUtils.toStream(oursNS.getChildNodeNames()),
CollectionUtils.toStream(baseNS.getChildNodeNames())).collect(toSet());
Set<String> candidates = Stream.concat(StreamUtils.toStream(oursNS.getChildNodeNames()),
StreamUtils.toStream(baseNS.getChildNodeNames())).collect(toSet());
for (String name : candidates) {
NodeState ours = oursNS.getChildNode(name);
NodeState base = baseNS.getChildNode(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.apache.jackrabbit.oak.commons.PathUtils;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
import org.apache.jackrabbit.oak.plugins.index.IndexUtils;
import org.apache.jackrabbit.oak.plugins.index.cursor.Cursors;
Expand Down Expand Up @@ -108,7 +109,7 @@ public class PropertyIndexPlan {
this.matchesAllTypes = !definition.hasProperty(DECLARING_NODE_TYPES);
this.deprecated = definition.getBoolean(IndexConstants.INDEX_DEPRECATED);
this.matchesNodeTypes =
matchesAllTypes || CollectionUtils.toStream(types).anyMatch(filter.getSupertypes()::contains);
matchesAllTypes || StreamUtils.toStream(types).anyMatch(filter.getSupertypes()::contains);

ValuePattern valuePattern = new ValuePattern(definition);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
import org.apache.jackrabbit.oak.plugins.value.jcr.PartialValueFactory;
import org.apache.jackrabbit.oak.spi.commit.DefaultEditor;
Expand Down Expand Up @@ -176,7 +177,7 @@ public static TypeEditor create(@NotNull ConstraintViolationCallback callback, S
this.checkThisNode =
typesToCheck == null
|| typesToCheck.contains(primary)
|| CollectionUtils.toStream(mixins).anyMatch(typesToCheck::contains);
|| StreamUtils.toStream(mixins).anyMatch(typesToCheck::contains);
this.parent = null;
this.nodeName = null;
this.types = requireNonNull(types);
Expand All @@ -196,7 +197,7 @@ private TypeEditor(
this.checkThisNode =
typesToCheck == null
|| typesToCheck.contains(primary)
|| CollectionUtils.toStream(mixins).anyMatch(typesToCheck::contains);
|| StreamUtils.toStream(mixins).anyMatch(typesToCheck::contains);
this.parent = requireNonNull(parent);
this.nodeName = requireNonNull(name);
this.types = parent.types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.apache.jackrabbit.oak.api.Tree;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry;
import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
Expand Down Expand Up @@ -161,7 +162,7 @@ public boolean test(String primary, Set<String> mixins) {
if (primaryTypes != null && primaryTypes.contains(primary)) {
return true;
}
if (mixinTypes != null && CollectionUtils.toStream(mixins).anyMatch(mixinTypes::contains)) {
if (mixinTypes != null && StreamUtils.toStream(mixins).anyMatch(mixinTypes::contains)) {
return true;
}
return false;
Expand All @@ -175,7 +176,7 @@ public boolean test(@Nullable Tree input) {
return true;
}
if (mixinTypes != null
&& CollectionUtils.toStream(TreeUtil.getNames(input, JCR_MIXINTYPES)).anyMatch(mixinTypes::contains)) {
&& StreamUtils.toStream(TreeUtil.getNames(input, JCR_MIXINTYPES)).anyMatch(mixinTypes::contains)) {
return true;
}
}
Expand All @@ -193,7 +194,7 @@ public boolean test(@Nullable NodeState input) {
return true;
}
if (mixinTypes != null
&& CollectionUtils.toStream(input.getNames(JCR_MIXINTYPES)).anyMatch(mixinTypes::contains)) {
&& StreamUtils.toStream(input.getNames(JCR_MIXINTYPES)).anyMatch(mixinTypes::contains)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.commons.PathUtils;
import org.apache.jackrabbit.oak.commons.PerfLogger;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
import org.apache.jackrabbit.oak.commons.conditions.Validate;
import org.apache.jackrabbit.oak.commons.properties.SystemPropertySupplier;
import org.apache.jackrabbit.oak.plugins.index.lucene.util.fv.SimSearchUtils;
Expand Down Expand Up @@ -1088,7 +1088,7 @@ private static void addNonFullTextConstraints(List<Query> qs,
// deduced
if (planResult.isPathTransformed()) {
String parentPathSegment = planResult.getParentPathSegment();
if (!CollectionUtils.toStream(PathUtils.elements(parentPathSegment)).anyMatch("*"::equals)) {
if (!StreamUtils.toStream(PathUtils.elements(parentPathSegment)).anyMatch("*"::equals)) {
qs.add(new TermQuery(newPathTerm(path + parentPathSegment)));
}
} else {
Expand All @@ -1106,7 +1106,7 @@ private static void addNonFullTextConstraints(List<Query> qs,
// deduced
if (planResult.isPathTransformed()) {
String parentPathSegment = planResult.getParentPathSegment();
if (!CollectionUtils.toStream(PathUtils.elements(parentPathSegment)).anyMatch("*"::equals)) {
if (!StreamUtils.toStream(PathUtils.elements(parentPathSegment)).anyMatch("*"::equals)) {
qs.add(new TermQuery(newPathTerm(getParentPath(path) + parentPathSegment)));
}
} else {
Expand Down
Loading

0 comments on commit 8dca855

Please sign in to comment.