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

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

Go to download

Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.

There is a newer version: 7.0.0-nightly
Show newest version
/**
 * Copyright (c) 2000-2013 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.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.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.List;
import java.util.Map;
import java.util.Set;

/**
 * @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 void distinct(List list) {
		distinct(list, null);
	}

	public static  void distinct(List list, Comparator comparator) {
		if ((list == null) || list.isEmpty()) {
			return;
		}

		Set set = new HashSet();

		Iterator itr = list.iterator();

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

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

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

	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) && List.class.isAssignableFrom(c.getClass())) {
			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();

		UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
			new FileReader(file));

		String s = StringPool.BLANK;

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

		unsyncBufferedReader.close();

		return list;
	}

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

	public static  List fromMapKeys(Map map) {
		if ((map == null) || map.isEmpty()) {
			return new ArrayList();
		}

		List list = new ArrayList(map.size());

		for (Map.Entry entry : map.entrySet()) {
			list.add(entry.getKey());
		}

		return list;
	}

	public static  List fromMapValues(Map map) {
		if ((map == null) || map.isEmpty()) {
			return new ArrayList();
		}

		List list = new ArrayList(map.size());

		for (Map.Entry entry : map.entrySet()) {
			list.add(entry.getValue());
		}

		return list;
	}

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

	/**
	 * @deprecated As of 6.2.0
	 */
	public static  boolean remove(List list, E element) {
		Iterator itr = list.iterator();

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

			if ((curElement == element) || curElement.equals(element)) {
				itr.remove();

				return true;
			}
		}

		return false;
	}

	public static  List remove(List list, List remove) {
		if ((list == null) || list.isEmpty() ||
			(remove == null)|| remove.isEmpty()) {

			return list;
		}

		list = copy(list);

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

		return list;
	}

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

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

		if (UnmodifiableList.class.isAssignableFrom(list.getClass())) {
			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 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(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;
	}

	/**
	 * @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 ((list == null) || list.isEmpty()) {
			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();
	}

	/**
	 * @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 ((list == null) || list.isEmpty()) {
			return StringPool.BLANK;
		}

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

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

			V value = accessor.get(bean);

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

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

		return sb.toString();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy