com.liferay.portal.kernel.util.SetUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.portal.kernel Show documentation
Show all versions of com.liferay.portal.kernel Show documentation
Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.
/**
* 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.portal.kernel.io.unsync.UnsyncBufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
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;
}
public static Set fromCollection(Collection extends E> c) {
if ((c != null) && (c instanceof Set)) {
return (Set)c;
}
if ((c == null) || c.isEmpty()) {
return new HashSet<>();
}
return new HashSet<>(c);
}
public static Set fromEnumeration(Enumeration extends E> 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<>();
try (UnsyncBufferedReader unsyncBufferedReader =
new UnsyncBufferedReader(new FileReader(file))) {
String s = StringPool.BLANK;
while ((s = unsyncBufferedReader.readLine()) != null) {
set.add(s);
}
}
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 extends E> array) {
if (ListUtil.isEmpty(array)) {
return new HashSet<>();
}
return new HashSet<>(array);
}
public static Set fromString(String s) {
return fromArray(StringUtil.splitLines(s));
}
public static Set intersect(
Collection collection1, Collection collection2) {
if (collection1.isEmpty() || collection2.isEmpty()) {
return Collections.emptySet();
}
Set set1 = _toSet(collection1);
Set set2 = _toSet(collection2);
if (set1.size() > set2.size()) {
set2.retainAll(set1);
return set2;
}
set1.retainAll(set2);
return set1;
}
public static Set intersect(long[] array1, long[] array2) {
return intersect(fromArray(array1), fromArray(array2));
}
public static boolean isEmpty(Set> set) {
if ((set == null) || set.isEmpty()) {
return true;
}
return false;
}
public static boolean isNotEmpty(Set> set) {
return !isEmpty(set);
}
public static Set symmetricDifference(
Collection collection1, Collection collection2) {
if (collection1.isEmpty()) {
return _toSet(collection2);
}
if (collection2.isEmpty()) {
return _toSet(collection1);
}
Set set1 = _toSet(collection1);
Set set2 = _toSet(collection2);
Set intersection = intersect(set1, set2);
if (set1.size() > set2.size()) {
set1.addAll(set2);
}
else {
set2.addAll(set1);
set1 = set2;
}
set1.removeAll(intersection);
return set1;
}
public static Set symmetricDifference(long[] array1, long[] array2) {
return symmetricDifference(fromArray(array1), fromArray(array2));
}
private static Set _toSet(Collection collection) {
if (collection instanceof Set) {
return (Set)collection;
}
return new HashSet<>(collection);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy