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

org.eclipse.collections.impl.bag.immutable.ImmutableBagFactoryImpl 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.bag.immutable;

import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.factory.bag.ImmutableBagFactory;
import org.eclipse.collections.impl.utility.Iterate;

public class ImmutableBagFactoryImpl implements ImmutableBagFactory
{
    public static final ImmutableBagFactory INSTANCE = new ImmutableBagFactoryImpl();

    @Override
    public  ImmutableBag empty()
    {
        return (ImmutableBag) ImmutableEmptyBag.INSTANCE;
    }

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

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

    @Override
    public  ImmutableBag of(T element)
    {
        return this.with(element);
    }

    @Override
    public  ImmutableBag with(T element)
    {
        return new ImmutableSingletonBag<>(element);
    }

    @Override
    public  ImmutableBag of(T... elements)
    {
        return this.with(elements);
    }

    @Override
    public  ImmutableBag with(T... elements)
    {
        if (elements == null || elements.length == 0)
        {
            return this.empty();
        }
        if (elements.length == 1)
        {
            return this.of(elements[0]);
        }
        if (elements.length < ImmutableArrayBag.MAXIMUM_USEFUL_ARRAY_BAG_SIZE)
        {
            return ImmutableArrayBag.newBagWith(elements);
        }
        return ImmutableHashBag.newBagWith(elements);
    }

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

    @Override
    public  ImmutableBag withAll(Iterable items)
    {
        if (Iterate.isEmpty(items))
        {
            return this.empty();
        }
        if (items instanceof ImmutableBag)
        {
            return (ImmutableBag) items;
        }
        if (items instanceof Bag)
        {
            Bag bag = (Bag) items;
            if (bag.size() == 1)
            {
                return this.with(bag.getFirst());
            }
            if (bag.sizeDistinct() < ImmutableArrayBag.MAXIMUM_USEFUL_ARRAY_BAG_SIZE)
            {
                return ImmutableArrayBag.copyFrom(bag);
            }
            return new ImmutableHashBag<>(bag);
        }
        return this.of((T[]) Iterate.toArray(items));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy