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

org.eclipse.collections.impl.bag.strategy.mutable.HashBagWithHashingStrategy Maven / Gradle / Ivy

Go to download

Builds the commons-text. Requires eclipse-collections-api be built first and be excluded from any other poms requiring it.

There is a newer version: 11.1.0-r13
Show newest version
/*
 * Copyright (c) 2021 Bhavana Hindupur.
 * 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.strategy.mutable;

import java.io.Serializable;

import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.block.HashingStrategy;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.factory.primitive.ObjectIntHashingStrategyMaps;
import org.eclipse.collections.api.map.primitive.MutableObjectIntMap;
import org.eclipse.collections.impl.bag.mutable.AbstractHashBag;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.Iterate;

public class HashBagWithHashingStrategy
        extends AbstractHashBag
        implements Serializable
{
    private static final long serialVersionUID = 1L;

    private final HashingStrategy hashingStrategy;

    public HashBagWithHashingStrategy(HashingStrategy hashingStrategy)
    {
        if (hashingStrategy == null)
        {
            throw new IllegalArgumentException("Cannot Instantiate HashBagWithHashingStrategy with null HashingStrategy");
        }
        this.hashingStrategy = hashingStrategy;
        this.items = ObjectIntHashingStrategyMaps.mutable.with(hashingStrategy);
    }

    public HashBagWithHashingStrategy(HashingStrategy hashingStrategy, int size)
    {
        if (hashingStrategy == null)
        {
            throw new IllegalArgumentException("Cannot Instantiate HashBagWithHashingStrategy with null HashingStrategy");
        }
        this.hashingStrategy = hashingStrategy;
        this.items = ObjectIntHashingStrategyMaps.mutable.withInitialCapacity(hashingStrategy, size);
    }

    private HashBagWithHashingStrategy(HashingStrategy hashingStrategy, MutableObjectIntMap map)
    {
        this.hashingStrategy = hashingStrategy;
        this.items = map;
        this.size = (int) map.sum();
    }

    public static  HashBagWithHashingStrategy newBag(HashingStrategy hashingStrategy)
    {
        return new HashBagWithHashingStrategy<>(hashingStrategy);
    }

    public static  HashBagWithHashingStrategy newBag(HashingStrategy hashingStrategy, int size)
    {
        return new HashBagWithHashingStrategy<>(hashingStrategy, size);
    }

    public static  HashBagWithHashingStrategy newBag(HashingStrategy hashingStrategy, Bag source)
    {
        HashBagWithHashingStrategy result = HashBagWithHashingStrategy.newBag(hashingStrategy, source.sizeDistinct());
        result.addAllBag(source);
        return result;
    }

    public static  HashBagWithHashingStrategy newBag(HashingStrategy hashingStrategy, Iterable source)
    {
        if (source instanceof Bag)
        {
            return HashBagWithHashingStrategy.newBag(hashingStrategy, (Bag) source);
        }
        return HashBagWithHashingStrategy.newBagWith(hashingStrategy, (E[]) Iterate.toArray(source));
    }

    public static  HashBagWithHashingStrategy newBagWith(HashingStrategy hashingStrategy, E... elements)
    {
        HashBagWithHashingStrategy result = HashBagWithHashingStrategy.newBag(hashingStrategy);
        ArrayIterate.addAllTo(elements, result);
        return result;
    }

    public HashingStrategy hashingStrategy()
    {
        return this.hashingStrategy;
    }

    @Override
    protected int computeHashCode(T item)
    {
        return this.hashingStrategy.computeHashCode(item);
    }

    @Override
    public MutableBag selectByOccurrences(IntPredicate predicate)
    {
        MutableObjectIntMap map = this.items.select((each, occurrences) -> predicate.accept(occurrences));
        return new HashBagWithHashingStrategy<>(this.hashingStrategy, map);
    }

    protected Object writeReplace()
    {
        return new HashBagWithHashingStrategySerializationProxy<>(this);
    }

    @Override
    public MutableBag with(T element)
    {
        this.add(element);
        return this;
    }

    @Override
    public MutableBag without(T element)
    {
        this.remove(element);
        return this;
    }

    @Override
    public MutableBag withAll(Iterable elements)
    {
        this.addAllIterable(elements);
        return this;
    }

    @Override
    public MutableBag withoutAll(Iterable elements)
    {
        this.removeAllIterable(elements);
        return this;
    }

    @Override
    public MutableBag newEmpty()
    {
        return HashBagWithHashingStrategy.newBag(this.hashingStrategy);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy