forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polish apache#5313 : [Migration] migrate the code in common module fr…
…om cloud-native branch to master
- Loading branch information
1 parent
4278dc1
commit 3734800
Showing
10 changed files
with
561 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
dubbo-common/src/main/java/org/apache/dubbo/common/function/Predicates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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.dubbo.common.function; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import static java.util.stream.Stream.of; | ||
|
||
/** | ||
* The utilities class for Java {@link Predicate} | ||
* | ||
* @since 2.7.5 | ||
*/ | ||
public interface Predicates { | ||
|
||
Predicate[] EMPTY_ARRAY = new Predicate[0]; | ||
|
||
/** | ||
* {@link Predicate} always return <code>true</code> | ||
* | ||
* @param <T> the type to test | ||
* @return <code>true</code> | ||
*/ | ||
static <T> Predicate<T> alwaysTrue() { | ||
return e -> true; | ||
} | ||
|
||
/** | ||
* {@link Predicate} always return <code>false</code> | ||
* | ||
* @param <T> the type to test | ||
* @return <code>false</code> | ||
*/ | ||
static <T> Predicate<T> alwaysFalse() { | ||
return e -> false; | ||
} | ||
|
||
/** | ||
* a composed predicate that represents a short-circuiting logical AND of {@link Predicate predicates} | ||
* | ||
* @param predicates {@link Predicate predicates} | ||
* @param <T> the type to test | ||
* @return non-null | ||
*/ | ||
static <T> Predicate<T> and(Predicate<T>... predicates) { | ||
return of(predicates).reduce((a, b) -> a.and(b)).orElseGet(Predicates::alwaysTrue); | ||
} | ||
|
||
/** | ||
* a composed predicate that represents a short-circuiting logical OR of {@link Predicate predicates} | ||
* | ||
* @param predicates {@link Predicate predicates} | ||
* @param <T> the detected type | ||
* @return non-null | ||
*/ | ||
static <T> Predicate<T> or(Predicate<T>... predicates) { | ||
return of(predicates).reduce((a, b) -> a.or(b)).orElse(e -> true); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
dubbo-common/src/main/java/org/apache/dubbo/common/function/Streams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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.dubbo.common.function; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static java.util.stream.StreamSupport.stream; | ||
import static org.apache.dubbo.common.function.Predicates.and; | ||
import static org.apache.dubbo.common.function.Predicates.or; | ||
|
||
/** | ||
* The utilities class for {@link Stream} | ||
* | ||
* @since 2.7.5 | ||
*/ | ||
public interface Streams { | ||
|
||
static <T, S extends Iterable<T>> Stream<T> filterStream(S values, Predicate<T> predicate) { | ||
return stream(values.spliterator(), false).filter(predicate); | ||
} | ||
|
||
static <T, S extends Iterable<T>> List<T> filterList(S values, Predicate<T> predicate) { | ||
return filterStream(values, predicate).collect(toList()); | ||
} | ||
|
||
static <T, S extends Iterable<T>> Set<T> filterSet(S values, Predicate<T> predicate) { | ||
// new Set with insertion order | ||
return filterStream(values, predicate).collect(LinkedHashSet::new, Set::add, Set::addAll); | ||
} | ||
|
||
static <T, S extends Iterable<T>> S filter(S values, Predicate<T> predicate) { | ||
final boolean isSet = Set.class.isAssignableFrom(values.getClass()); | ||
return (S) (isSet ? filterSet(values, predicate) : filterList(values, predicate)); | ||
} | ||
|
||
static <T, S extends Iterable<T>> S filterAll(S values, Predicate<T>... predicates) { | ||
return filter(values, and(predicates)); | ||
} | ||
|
||
static <T, S extends Iterable<T>> S filterAny(S values, Predicate<T>... predicates) { | ||
return filter(values, or(predicates)); | ||
} | ||
|
||
static <T> T filterFirst(Iterable<T> values, Predicate<T>... predicates) { | ||
return stream(values.spliterator(), false) | ||
.filter(and(predicates)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
dubbo-common/src/test/java/org/apache/dubbo/common/function/PredicatesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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.dubbo.common.function; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.apache.dubbo.common.function.Predicates.alwaysFalse; | ||
import static org.apache.dubbo.common.function.Predicates.alwaysTrue; | ||
import static org.apache.dubbo.common.function.Predicates.and; | ||
import static org.apache.dubbo.common.function.Predicates.or; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
/** | ||
* {@link Predicates} Test | ||
* | ||
* @since 2.7.5 | ||
*/ | ||
public class PredicatesTest { | ||
|
||
@Test | ||
public void testAlwaysTrue() { | ||
assertTrue(alwaysTrue().test(null)); | ||
} | ||
|
||
@Test | ||
public void testAlwaysFalse() { | ||
assertFalse(alwaysFalse().test(null)); | ||
} | ||
|
||
@Test | ||
public void testAnd() { | ||
assertTrue(and(alwaysTrue(), alwaysTrue(), alwaysTrue()).test(null)); | ||
assertFalse(and(alwaysFalse(), alwaysFalse(), alwaysFalse()).test(null)); | ||
assertFalse(and(alwaysTrue(), alwaysFalse(), alwaysFalse()).test(null)); | ||
assertFalse(and(alwaysTrue(), alwaysTrue(), alwaysFalse()).test(null)); | ||
} | ||
|
||
@Test | ||
public void testOr() { | ||
assertTrue(or(alwaysTrue(), alwaysTrue(), alwaysTrue()).test(null)); | ||
assertTrue(or(alwaysTrue(), alwaysTrue(), alwaysFalse()).test(null)); | ||
assertTrue(or(alwaysTrue(), alwaysFalse(), alwaysFalse()).test(null)); | ||
assertFalse(or(alwaysFalse(), alwaysFalse(), alwaysFalse()).test(null)); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
dubbo-common/src/test/java/org/apache/dubbo/common/function/StreamsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.dubbo.common.function; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.Arrays.asList; | ||
import static java.util.stream.Collectors.toList; | ||
import static org.apache.dubbo.common.function.Streams.filterList; | ||
import static org.apache.dubbo.common.function.Streams.filterSet; | ||
import static org.apache.dubbo.common.function.Streams.filterStream; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* {@link Streams} Test | ||
* | ||
* @since 2.7.5 | ||
*/ | ||
public class StreamsTest { | ||
|
||
@Test | ||
public void testFilterStream() { | ||
Stream<Integer> stream = filterStream(asList(1, 2, 3, 4, 5), i -> i % 2 == 0); | ||
assertEquals(asList(2, 4), stream.collect(toList())); | ||
} | ||
|
||
@Test | ||
public void testFilterList() { | ||
List<Integer> list = filterList(asList(1, 2, 3, 4, 5), i -> i % 2 == 0); | ||
assertEquals(asList(2, 4), list); | ||
} | ||
|
||
@Test | ||
public void testFilterSet() { | ||
Set<Integer> set = filterSet(asList(1, 2, 3, 4, 5), i -> i % 2 == 0); | ||
assertEquals(new LinkedHashSet<>(asList(2, 4)), set); | ||
} | ||
} |
Oops, something went wrong.