dubbo Streams 源码
dubbo Streams 代码
文件路径:/dubbo-common/src/main/java/org/apache/dubbo/common/function/Streams.java
/*
* 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);
}
}
相关信息
相关文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