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

org.eclipse.collections.impl.set.immutable.ImmutableSetFactoryImpl Maven / Gradle / Ivy

There is a newer version: 11.1.0-r13
Show newest version
/*
 * Copyright (c) 2021 Goldman Sachs and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.collections.impl.set.immutable;

import java.util.Objects;

import org.eclipse.collections.api.factory.set.ImmutableSetFactory;
import org.eclipse.collections.api.set.ImmutableSet;
import org.eclipse.collections.impl.utility.Iterate;

public class ImmutableSetFactoryImpl implements ImmutableSetFactory
{
    public static final ImmutableSetFactory INSTANCE = new ImmutableSetFactoryImpl();

    @Override
    public  ImmutableSet empty()
    {
        return (ImmutableSet) ImmutableEmptySet.INSTANCE;
    }

    @Override
    public  ImmutableSet of()
    {
        return this.empty();
    }

    @Override
    public  ImmutableSet with()
    {
        return this.empty();
    }

    @Override
    public  ImmutableSet of(T one)
    {
        return this.with(one);
    }

    @Override
    public  ImmutableSet with(T one)
    {
        return new ImmutableSingletonSet<>(one);
    }

    @Override
    public  ImmutableSet of(T one, T two)
    {
        return this.with(one, two);
    }

    @Override
    public  ImmutableSet with(T one, T two)
    {
        if (Objects.equals(one, two))
        {
            return this.of(one);
        }
        return new ImmutableDoubletonSet<>(one, two);
    }

    @Override
    public  ImmutableSet of(T one, T two, T three)
    {
        return this.with(one, two, three);
    }

    @Override
    public  ImmutableSet with(T one, T two, T three)
    {
        if (Objects.equals(one, two))
        {
            return this.of(one, three);
        }
        if (Objects.equals(one, three))
        {
            return this.of(one, two);
        }
        if (Objects.equals(two, three))
        {
            return this.of(one, two);
        }
        return new ImmutableTripletonSet<>(one, two, three);
    }

    @Override
    public  ImmutableSet of(T one, T two, T three, T four)
    {
        return this.with(one, two, three, four);
    }

    @Override
    public  ImmutableSet with(T one, T two, T three, T four)
    {
        if (Objects.equals(one, two))
        {
            return this.of(one, three, four);
        }
        if (Objects.equals(one, three))
        {
            return this.of(one, two, four);
        }
        if (Objects.equals(one, four))
        {
            return this.of(one, two, three);
        }
        if (Objects.equals(two, three))
        {
            return this.of(one, two, four);
        }
        if (Objects.equals(two, four))
        {
            return this.of(one, two, three);
        }
        if (Objects.equals(three, four))
        {
            return this.of(one, two, three);
        }
        return new ImmutableQuadrupletonSet<>(one, two, three, four);
    }

    @Override
    public  ImmutableSet of(T... items)
    {
        return this.with(items);
    }

    @Override
    public  ImmutableSet with(T... items)
    {
        if (items == null || items.length == 0)
        {
            return this.empty();
        }

        switch (items.length)
        {
            case 1:
                return this.of(items[0]);
            case 2:
                return this.of(items[0], items[1]);
            case 3:
                return this.of(items[0], items[1], items[2]);
            case 4:
                return this.of(items[0], items[1], items[2], items[3]);
            default:
                return ImmutableUnifiedSet.newSetWith(items);
        }
    }

    @Override
    public  ImmutableSet ofAll(Iterable items)
    {
        return this.withAll(items);
    }

    @Override
    public  ImmutableSet withAll(Iterable items)
    {
        if (Iterate.isEmpty(items))
        {
            return this.empty();
        }
        if (items instanceof ImmutableSet)
        {
            return (ImmutableSet) items;
        }
        return this.with((T[]) Iterate.toArray(items));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy