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

org.openstreetmap.atlas.utilities.collections.Sets Maven / Gradle / Ivy

There is a newer version: 7.0.8
Show newest version
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()
    {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy