All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.portal.kernel.util.ListUtil Maven / Gradle / Ivy

There is a newer version: 7.4.3.112-ga112
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.kernel.util;

import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import java.lang.reflect.Array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.function.ToLongFunction;

/**
 * @author Brian Wing Shun Chan
 * @author Shuyang Zhou
 */
public class ListUtil {

	public static  List copy(List master) {
		if (master == null) {
			return null;
		}

		return new ArrayList<>(master);
	}

	public static  void copy(
		List master, List copy) {

		if ((master == null) || (copy == null)) {
			return;
		}

		copy.clear();

		copy.addAll(master);
	}

	public static  int count(
		List list, PredicateFilter predicateFilter) {

		if (isEmpty(list)) {
			return 0;
		}

		int count = 0;

		for (E element : list) {
			if (predicateFilter.filter(element)) {
				count++;
			}
		}

		return count;
	}

	public static  void distinct(
		List list, Comparator comparator) {

		if (isEmpty(list)) {
			return;
		}

		Set set = new HashSet<>();

		Iterator itr = list.iterator();

		while (itr.hasNext()) {
			E obj = itr.next();

			if (!set.add(obj)) {
				itr.remove();
			}
		}

		if (comparator != null) {
			Collections.sort(list, comparator);
		}
	}

	public static void distinct(List list) {
		distinct(list, null);
	}

	public static  boolean exists(
		List list, PredicateFilter predicateFilter) {

		if (isEmpty(list)) {
			return false;
		}

		for (E element : list) {
			if (predicateFilter.filter(element)) {
				return true;
			}
		}

		return false;
	}

	public static  List filter(
		List inputList, List outputList,
		PredicateFilter predicateFilter) {

		for (T item : inputList) {
			if (predicateFilter.filter(item)) {
				outputList.add(item);
			}
		}

		return outputList;
	}

	public static  List filter(
		List inputList, PredicateFilter predicateFilter) {

		return filter(
			inputList, new ArrayList(inputList.size()), predicateFilter);
	}

	public static  List fromArray(E[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new ArrayList<>();
		}

		return new ArrayList<>(Arrays.asList(array));
	}

	@SuppressWarnings("rawtypes")
	public static  List fromCollection(Collection c) {
		if ((c != null) && (c instanceof List)) {
			return (List)c;
		}

		if ((c == null) || c.isEmpty()) {
			return new ArrayList<>();
		}

		List list = new ArrayList<>(c.size());

		list.addAll(c);

		return list;
	}

	public static  List fromEnumeration(Enumeration enu) {
		List list = new ArrayList<>();

		while (enu.hasMoreElements()) {
			E obj = enu.nextElement();

			list.add(obj);
		}

		return list;
	}

	public static List fromFile(File file) throws IOException {
		if (!file.exists()) {
			return new ArrayList<>();
		}

		List list = new ArrayList<>();

		try (UnsyncBufferedReader unsyncBufferedReader =
				new UnsyncBufferedReader(new FileReader(file))) {

			String s = StringPool.BLANK;

			while ((s = unsyncBufferedReader.readLine()) != null) {
				list.add(s);
			}
		}

		return list;
	}

	public static List fromFile(String fileName) throws IOException {
		return fromFile(new File(fileName));
	}

	public static  List fromMapKeys(Map map) {
		if (MapUtil.isEmpty(map)) {
			return new ArrayList<>();
		}

		return new ArrayList<>(map.keySet());
	}

	public static  List fromMapValues(Map map) {
		if (MapUtil.isEmpty(map)) {
			return new ArrayList<>();
		}

		return new ArrayList<>(map.values());
	}

	public static List fromString(String s) {
		return fromArray(StringUtil.splitLines(s));
	}

	public static List fromString(String s, String delimiter) {
		return fromArray(StringUtil.split(s, delimiter));
	}

	public static boolean isEmpty(List list) {
		if ((list == null) || list.isEmpty()) {
			return true;
		}

		return false;
	}

	public static boolean isNotEmpty(List list) {
		return !isEmpty(list);
	}

	public static boolean isNotNull(List list) {
		return !isNull(list);
	}

	public static boolean isNull(List list) {
		if ((list == null) || list.isEmpty()) {
			return true;
		}

		for (Object bean : list) {
			if (Validator.isNotNull(bean)) {
				return false;
			}
		}

		return true;
	}

	public static boolean isUnmodifiableList(List list) {
		return _UNMODIFIABLE_LIST_CLASS.isInstance(list);
	}

	public static  List remove(List list, List remove) {
		if (isEmpty(list) || isEmpty(remove)) {
			return list;
		}

		list = copy(list);

		for (E element : remove) {
			list.remove(element);
		}

		return list;
	}

	public static  Iterator reverseIterator(List list) {
		final ListIterator listIterator = list.listIterator(list.size());

		return new Iterator() {

			@Override
			public boolean hasNext() {
				return listIterator.hasPrevious();
			}

			@Override
			public E next() {
				return listIterator.previous();
			}

			@Override
			public void remove() {
				listIterator.remove();
			}

		};
	}

	public static  List sort(List list) {
		return sort(list, null);
	}

	public static  List sort(
		List list, Comparator comparator) {

		if (isUnmodifiableList(list)) {
			list = copy(list);
		}

		Collections.sort(list, comparator);

		return list;
	}

