Skip to content

Commit 8dca855

Browse files
authored
OAK-11357: Extract StreamUtils from CollectionUtils (#1980)
1 parent b363d95 commit 8dca855

File tree

16 files changed

+147
-62
lines changed

16 files changed

+147
-62
lines changed

oak-auth-external/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/AbstractDynamicTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.apache.jackrabbit.api.security.user.Authorizable;
2121
import org.apache.jackrabbit.api.security.user.UserManager;
2222
import org.apache.jackrabbit.oak.api.Root;
23-
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
23+
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
2424
import org.apache.jackrabbit.oak.spi.security.authentication.external.AbstractExternalAuthTest;
2525
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentity;
2626
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalUser;
@@ -116,7 +116,7 @@ protected void sync(@NotNull ExternalIdentity externalIdentity, @NotNull SyncRes
116116

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

128128
@NotNull
129129
static List<String> getPrincipalNames(@NotNull Iterator<Principal> groupPrincipals) {
130-
return CollectionUtils.toStream(groupPrincipals).map(Principal::getName).collect(Collectors.toList());
130+
return StreamUtils.toStream(groupPrincipals).map(Principal::getName).collect(Collectors.toList());
131131
}
132132
}

oak-auth-external/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalGroupPrincipalProviderTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.jackrabbit.oak.api.Tree;
2929
import org.apache.jackrabbit.oak.commons.PathUtils;
3030
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
31+
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
3132
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
3233
import org.apache.jackrabbit.oak.plugins.tree.TreeAware;
3334
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalGroup;
@@ -89,7 +90,7 @@ void sync(@NotNull ExternalUser externalUser) throws Exception {
8990
@NotNull
9091
Set<Principal> getExpectedGroupPrincipals(@NotNull String userId) throws Exception {
9192
if (syncConfig.user().getMembershipNestingDepth() == 1) {
92-
return CollectionUtils.toStream(idp.getUser(userId).getDeclaredGroups()).map(externalIdentityRef -> {
93+
return StreamUtils.toStream(idp.getUser(userId).getDeclaredGroups()).map(externalIdentityRef -> {
9394
try {
9495
return new PrincipalImpl(idp.getIdentity(externalIdentityRef).getPrincipalName());
9596
} catch (ExternalIdentityException e) {

oak-auth-external/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/external/impl/principal/ExternalGroupPrincipalTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.apache.jackrabbit.oak.api.QueryEngine;
2525
import org.apache.jackrabbit.oak.api.Root;
2626
import org.apache.jackrabbit.oak.api.Type;
27-
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
27+
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
2828
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentity;
2929
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef;
3030
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalUser;
@@ -81,7 +81,7 @@ public void testNotIsMember() throws Exception {
8181
public void testIsMemberExternalGroup() throws Exception {
8282
GroupPrincipal principal = getGroupPrincipal();
8383

84-
List<String> exGroupPrincNames = CollectionUtils.toStream(
84+
List<String> exGroupPrincNames = StreamUtils.toStream(
8585
idp.listGroups()).map(ExternalIdentity::getPrincipalName).collect(Collectors.toList());
8686
for (String principalName : exGroupPrincNames) {
8787
assertFalse(principal.isMember(new PrincipalImpl(principalName)));

oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.util.stream.Collectors;
4141
import java.util.stream.IntStream;
4242
import java.util.stream.Stream;
43-
import java.util.stream.StreamSupport;
4443

4544
import org.jetbrains.annotations.NotNull;
4645

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

522-
/**
523-
* Generates a (non-parallel) {@linkplain Stream} for the {@linkplain Iterable}
524-
* @param iterable iterable to convert
525-
* @return the stream
526-
*/
527-
@NotNull
528-
public static <T> Stream<T> toStream(@NotNull Iterable<T> iterable) {
529-
Objects.requireNonNull(iterable);
530-
return StreamSupport.stream(iterable.spliterator(), false);
531-
}
532-
533-
/**
534-
* Generates a (non-parallel) {@linkplain Stream} for the
535-
* {@linkplain Iterable}
536-
* <p>
537-
* This method is not thread-safe
538-
*
539-
* @param iterator
540-
* iterator to convert
541-
* @return the stream (representing the remaining elements in the iterator)
542-
*/
543-
@NotNull
544-
public static <T> Stream<T> toStream(@NotNull Iterator<T> iterator) {
545-
return StreamSupport.stream(toIterable(iterator).spliterator(), false);
546-
}
547-
548521
/**
549522
* Ensure the capacity of a map or set given the expected number of elements.
550523
*
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.jackrabbit.oak.commons.collections;
20+
21+
import org.jetbrains.annotations.NotNull;
22+
23+
import java.util.Iterator;
24+
import java.util.Objects;
25+
import java.util.stream.Stream;
26+
import java.util.stream.StreamSupport;
27+
28+
public class StreamUtils {
29+
30+
private StreamUtils() {
31+
// no instances for you
32+
}
33+
34+
/**
35+
* Generates a (non-parallel) {@linkplain Stream} for the {@linkplain Iterable}
36+
* @param iterable iterable to convert
37+
* @return the stream
38+
*/
39+
@NotNull
40+
public static <T> Stream<T> toStream(@NotNull Iterable<T> iterable) {
41+
Objects.requireNonNull(iterable);
42+
return StreamSupport.stream(iterable.spliterator(), false);
43+
}
44+
45+
/**
46+
* Generates a (non-parallel) {@linkplain Stream} for the
47+
* {@linkplain Iterable}
48+
* <p>
49+
* This method is not thread-safe
50+
*
51+
* @param iterator
52+
* iterator to convert
53+
* @return the stream (representing the remaining elements in the iterator)
54+
*/
55+
@NotNull
56+
public static <T> Stream<T> toStream(@NotNull Iterator<T> iterator) {
57+
return StreamSupport.stream(CollectionUtils.toIterable(iterator).spliterator(), false);
58+
}
59+
}

oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Utilities for Java collections and streams.
2222
*/
2323
@Internal(since = "1.0.0")
24-
@Version("1.2.0")
24+
@Version("2.0.0")
2525
package org.apache.jackrabbit.oak.commons.collections;
2626
import org.apache.jackrabbit.oak.commons.annotations.Internal;
2727
import org.osgi.annotation.versioning.Version;

oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,6 @@ public void iteratorToIIteratable() {
495495
}
496496
}
497497

498-
@Test
499-
public void iteratorToStream() {
500-
List<String> input = List.of("a", "b", "c");
501-
Iterator<String> iterator = input.iterator();
502-
Stream<String> stream = CollectionUtils.toStream(iterator);
503-
List<String> result = stream.collect(Collectors.toList());
504-
Assert.assertEquals(input.toString(), result.toString());
505-
}
506-
507498
@Test
508499
public void testFromProperties() {
509500
final Properties properties = new Properties();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.jackrabbit.oak.commons.collections;
20+
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
import java.util.Iterator;
25+
import java.util.List;
26+
import java.util.stream.Collectors;
27+
import java.util.stream.Stream;
28+
29+
public class StreamUtilsTest {
30+
31+
@Test
32+
public void iteratorToStream() {
33+
List<String> input = List.of("a", "b", "c");
34+
Iterator<String> iterator = input.iterator();
35+
Stream<String> stream = StreamUtils.toStream(iterator);
36+
List<String> result = stream.collect(Collectors.toList());
37+
Assert.assertEquals(input.toString(), result.toString());
38+
}
39+
40+
@Test(expected = NullPointerException.class)
41+
public void iteratorToStreamNull() {
42+
StreamUtils.toStream((Iterator)null);
43+
}
44+
45+
@Test
46+
public void iterableToStream() {
47+
List<String> input = List.of("a", "b", "c");
48+
Stream<String> stream = StreamUtils.toStream(input);
49+
List<String> result = stream.collect(Collectors.toList());
50+
Assert.assertEquals(input.toString(), result.toString());
51+
}
52+
53+
@Test(expected = NullPointerException.class)
54+
public void iterableStreamNull() {
55+
StreamUtils.toStream((Iterable)null);
56+
}
57+
}

oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/commit/MergingNodeStateDiff.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import java.util.stream.Stream;
3838

3939
import org.apache.jackrabbit.oak.api.PropertyState;
40-
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
40+
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
4141
import org.apache.jackrabbit.oak.json.JsopDiff;
4242
import org.apache.jackrabbit.oak.plugins.memory.PropertyBuilder;
4343
import org.apache.jackrabbit.oak.plugins.tree.TreeConstants;
@@ -134,8 +134,8 @@ private void resolveConflict(ConflictType conflictType, NodeState conflictInfo)
134134
NodeState oursNS = conflictInfo.getChildNode(OURS);
135135
NodeState baseNS = conflictInfo.getChildNode(BASE);
136136

137-
Set<String> candidates = Stream.concat(CollectionUtils.toStream(oursNS.getChildNodeNames()),
138-
CollectionUtils.toStream(baseNS.getChildNodeNames())).collect(toSet());
137+
Set<String> candidates = Stream.concat(StreamUtils.toStream(oursNS.getChildNodeNames()),
138+
StreamUtils.toStream(baseNS.getChildNodeNames())).collect(toSet());
139139
for (String name : candidates) {
140140
NodeState ours = oursNS.getChildNode(name);
141141
NodeState base = baseNS.getChildNode(name);

oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexPlan.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.apache.jackrabbit.oak.commons.PathUtils;
2929
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
30+
import org.apache.jackrabbit.oak.commons.collections.StreamUtils;
3031
import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
3132
import org.apache.jackrabbit.oak.plugins.index.IndexUtils;
3233
import org.apache.jackrabbit.oak.plugins.index.cursor.Cursors;
@@ -108,7 +109,7 @@ public class PropertyIndexPlan {
108109
this.matchesAllTypes = !definition.hasProperty(DECLARING_NODE_TYPES);
109110
this.deprecated = definition.getBoolean(IndexConstants.INDEX_DEPRECATED);
110111
this.matchesNodeTypes =
111-
matchesAllTypes || CollectionUtils.toStream(types).anyMatch(filter.getSupertypes()::contains);
112+
matchesAllTypes || StreamUtils.toStream(types).anyMatch(filter.getSupertypes()::contains);
112113

113114
ValuePattern valuePattern = new ValuePattern(definition);
114115

0 commit comments

Comments
 (0)