org.openstreetmap.atlas.utilities.collections.Sets Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of atlas Show documentation
Show all versions of atlas Show documentation
"Library to load OSM data into an Atlas format"
package org.openstreetmap.atlas.utilities.collections;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.openstreetmap.atlas.exception.CoreException;
/**
* @author matthieun
*/
public final class Sets
{
@SafeVarargs
public static Set hashSet(final T... elements)
{
final Set result = new HashSet<>();
for (final T element : elements)
{
result.add(element);
}
return result;
}
@SafeVarargs
public static > SortedSet treeSet(final T... elements)
{
final SortedSet result = new TreeSet<>();
for (final T element : elements)
{
result.add(element);
}
return result;
}
@SafeVarargs
public static Set withSets(final boolean rejectCollisions, final Set... items)
{
if (items.length == 0)
{
return new HashSet<>();
}
if (items.length == 1)
{
return items[0];
}
final Set result = new HashSet<>();
for (final Set item : items)
{
for (final V entry : item)
{
if (rejectCollisions && result.contains(entry))
{
throw new CoreException("Cannot merge sets! Collision on element.");
}
result.add(entry);
}
}
return result;
}
@SafeVarargs
public static Set withSets(final Set... items)
{
return withSets(true, items);
}
@SafeVarargs
public static SortedSet withSortedSets(final boolean rejectCollisions,
final SortedSet... items)
{
if (items.length == 0)
{
return new TreeSet<>();
}
if (items.length == 1)
{
return items[0];
}
final SortedSet result = new TreeSet<>();
for (final SortedSet item : items)
{
for (final V entry : item)
{
if (rejectCollisions && result.contains(entry))
{
throw new CoreException("Cannot merge sets! Collision on element.");
}
result.add(entry);
}
}
return result;
}
@SafeVarargs
public static SortedSet withSortedSets(final SortedSet... items)
{
return withSortedSets(true, items);
}
private Sets()
{
}
}