org.apache.dubbo.common.utils.CollectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dubbo Show documentation
Show all versions of dubbo Show documentation
The all in one project of dubbo
/*
* 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.utils;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
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.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;
/**
* Miscellaneous collection utility methods.
* Mainly for internal use within the framework.
*
* @author william.liangf
* @since 2.0.7
*/
public class CollectionUtils {
private static final Comparator SIMPLE_NAME_COMPARATOR = (s1, s2) -> {
if (s1 == null && s2 == null) {
return 0;
}
if (s1 == null) {
return -1;
}
if (s2 == null) {
return 1;
}
int i1 = s1.lastIndexOf('.');
if (i1 >= 0) {
s1 = s1.substring(i1 + 1);
}
int i2 = s2.lastIndexOf('.');
if (i2 >= 0) {
s2 = s2.substring(i2 + 1);
}
return s1.compareToIgnoreCase(s2);
};
private CollectionUtils() {
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static List sort(List list) {
if (isNotEmpty(list)) {
Collections.sort((List) list);
}
return list;
}
public static List sortSimpleName(List list) {
if (list != null && list.size() > 0) {
Collections.sort(list, SIMPLE_NAME_COMPARATOR);
}
return list;
}
public static Map> splitAll(Map> list, String separator) {
if (list == null) {
return null;
}
Map> result = new HashMap<>();
for (Map.Entry> entry : list.entrySet()) {
result.put(entry.getKey(), split(entry.getValue(), separator));
}
return result;
}
public static Map> joinAll(Map> map, String separator) {
if (map == null) {
return null;
}
Map> result = new HashMap<>();
for (Map.Entry> entry : map.entrySet()) {
result.put(entry.getKey(), join(entry.getValue(), separator));
}
return result;
}
public static Map split(List list, String separator) {
if (list == null) {
return null;
}
Map map = new HashMap<>();
if (list.isEmpty()) {
return map;
}
for (String item : list) {
int index = item.indexOf(separator);
if (index == -1) {
map.put(item, "");
} else {
map.put(item.substring(0, index), item.substring(index + 1));
}
}
return map;
}
public static List join(Map map, String separator) {
if (map == null) {
return null;
}
List list = new ArrayList<>();
if (map.size() == 0) {
return list;
}
for (Map.Entry entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (StringUtils.isEmpty(value)) {
list.add(key);
} else {
list.add(key + separator + value);
}
}
return list;
}
public static String join(List list, String separator) {
StringBuilder sb = new StringBuilder();
for (String ele : list) {
if (sb.length() > 0) {
sb.append(separator);
}
sb.append(ele);
}
return sb.toString();
}
public static boolean mapEquals(Map map1, Map map2) {
if (map1 == null && map2 == null) {
return true;
}
if (map1 == null || map2 == null) {
return false;
}
if (map1.size() != map2.size()) {
return false;
}
for (Map.Entry entry : map1.entrySet()) {
Object key = entry.getKey();
Object value1 = entry.getValue();
Object value2 = map2.get(key);
if (!objectEquals(value1, value2)) {
return false;
}
}
return true;
}
private static boolean objectEquals(Object obj1, Object obj2) {
if (obj1 == null && obj2 == null) {
return true;
}
if (obj1 == null || obj2 == null) {
return false;
}
return obj1.equals(obj2);
}
public static Map toStringMap(String... pairs) {
Map parameters = new HashMap<>();
if (ArrayUtils.isEmpty(pairs)) {
return parameters;
}
if (pairs.length > 0) {
if (pairs.length % 2 != 0) {
throw new IllegalArgumentException("pairs must be even.");
}
for (int i = 0; i < pairs.length; i = i + 2) {
parameters.put(pairs[i], pairs[i + 1]);
}
}
return parameters;
}
@SuppressWarnings("unchecked")
public static Map toMap(Object... pairs) {
Map ret = new HashMap<>();
if (pairs == null || pairs.length == 0) {
return ret;
}
if (pairs.length % 2 != 0) {
throw new IllegalArgumentException("Map pairs can not be odd number.");
}
int len = pairs.length / 2;
for (int i = 0; i < len; i++) {
ret.put((K) pairs[2 * i], (V) pairs[2 * i + 1]);
}
return ret;
}
/**
* Return {@code true} if the supplied Collection is {@code null} or empty.
* Otherwise, return {@code false}.
*
* @param collection the Collection to check
* @return whether the given Collection is empty
*/
public static boolean isEmpty(Collection collection) {
return collection == null || collection.isEmpty();
}
/**
* Return {@code true} if the supplied Collection is {@code not null} or not empty.
* Otherwise, return {@code false}.
*
* @param collection the Collection to check
* @return whether the given Collection is not empty
*/
public static boolean isNotEmpty(Collection collection) {
return !isEmpty(collection);
}
/**
* Return {@code true} if the supplied Map is {@code null} or empty.
* Otherwise, return {@code false}.
*
* @param map the Map to check
* @return whether the given Map is empty
*/
public static boolean isEmptyMap(Map map) {
return map == null || map.size() == 0;
}
/**
* Return {@code true} if the supplied Map is {@code not null} or not empty.
* Otherwise, return {@code false}.
*
* @param map the Map to check
* @return whether the given Map is not empty
*/
public static boolean isNotEmptyMap(Map map) {
return !isEmptyMap(map);
}
/**
* Convert to multiple values to be {@link LinkedHashSet}
*
* @param values one or more values
* @param the type of values
* @return read-only {@link Set}
*/
public static Set ofSet(T... values) {
int size = values == null ? 0 : values.length;
if (size < 1) {
return emptySet();
}
float loadFactor = 1f / ((size + 1) * 1.0f);
if (loadFactor > 0.75f) {
loadFactor = 0.75f;
}
Set elements = new LinkedHashSet<>(size, loadFactor);
for (int i = 0; i < size; i++) {
elements.add(values[i]);
}
return unmodifiableSet(elements);
}
/**
* Get the size of the specified {@link Collection}
*
* @param collection the specified {@link Collection}
* @return must be positive number
* @since 2.7.6
*/
public static int size(Collection collection) {
return collection == null ? 0 : collection.size();
}
/**
* Compares the specified collection with another, the main implementation references
* {@link AbstractSet}
*
* @param one {@link Collection}
* @param another {@link Collection}
* @return if equals, return true
, or false
* @since 2.7.6
*/
public static boolean equals(Collection one, Collection another) {
if (one == another) {
return true;
}
if (isEmpty(one) && isEmpty(another)) {
return true;
}
if (size(one) != size(another)) {
return false;
}
try {
return one.containsAll(another);
} catch (ClassCastException | NullPointerException unused) {
return false;
}
}
/**
* Add the multiple values into {@link Collection the specified collection}
*
* @param collection {@link Collection the specified collection}
* @param values the multiple values
* @param the type of values
* @return the effected count after added
* @since 2.7.6
*/
public static int addAll(Collection collection, T... values) {
int size = values == null ? 0 : values.length;
if (collection == null || size < 1) {
return 0;
}
int effectedCount = 0;
for (int i = 0; i < size; i++) {
if (collection.add(values[i])) {
effectedCount++;
}
}
return effectedCount;
}
/**
* Take the first element from the specified collection
*
* @param values the collection object
* @param the type of element of collection
* @return if found, return the first one, or null
* @since 2.7.6
*/
public static T first(Collection values) {
if (isEmpty(values)) {
return null;
}
if (values instanceof List) {
List list = (List) values;
return list.get(0);
} else {
return values.iterator().next();
}
}
}