org.nervousync.utils.CollectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-jdk11 Show documentation
Show all versions of utils-jdk11 Show documentation
Java utility collections, development by Nervousync Studio (NSYC)
/* * Licensed to the Nervousync Studio (NSYC) 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.nervousync.utils; import java.lang.reflect.Array; import java.util.*; /** *
*/ public staticCollection Utilities
* * Current utilities implements features: *Check collection is empty
*Check collection contains target element
*Check two collection contains the same element
*Check collection contains unique element
*Convert object to array list
*Merge array to list
*Merge properties to map
*Find the first match element of collection
* *数据集合工具集
* * 此工具集实现以下功能: *检查集合是否为空
*检查集合是否包含目标对象
*检查两个集合是否包含同一元素
*检查集合是否有唯一元素
*转换对象为列表
*合并数组到列表中
*合并属性信息实例到哈希表中
*从集合中寻找第一个符合要求的元素
* * * @author Steven Wee [email protected] * @version $Revision: 1.2.0 $ $Date: Jan 13, 2010 16:27:49 $ */ public final class CollectionUtils { /** *Private constructor for CollectionUtils
*数据集合工具集的私有构造方法
*/ private CollectionUtils() { } /** *Check given collection instance is empty
* * Returntrue
if the supplied Collection isnull
* or empty. Otherwise, returnfalse
. * *检查给定的集合实例对象是否为空
* 当给定的集合实例对象为null或者集合为空返回true
,否则返回false
* * @param collection collection instance * 集合实例对象 * * @return whether the given Collection is empty * 给定的集合实例对象是否为空 */ public static boolean isEmpty(final Collection> collection) { return (collection == null || collection.isEmpty()); } /** *Check given collection instance is empty
* * Returntrue
if the supplied Collection isnull
* or empty. Otherwise, returnfalse
. * *检查给定的集合实例对象是否为空
* 当给定的集合实例对象为null或者集合为空返回true
,否则返回false
* * @param objects array instance * 数组实例对象 * * @return whether the given Collection is empty * 给定的集合实例对象是否为空 */ public static boolean isEmpty(final Object[] objects) { return (objects == null || objects.length == 0); } /** *Check given map instance is empty
* * Returntrue
if the supplied Map isnull
* or empty. Otherwise, returnfalse
. * *检查给定的Map实例对象是否为空
* 当给定的Map实例对象为null或者集合为空返回true
,否则返回false
* * @param map Map instance * Map实例对象 * * @return whether the given map is empty * 给定的Map实例对象是否为空 */ public static boolean isEmpty(final Map, ?> map) { return (map == null || map.isEmpty()); } /** *Append the given Object to the given array, returning a new array consisting of the input array contents plus the given Object.
*将给定的对象附加到给定的数组,返回一个由输入数组内容加上给定的对象组成的新数组。
* * @param array the array to append to (can benull
) * 要附加到的数组(可以为null
) * @param obj the Object to append * 要附加的对象 * * @return the new array of the same component type; (nevernull
) * 相同组件类型的新数组;(永远不为null
) */ public static Object[] addObjectToArray(final Object[] array, final Object obj) { Class> compType = Object.class; if (array != null) { compType = array.getClass().getComponentType(); } else if (obj != null) { compType = obj.getClass(); } int newArrLength = (array != null ? array.length + 1 : 1); Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength); if (array != null) { System.arraycopy(array, 0, newArr, 0, array.length); } newArr[newArr.length - 1] = obj; return newArr; } /** *Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects).
* Anull
source value will be converted to an empty Object array. *将给定数组(可能是原始数组)转换为对象数组(如果需要原始包装对象)。
*null
源值将转换为空对象数组。 * * @param source the (potentially primitive) array * 可能为基础类型的实例对象(数组) * * @return the corresponding object array (nevernull
) * 相应的对象数组(永远不为null
) * * @throws IllegalArgumentException * if the parameter is not an array * 如果参数不是数组 */ public static Object[] toArray(final Object source) { if (source instanceof Object[]) { return (Object[]) source; } if (source == null) { return new Object[0]; } if (!source.getClass().isArray()) { throw new IllegalArgumentException("Source is not an array: " + source); } int length = Array.getLength(source); if (length == 0) { return new Object[0]; } Class> wrapperType = Array.get(source, 0).getClass(); Object[] newArray = (Object[]) Array.newInstance(wrapperType, length); for (int i = 0; i < length; i++) { newArray[i] = Array.get(source, i); } return newArray; } /** *Convert supplied object (or array) instance to a List
* * Convert the supplied array into a List. A primitive array gets * converted into a List of the appropriate wrapper type. *A
*null
source value will be converted to an empty List. *转换支持的实例对象为列表
* 转换支持的实例对象(数组)为列表。基础数据类型的数据转换为对应包装类对象列表。实例对象为null将返回一个空列表 * * @param source the (potentially primitive) array * 可能为基础类型的实例对象(数组) * * @return the converted List result * 转换后的列表结果 */ public static List> toList(final Object source) { if (source instanceof Collection) { return new ArrayList<>((Collection>) source); } else if (source instanceof Enumeration) { ListT findValueOfType(final Collection collection, final Class type) { if (isEmpty(collection) || type == null) { return null; } return collection.stream() .filter(type::isInstance) .findFirst() .orElse(null); } /** * Find a single value of one of the given types in the given Collection
* * searching the Collection for a value of the first type, then * searching for a value of the second type, etc. * *在给定集合中查找给定类型之一的单个值
* 在 Collection 中搜索第一种类型的值,然后搜索第二种类型的值,依此类推。 * * @param collection the Collection to search * 要查询的集合 * @param types the types to look for, in prioritized order * 要查找的类型(按优先顺序) * * @return * a value of the given type found if there is a clear match, ornull
if none or more than one such value found * 如果存在明确匹配,则找到给定类型的值;如果未找到或找到多个此类值,则返回 null */ public staticT findValueOfTypes(final Collection collection, final Class [] types) { if (isEmpty(collection) || isEmpty(types)) { return null; } for (Class type : types) { T value = findValueOfType(collection, type); if (value != null) { return value; } } return null; } /** * Determine whether the given Collection only contains a single unique object.
*确定给定的集合是否仅包含单个唯一对象。
* * @param collection the Collection to check * 要检查的集合 * * @return * *true
if the collection contains a single reference * or multiple references to the same instance,false
else * * 如果集合包含对同一实例的单个引用或多个引用,则为true
,否则为false
*/ public static boolean hasUniqueObject(final Collection> collection) { if (isEmpty(collection)) { return Boolean.FALSE; } boolean hasCandidate = Boolean.FALSE; Object candidate = null; for (Object elem : collection) { if (!hasCandidate) { hasCandidate = Boolean.TRUE; candidate = elem; } else if (candidate != elem) { return Boolean.FALSE; } } return Boolean.TRUE; } }