com.mindoo.domino.jna.utils.SetUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of domino-jna Show documentation
Show all versions of domino-jna Show documentation
Java project to access the HCL Domino C API using Java Native Access (JNA)
package com.mindoo.domino.jna.utils;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* Utility class to work with sets
*
* @author Karsten Lehmann
*/
public class SetUtil {
/**
* Intersects the specified sets and returns a set that contains elements that
* exist in all of the sets.
*
* @param ids sets
* @return intersection of sets
*/
public static Set and(Collection> ids) {
if (ids==null || ids.size()==0) {
return Collections.emptySet();
}
else if (ids.size()==1) {
return new HashSet(ids.iterator().next());
}
else {
Iterator> idSetsIt = ids.iterator();
Set idsOfAll = new HashSet(idSetsIt.next());
while (idSetsIt.hasNext()) {
idsOfAll.retainAll(idSetsIt.next());
}
return idsOfAll;
}
}
/**
* Intersects the specified sets and returns a set that contains elements that
* exist in all of the sets.
*
* @param ids sets
* @return intersection of sets
*/
public static Set and(Set... ids) {
if (ids==null || ids.length==0) {
return Collections.emptySet();
}
else if (ids.length==1) {
return new HashSet(ids[0]);
}
else {
Set idsOfAll = new HashSet(ids[0]);
for (int i=1; i or(Collection> ids) {
Set allIds = new HashSet();
if (ids!=null) {
for (Set currSet : ids) {
allIds.addAll(currSet);
}
}
return allIds;
}
/**
* Merges the specified sets and returns a set with elements from all sets
*
* @param ids ids to merge
* @return merge result
*/
public static Set or(Set... ids) {
Set allIds = new HashSet();
if (ids!=null) {
for (int i=0; i set) {
int[] arr = new int[set.size()];
int i=0;
Iterator values = set.iterator();
while (values.hasNext()) {
arr[i++] = values.next().intValue();
}
return arr;
}
/**
* Converts an int array to a {@link LinkedHashSet} keeping the original order
*
* @param arr int array
* @return set
*/
public static LinkedHashSet fromPrimitiveArray(int[] arr) {
LinkedHashSet set = new LinkedHashSet();
for (int i=0; i