	public static  List subList(List list, int start, int end) {
		if (start < 0) {
			start = 0;
		}

		if ((end < 0) || (end > list.size())) {
			end = list.size();
		}

		if (start < end) {
			return list.subList(start, end);
		}

		return Collections.emptyList();
	}

	public static  A[] toArray(
		List list, Accessor accessor) {

		if (isEmpty(list)) {
			return (A[])Array.newInstance(accessor.getAttributeClass(), 0);
		}

		A[] array = (A[])Array.newInstance(
			accessor.getAttributeClass(), list.size());

		for (int i = 0; i < list.size(); i++) {
			T bean = list.get(i);

			A attribute = accessor.get(bean);

			array[i] = attribute;
		}

		return array;
	}

	public static List toList(boolean[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new ArrayList<>();
		}

		List list = new ArrayList<>(array.length);

		for (boolean value : array) {
			list.add(value);
		}

		return list;
	}

	public static List toList(char[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new ArrayList<>();
		}

		List list = new ArrayList<>(array.length);

		for (char value : array) {
			list.add(value);
		}

		return list;
	}

	public static List toList(double[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new ArrayList<>();
		}

		List list = new ArrayList<>(array.length);

		for (double value : array) {
			list.add(value);
		}

		return list;
	}

	public static  List toList(E[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new ArrayList<>();
		}

		return new ArrayList<>(Arrays.asList(array));
	}

	public static List toList(float[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new ArrayList<>();
		}

		List list = new ArrayList<>(array.length);

		for (float value : array) {
			list.add(value);
		}

		return list;
	}

	public static List toList(int[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new ArrayList<>();
		}

		List list = new ArrayList<>(array.length);

		for (int value : array) {
			list.add(value);
		}

		return list;
	}

	public static  List toList(List list, Accessor accessor) {
		List aList = new ArrayList<>(list.size());

		for (T t : list) {
			aList.add(accessor.get(t));
		}

		return aList;
	}

	public static  List toList(List list, Function function) {
		final List result = new ArrayList<>(list.size());

		for (T t : list) {
			result.add(function.apply(t));
		}

		return result;
	}

	public static  List toList(List vlist) {
		return new ArrayList(vlist);
	}

	public static List toList(long[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new ArrayList<>();
		}

		List list = new ArrayList<>(array.length);

		for (long value : array) {
			list.add(value);
		}

		return list;
	}

	public static List toList(short[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new ArrayList<>();
		}

		List list = new ArrayList<>(array.length);

		for (short value : array) {
			list.add(value);
		}

		return list;
	}

	public static  long[] toLongArray(
		List list, Accessor accessor) {

		if (isEmpty(list)) {
			return _EMPTY_LONG_ARRAY;
		}

		long[] array = new long[list.size()];

		for (int i = 0; i < list.size(); i++) {
			T bean = list.get(i);

			Long attribute = accessor.get(bean);

			array[i] = attribute;
		}

		return array;
	}

	public static  long[] toLongArray(
		List list, ToLongFunction toLongFunction) {

		if (isEmpty(list)) {
			return _EMPTY_LONG_ARRAY;
		}

		long[] array = new long[list.size()];

		for (int i = 0; i < list.size(); i++) {
			array[i] = toLongFunction.applyAsLong(list.get(i));
		}

		return array;
	}

	/**
	 * @see ArrayUtil#toString(Object[], Accessor)
	 */
	public static  String toString(
		List list, Accessor accessor) {

		return toString(list, accessor, StringPool.COMMA);
	}

	/**
	 * @see ArrayUtil#toString(Object[], Accessor, String)
	 */
	public static  String toString(
		List list, Accessor accessor, String delimiter) {

		if (isEmpty(list)) {
			return StringPool.BLANK;
		}

		StringBundler sb = new StringBundler(2 * list.size() - 1);

		for (int i = 0; i < list.size(); i++) {
			T bean = list.get(i);

			A attribute = accessor.get(bean);

			if (attribute != null) {
				sb.append(attribute);
			}

			if ((i + 1) != list.size()) {
				sb.append(delimiter);
			}
		}

		return sb.toString();
	}

	/**
	 * @see ArrayUtil#toString(Object[], String)
	 */
	public static String toString(List list, String param) {
		return toString(list, param, StringPool.COMMA);
	}

	/**
	 * @see ArrayUtil#toString(Object[], String, String)
	 */
	public static String toString(
		List list, String param, String delimiter) {

		if (isEmpty(list)) {
			return StringPool.BLANK;
		}

		StringBundler sb = new StringBundler(2 * list.size() - 1);

		for (int i = 0; i < list.size(); i++) {
			Object bean = list.get(i);

			Object value = null;

			if (Validator.isNull(param)) {
				value = String.valueOf(bean);
			}
			else {
				value = BeanPropertiesUtil.getObject(bean, param);
			}

			if (value != null) {
				sb.append(value);
			}

			if ((i + 1) != list.size()) {
				sb.append(delimiter);
			}
		}

		return sb.toString();
	}

	public static  List unique(List list) {
		Set set = new LinkedHashSet<>();

		set.addAll(list);

		if (list.size() == set.size()) {
			return list;
		}

		return new ArrayList<>(set);
	}

	private static final long[] _EMPTY_LONG_ARRAY = {};

	private static final Class> _UNMODIFIABLE_LIST_CLASS;

	static {
		List unmodifiableList = Collections.unmodifiableList(
			new LinkedList());

		_UNMODIFIABLE_LIST_CLASS =
			(Class>)unmodifiableList.getClass();
	}

}