Skip to content

Commit

Permalink
Polish apache#5313 : [Migration] migrate the code in common module fr…
Browse files Browse the repository at this point in the history
…om cloud-native branch to master
  • Loading branch information
mercyblitz committed Nov 12, 2019
1 parent 4278dc1 commit 3734800
Show file tree
Hide file tree
Showing 10 changed files with 561 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* The factory interface to create the instance of {@link DynamicConfiguration}
*/
@SPI("nop")
@SPI("file") // 2.7.5 change the default SPI implementation
public interface DynamicConfigurationFactory {

DynamicConfiguration getDynamicConfiguration(URL url);
Expand Down
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);
}

}
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);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;

public class CollectionUtils {

Expand Down Expand Up @@ -173,7 +179,7 @@ private static boolean objectEquals(Object obj1, Object obj2) {

public static Map<String, String> toStringMap(String... pairs) {
Map<String, String> parameters = new HashMap<>();
if(ArrayUtils.isEmpty(pairs)){
if (ArrayUtils.isEmpty(pairs)) {
return parameters;
}

Expand Down Expand Up @@ -221,4 +227,17 @@ public static boolean isNotEmptyMap(Map map) {
return !isEmptyMap(map);
}

/**
* Convert to multiple values to be {@link LinkedHashSet}
*
* @param values one or more values
* @param <T> the type of <code>values</code>
* @return read-only {@link Set}
*/
public static <T> Set<T> ofSet(T... values) {
if (values == null || values.length < 1) {
return emptySet();
}
return unmodifiableSet(new LinkedHashSet<>(asList(values)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.String.valueOf;
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SEPARATOR;
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN;
import static org.apache.dubbo.common.constants.CommonConstants.DOT_REGEX;
Expand All @@ -58,6 +59,34 @@ public final class StringUtils {
private static final Pattern INT_PATTERN = Pattern.compile("^\\d+$");
private static final int PAD_LIMIT = 8192;

/**
* @since 2.7.5
*/
public static final char EQUAL_CHAR = '=';

public static final String EQUAL = valueOf(EQUAL_CHAR);

public static final char AND_CHAR = '&';

public static final String AND = valueOf(AND_CHAR);

public static final char SEMICOLON_CHAR = ';';

public static final String SEMICOLON = valueOf(SEMICOLON_CHAR);

public static final char QUESTION_MASK_CHAR = '?';

public static final String QUESTION_MASK = valueOf(QUESTION_MASK_CHAR);

public static final char SLASH_CHAR = '/';

public static final String SLASH = valueOf(SLASH_CHAR);

/**
* The empty value
*/
public static final String EMPTY_VALUE = "";

private StringUtils() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@
import static org.apache.dubbo.config.Constants.REGISTRIES_SUFFIX;

public class ConfigManager extends LifecycleAdapter implements FrameworkExt {
public static final String NAME = "config";

private static final Logger logger = LoggerFactory.getLogger(ConfigManager.class);

public static final String NAME = "config";

private final Map<String, Map<String, AbstractConfig>> configsCache = newMap();

private final ReadWriteLock lock = new ReentrantReadWriteLock();
Expand Down
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));
}
}
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);
}
}
Loading

0 comments on commit 3734800

Please sign in to comment.