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

com.liferay.portal.kernel.util.SetUtil 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.io.unsync.UnsyncBufferedReader;

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

import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author Brian Wing Shun Chan
 */
public class SetUtil {

	public static Set fromArray(boolean[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new HashSet();
		}

		Set set = new HashSet(array.length);

		for (int i = 0; i < array.length; i++) {
			set.add(array[i]);
		}

		return set;
	}

	public static Set fromArray(byte[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new HashSet();
		}

		Set set = new HashSet(array.length);

		for (int i = 0; i < array.length; i++) {
			set.add(array[i]);
		}

		return set;
	}

	public static Set fromArray(char[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new HashSet();
		}

		Set set = new HashSet(array.length);

		for (int i = 0; i < array.length; i++) {
			set.add(array[i]);
		}

		return set;
	}

	public static Set fromArray(double[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new HashSet();
		}

		Set set = new HashSet(array.length);

		for (int i = 0; i < array.length; i++) {
			set.add(array[i]);
		}

		return set;
	}

	public static  Set fromArray(E[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new HashSet();
		}

		Set set = new HashSet(array.length);

		for (int i = 0; i < array.length; i++) {
			set.add(array[i]);
		}

		return set;
	}

	public static Set fromArray(float[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new HashSet();
		}

		Set set = new HashSet(array.length);

		for (int i = 0; i < array.length; i++) {
			set.add(array[i]);
		}

		return set;
	}

	public static Set fromArray(int[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new HashSet();
		}

		Set set = new HashSet(array.length);

		for (int i = 0; i < array.length; i++) {
			set.add(array[i]);
		}

		return set;
	}

	public static Set fromArray(long[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new HashSet();
		}

		Set set = new HashSet(array.length);

		for (int i = 0; i < array.length; i++) {
			set.add(array[i]);
		}

		return set;
	}

	public static Set fromArray(short[] array) {
		if (ArrayUtil.isEmpty(array)) {
			return new HashSet();
		}

		Set set = new HashSet(array.length);

		for (int i = 0; i < array.length; i++) {
			set.add(array[i]);
		}

		return set;
	}

	@SuppressWarnings("rawtypes")
	public static  Set fromCollection(Collection c) {
		if ((c != null) && Set.class.isAssignableFrom(c.getClass())) {
			return (Set)c;
		}

		if ((c == null) || (c.size() == 0)) {
			return new HashSet();
		}

		return new HashSet(c);
	}

	public static  Set fromEnumeration(Enumeration enu) {
		Set set = new HashSet();

		while (enu.hasMoreElements()) {
			set.add(enu.nextElement());
		}

		return set;
	}

	public static Set fromFile(File file) throws IOException {
		Set set = new HashSet();

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

		String s = StringPool.BLANK;

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

		unsyncBufferedReader.close();

		return set;
	}

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

	public static  Set fromIterator(Iterator itr) {
		Set set = new HashSet();

		while (itr.hasNext()) {
			set.add(itr.next());
		}

		return set;
	}

	public static  Set fromList(List array) {
		if ((array == null) || (array.size() == 0)) {
			return new HashSet();
		}

		return new HashSet(array);
	}

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy