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

com.liferay.poshi.runner.util.ListUtil Maven / Gradle / Ivy

/**
 * 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.poshi.runner.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

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

		return false;
	}

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

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

		list = copy(list);

		Collections.sort(list, comparator);

		return list;
	}

	public static String sort(String s) {
		return sort(s, StringPool.COMMA);
	}

	public static String sort(String s, String delimiter) {
		List list = Arrays.asList(s.split(delimiter));

		return toString(sort(list), delimiter);
	}

	public static String toString(List list) {
		return toString(list, StringPool.COMMA);
	}

	public static String toString(List list, String delimiter) {
		if (isEmpty(list)) {
			return StringPool.BLANK;
		}

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

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

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

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

		return sb.toString();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy