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

org.kuali.common.util.CollectionUtils Maven / Gradle / Ivy

There is a newer version: 4.4.17
Show newest version
/**
 * Copyright 2010-2012 The Kuali Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 *
 * 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.kuali.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang3.StringUtils;

public class CollectionUtils {

	public static final  List toEmptyList(T o) {
		if (o == null) {
			return Collections. emptyList();
		} else {
			return Collections.singletonList(o);
		}
	}

	public static final  List toEmpty(List list) {
		if (list == null) {
			return Collections. emptyList();
		} else {
			return list;
		}
	}

	public static final  List toNullIfEmpty(List list) {
		if (isEmpty(list)) {
			return null;
		} else {
			return list;
		}
	}

	public static final  Collection toNullIfEmpty(Collection c) {
		if (isEmpty(c)) {
			return null;
		} else {
			return c;
		}
	}

	public static final  List getPreFilledList(int size, T value) {
		if (value == null || size < 1) {
			return Collections. emptyList();
		} else {
			List list = new ArrayList(size);
			for (int i = 0; i < size; i++) {
				list.add(value);
			}
			return list;
		}
	}

	public static final Object[] toArray(List objects) {
		return objects.toArray(new Object[objects.size()]);
	}

	public static final boolean isEmpty(Collection c) {
		return c == null || c.size() == 0;
	}

	public static final List sortedMerge(List list, String csv) {
		Set set = new TreeSet();
		for (String string : toEmpty(list)) {
			set.add(string);
		}
		for (String string : getTrimmedListFromCSV(csv)) {
			set.add(string);
		}
		return new ArrayList(set);
	}

	public static final List getTrimmedListFromCSV(String csv) {
		if (StringUtils.isBlank(csv)) {
			return Collections. emptyList();
		}
		List list = new ArrayList();
		String[] tokens = Str.splitAndTrimCSV(csv);
		list.addAll(Arrays.asList(tokens));
		return list;
	}
}