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

org.eclipse.collections.impl.bag.AbstractBag Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2018 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;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringJoiner;

import org.eclipse.collections.api.BooleanIterable;
import org.eclipse.collections.api.ByteIterable;
import org.eclipse.collections.api.CharIterable;
import org.eclipse.collections.api.DoubleIterable;
import org.eclipse.collections.api.FloatIterable;
import org.eclipse.collections.api.IntIterable;
import org.eclipse.collections.api.LongIterable;
import org.eclipse.collections.api.ShortIterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.MutableBagIterable;
import org.eclipse.collections.api.bag.primitive.MutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.MutableByteBag;
import org.eclipse.collections.api.bag.primitive.MutableCharBag;
import org.eclipse.collections.api.bag.primitive.MutableDoubleBag;
import org.eclipse.collections.api.bag.primitive.MutableFloatBag;
import org.eclipse.collections.api.bag.primitive.MutableIntBag;
import org.eclipse.collections.api.bag.primitive.MutableLongBag;
import org.eclipse.collections.api.bag.primitive.MutableShortBag;
import org.eclipse.collections.api.bag.sorted.MutableSortedBag;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.Function3;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleObjectToDoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.FloatObjectToFloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.IntObjectToIntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.LongObjectToLongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.collection.primitive.MutableBooleanCollection;
import org.eclipse.collections.api.collection.primitive.MutableByteCollection;
import org.eclipse.collections.api.collection.primitive.MutableCharCollection;
import org.eclipse.collections.api.collection.primitive.MutableDoubleCollection;
import org.eclipse.collections.api.collection.primitive.MutableFloatCollection;
import org.eclipse.collections.api.collection.primitive.MutableIntCollection;
import org.eclipse.collections.api.collection.primitive.MutableLongCollection;
import org.eclipse.collections.api.collection.primitive.MutableShortCollection;
import org.eclipse.collections.api.factory.SortedSets;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.sorted.MutableSortedSet;
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.AbstractRichIterable;
import org.eclipse.collections.impl.Counter;
import org.eclipse.collections.impl.bag.mutable.HashBag;
import org.eclipse.collections.impl.bag.sorted.mutable.TreeBag;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
import org.eclipse.collections.impl.utility.Iterate;

/**
 * @since 7.0
 */
public abstract class AbstractBag
        extends AbstractRichIterable
        implements Collection, Bag
{
    @Override
    public > R select(Predicate predicate, R target)
    {
        if (target instanceof MutableBagIterable)
        {
            MutableBagIterable targetBag = (MutableBagIterable) target;

            this.forEachWithOccurrences((each, occurrences) -> {
                if (predicate.accept(each))
                {
                    targetBag.addOccurrences(each, occurrences);
                }
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                if (predicate.accept(each))
                {
                    for (int i = 0; i < occurrences; i++)
                    {
                        target.add(each);
                    }
                }
            });
        }
        return target;
    }

    @Override
    public > R selectWith(
            Predicate2 predicate,
            P parameter,
            R target)
    {
        if (target instanceof MutableBagIterable)
        {
            MutableBagIterable targetBag = (MutableBagIterable) target;

            this.forEachWithOccurrences((each, occurrences) -> {
                if (predicate.accept(each, parameter))
                {
                    targetBag.addOccurrences(each, occurrences);
                }
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                if (predicate.accept(each, parameter))
                {
                    for (int i = 0; i < occurrences; i++)
                    {
                        target.add(each);
                    }
                }
            });
        }
        return target;
    }

    @Override
    public > R reject(Predicate predicate, R target)
    {
        if (target instanceof MutableBagIterable)
        {
            MutableBagIterable targetBag = (MutableBagIterable) target;

            this.forEachWithOccurrences((each, occurrences) -> {
                if (!predicate.accept(each))
                {
                    targetBag.addOccurrences(each, occurrences);
                }
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                if (!predicate.accept(each))
                {
                    for (int i = 0; i < occurrences; i++)
                    {
                        target.add(each);
                    }
                }
            });
        }
        return target;
    }

    @Override
    public > R rejectWith(
            Predicate2 predicate,
            P parameter,
            R target)
    {
        if (target instanceof MutableBagIterable)
        {
            MutableBagIterable targetBag = (MutableBagIterable) target;

            this.forEachWithOccurrences((each, occurrences) -> {
                if (!predicate.accept(each, parameter))
                {
                    targetBag.addOccurrences(each, occurrences);
                }
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                if (!predicate.accept(each, parameter))
                {
                    for (int i = 0; i < occurrences; i++)
                    {
                        target.add(each);
                    }
                }
            });
        }
        return target;
    }

    @Override
    public int count(Predicate predicate)
    {
        Counter result = new Counter();
        this.forEachWithOccurrences((each, occurrences) -> {
            if (predicate.accept(each))
            {
                result.add(occurrences);
            }
        });
        return result.getCount();
    }

    @Override
    public > R collect(Function function, R target)
    {
        if (target instanceof MutableBagIterable)
        {
            MutableBagIterable targetBag = (MutableBagIterable) target;

            this.forEachWithOccurrences((each, occurrences) -> targetBag.addOccurrences(function.valueOf(each), occurrences));
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                V value = function.valueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    target.add(value);
                }
            });
        }
        return target;
    }

    @Override
    public > R collectWith(
            Function2 function,
            P parameter,
            R target)
    {
        if (target instanceof MutableBagIterable)
        {
            MutableBagIterable targetBag = (MutableBagIterable) target;

            this.forEachWithOccurrences((each, occurrences) -> targetBag.addOccurrences(function.value(each, parameter), occurrences));
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                V value = function.value(each, parameter);
                for (int i = 0; i < occurrences; i++)
                {
                    target.add(value);
                }
            });
        }
        return target;
    }

    @Override
    public > R collectIf(
            Predicate predicate,
            Function function,
            R target)
    {
        if (target instanceof MutableBagIterable)
        {
            MutableBagIterable targetBag = (MutableBagIterable) target;

            this.forEachWithOccurrences((each, occurrences) -> {
                if (predicate.accept(each))
                {
                    targetBag.addOccurrences(function.valueOf(each), occurrences);
                }
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                if (predicate.accept(each))
                {
                    V value = function.valueOf(each);
                    for (int i = 0; i < occurrences; i++)
                    {
                        target.add(value);
                    }
                }
            });
        }
        return target;
    }

    @Override
    public > R flatCollect(Function> function, R target)
    {
        if (target instanceof MutableBagIterable)
        {
            MutableBagIterable targetBag = (MutableBagIterable) target;

            this.forEachWithOccurrences((each, occurrences) -> {
                Iterable values = function.valueOf(each);
                Iterate.forEach(values, eachValue -> targetBag.addOccurrences(eachValue, occurrences));
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                Iterable values = function.valueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    Iterate.forEach(values, target::add);
                }
            });
        }
        return target;
    }

    @Override
    public  R collectBoolean(BooleanFunction booleanFunction, R target)
    {
        if (target instanceof MutableBooleanBag)
        {
            MutableBooleanBag targetBag = (MutableBooleanBag) target;
            this.forEachWithOccurrences((each, occurrences) -> targetBag.addOccurrences(booleanFunction.booleanValueOf(each), occurrences));
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                boolean value = booleanFunction.booleanValueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    target.add(value);
                }
            });
        }
        return target;
    }

    @Override
    public  R flatCollectBoolean(
            Function function, R target)
    {
        if (target instanceof MutableBooleanBag)
        {
            MutableBooleanBag targetBag = (MutableBooleanBag) target;
            this.forEachWithOccurrences((each, occurrences) -> {
                BooleanIterable values = function.valueOf(each);
                values.forEach(value -> targetBag.addOccurrences(value, occurrences));
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                BooleanIterable value = function.valueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    value.forEach(target::add);
                }
            });
        }
        return target;
    }

    @Override
    public  R collectByte(ByteFunction byteFunction, R target)
    {
        if (target instanceof MutableByteBag)
        {
            MutableByteBag targetBag = (MutableByteBag) target;
            this.forEachWithOccurrences((each, occurrences) -> targetBag.addOccurrences(byteFunction.byteValueOf(each), occurrences));
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                byte value = byteFunction.byteValueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    target.add(value);
                }
            });
        }
        return target;
    }

    @Override
    public  R flatCollectByte(
            Function function, R target)
    {
        if (target instanceof MutableByteBag)
        {
            MutableByteBag targetBag = (MutableByteBag) target;
            this.forEachWithOccurrences((each, occurrences) -> {
                ByteIterable values = function.valueOf(each);
                values.forEach(value -> targetBag.addOccurrences(value, occurrences));
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                ByteIterable value = function.valueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    value.forEach(target::add);
                }
            });
        }
        return target;
    }

    @Override
    public  R collectChar(CharFunction charFunction, R target)
    {
        if (target instanceof MutableCharBag)
        {
            MutableCharBag targetBag = (MutableCharBag) target;
            this.forEachWithOccurrences((each, occurrences) -> targetBag.addOccurrences(charFunction.charValueOf(each), occurrences));
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                char value = charFunction.charValueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    target.add(value);
                }
            });
        }
        return target;
    }

    @Override
    public  R flatCollectChar(
            Function function, R target)
    {
        if (target instanceof MutableCharBag)
        {
            MutableCharBag targetBag = (MutableCharBag) target;
            this.forEachWithOccurrences((each, occurrences) -> {
                CharIterable values = function.valueOf(each);
                values.forEach(value -> targetBag.addOccurrences(value, occurrences));
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                CharIterable value = function.valueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    value.forEach(target::add);
                }
            });
        }
        return target;
    }

    @Override
    public  R collectDouble(DoubleFunction doubleFunction, R target)
    {
        if (target instanceof MutableDoubleBag)
        {
            MutableDoubleBag targetBag = (MutableDoubleBag) target;
            this.forEachWithOccurrences((each, occurrences) -> targetBag.addOccurrences(doubleFunction.doubleValueOf(each), occurrences));
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                double value = doubleFunction.doubleValueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    target.add(value);
                }
            });
        }
        return target;
    }

    @Override
    public  R flatCollectDouble(
            Function function,
            R target)
    {
        if (target instanceof MutableDoubleBag)
        {
            MutableDoubleBag targetBag = (MutableDoubleBag) target;
            this.forEachWithOccurrences((each, occurrences) -> {
                DoubleIterable values = function.valueOf(each);
                values.forEach(value -> targetBag.addOccurrences(value, occurrences));
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                DoubleIterable value = function.valueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    value.forEach(target::add);
                }
            });
        }
        return target;
    }

    @Override
    public  R collectFloat(FloatFunction floatFunction, R target)
    {
        if (target instanceof MutableFloatBag)
        {
            MutableFloatBag targetBag = (MutableFloatBag) target;
            this.forEachWithOccurrences((each, occurrences) -> targetBag.addOccurrences(floatFunction.floatValueOf(each), occurrences));
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                float value = floatFunction.floatValueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    target.add(value);
                }
            });
        }
        return target;
    }

    @Override
    public  R flatCollectFloat(
            Function function, R target)
    {
        if (target instanceof MutableFloatBag)
        {
            MutableFloatBag targetBag = (MutableFloatBag) target;
            this.forEachWithOccurrences((each, occurrences) -> {
                FloatIterable values = function.valueOf(each);
                values.forEach(value -> targetBag.addOccurrences(value, occurrences));
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                FloatIterable value = function.valueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    value.forEach(target::add);
                }
            });
        }
        return target;
    }

    @Override
    public  R collectInt(IntFunction intFunction, R target)
    {
        if (target instanceof MutableIntBag)
        {
            MutableIntBag targetBag = (MutableIntBag) target;
            this.forEachWithOccurrences((each, occurrences) -> targetBag.addOccurrences(intFunction.intValueOf(each), occurrences));
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                int value = intFunction.intValueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    target.add(value);
                }
            });
        }
        return target;
    }

    @Override
    public  R flatCollectInt(
            Function function, R target)
    {
        if (target instanceof MutableIntBag)
        {
            MutableIntBag targetBag = (MutableIntBag) target;
            this.forEachWithOccurrences((each, occurrences) -> {
                IntIterable values = function.valueOf(each);
                values.forEach(value -> targetBag.addOccurrences(value, occurrences));
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                IntIterable value = function.valueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    value.forEach(target::add);
                }
            });
        }
        return target;
    }

    @Override
    public  R collectLong(LongFunction longFunction, R target)
    {
        if (target instanceof MutableLongBag)
        {
            MutableLongBag targetBag = (MutableLongBag) target;
            this.forEachWithOccurrences((each, occurrences) -> targetBag.addOccurrences(longFunction.longValueOf(each), occurrences));
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                long value = longFunction.longValueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    target.add(value);
                }
            });
        }
        return target;
    }

    @Override
    public  R flatCollectLong(
            Function function, R target)
    {
        if (target instanceof MutableLongBag)
        {
            MutableLongBag targetBag = (MutableLongBag) target;
            this.forEachWithOccurrences((each, occurrences) -> {
                LongIterable values = function.valueOf(each);
                values.forEach(value -> targetBag.addOccurrences(value, occurrences));
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                LongIterable value = function.valueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    value.forEach(target::add);
                }
            });
        }
        return target;
    }

    @Override
    public  R collectShort(ShortFunction shortFunction, R target)
    {
        if (target instanceof MutableShortBag)
        {
            MutableShortBag targetBag = (MutableShortBag) target;
            this.forEachWithOccurrences((each, occurrences) -> targetBag.addOccurrences(shortFunction.shortValueOf(each), occurrences));
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                short value = shortFunction.shortValueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    target.add(value);
                }
            });
        }
        return target;
    }

    @Override
    public  R flatCollectShort(
            Function function, R target)
    {
        if (target instanceof MutableShortBag)
        {
            MutableShortBag targetBag = (MutableShortBag) target;
            this.forEachWithOccurrences((each, occurrences) -> {
                ShortIterable values = function.valueOf(each);
                values.forEach(value -> targetBag.addOccurrences(value, occurrences));
            });
        }
        else
        {
            this.forEachWithOccurrences((each, occurrences) -> {
                ShortIterable value = function.valueOf(each);
                for (int i = 0; i < occurrences; i++)
                {
                    value.forEach(target::add);
                }
            });
        }
        return target;
    }

    @Override
    public > R groupBy(
            Function function,
            R target)
    {
        this.forEachWithOccurrences((each, occurrences) -> {
            V value = function.valueOf(each);
            target.putAll(value, Collections.nCopies(occurrences, each));
        });
        return target;
    }

    @Override
    public > R groupByEach(
            Function> function,
            R target)
    {
        this.forEachWithOccurrences((each, occurrences) -> {
            Iterable values = function.valueOf(each);
            Iterate.forEach(values, value -> target.putAll(value, Collections.nCopies(occurrences, each)));
        });
        return target;
    }

    @Override
    public long sumOfInt(IntFunction function)
    {
        long[] sum = {0L};
        this.forEachWithOccurrences((each, occurrences) -> {
            int intValue = function.intValueOf(each);
            sum[0] += (long) intValue * (long) occurrences;
        });
        return sum[0];
    }

    @Override
    public double sumOfFloat(FloatFunction function)
    {
        double[] sum = {0.0d};
        double[] compensation = {0.0d};
        this.forEachWithOccurrences((each, occurrences) -> {
            float f = function.floatValueOf(each);
            for (int i = 0; i < occurrences; i++)
            {
                double adjustedValue = f - compensation[0];
                double nextSum = sum[0] + adjustedValue;
                compensation[0] = nextSum - sum[0] - adjustedValue;
                sum[0] = nextSum;
            }
        });
        return sum[0];
    }

    @Override
    public long sumOfLong(LongFunction function)
    {
        long[] sum = {0L};
        this.forEachWithOccurrences((each, occurrences) -> {
            long longValue = function.longValueOf(each);
            sum[0] += longValue * (long) occurrences;
        });
        return sum[0];
    }

    @Override
    public double sumOfDouble(DoubleFunction function)
    {
        double[] sum = {0.0d};
        double[] compensation = {0.0d};
        this.forEachWithOccurrences((each, occurrences) -> {
            double d = function.doubleValueOf(each);
            for (int i = 0; i < occurrences; i++)
            {
                double y = d - compensation[0];
                double t = sum[0] + y;
                compensation[0] = t - sum[0] - y;
                sum[0] = t;
            }
        });
        return sum[0];
    }

    @Override
    public  IV injectInto(IV injectedValue, Function2 function)
    {
        IV[] result = (IV[]) new Object[]{injectedValue};
        this.forEachWithOccurrences((each, occurrences) -> {
            for (int i = 0; i < occurrences; i++)
            {
                result[0] = function.value(result[0], each);
            }
        });
        return result[0];
    }

    @Override
    public int injectInto(int injectedValue, IntObjectToIntFunction function)
    {
        int[] result = {injectedValue};
        this.forEachWithOccurrences((each, occurrences) -> {
            for (int i = 0; i < occurrences; i++)
            {
                result[0] = function.intValueOf(result[0], each);
            }
        });
        return result[0];
    }

    @Override
    public long injectInto(long injectedValue, LongObjectToLongFunction function)
    {
        long[] result = {injectedValue};
        this.forEachWithOccurrences((each, occurrences) -> {
            for (int i = 0; i < occurrences; i++)
            {
                result[0] = function.longValueOf(result[0], each);
            }
        });
        return result[0];
    }

    @Override
    public double injectInto(double injectedValue, DoubleObjectToDoubleFunction function)
    {
        double[] result = {injectedValue};
        this.forEachWithOccurrences((each, occurrences) -> {
            for (int i = 0; i < occurrences; i++)
            {
                result[0] = function.doubleValueOf(result[0], each);
            }
        });
        return result[0];
    }

    @Override
    public float injectInto(float injectedValue, FloatObjectToFloatFunction function)
    {
        float[] result = {injectedValue};
        this.forEachWithOccurrences((each, occurrences) -> {
            for (int i = 0; i < occurrences; i++)
            {
                result[0] = function.floatValueOf(result[0], each);
            }
        });
        return result[0];
    }

    public  IV injectIntoWith(IV injectedValue, Function3 function, P parameter)
    {
        IV[] result = (IV[]) new Object[]{injectedValue};
        this.forEachWithOccurrences((each, occurrences) -> {
            for (int i = 0; i < occurrences; i++)
            {
                result[0] = function.value(result[0], each, parameter);
            }
        });
        return result[0];
    }

    @Override
    public String toStringOfItemToCount()
    {
        StringJoiner joiner = new StringJoiner(", ", "{", "}");
        this.forEachWithOccurrences((each, occurrences) -> joiner.add(each + "=" + occurrences));
        return joiner.toString();
    }

    protected MutableList> toListWithOccurrences()
    {
        MutableList> result = FastList.newList(this.sizeDistinct());
        this.forEachWithOccurrences((each, count) -> result.add(PrimitiveTuples.pair(each, count)));
        return result;
    }

    @Override
    public MutableList toList()
    {
        MutableList result = FastList.newList(this.size());
        this.forEachWithOccurrences((each, occurrences) -> {
            for (int i = 0; i < occurrences; i++)
            {
                result.add(each);
            }
        });
        return result;
    }

    @Override
    public MutableList toSortedList(Comparator comparator)
    {
        MutableList> sorted = this.toListWithOccurrences().sortThis((o1, o2) -> comparator.compare(o1.getOne(), o2.getOne()));

        MutableList result = FastList.newList(this.size());
        sorted.each(each -> {
            T object = each.getOne();
            int occurrences = each.getTwo();
            for (int i = 0; i < occurrences; i++)
            {
                result.add(object);
            }
        });
        return result;
    }

    @Override
    public MutableSet toSet()
    {
        MutableSet result = UnifiedSet.newSet(this.sizeDistinct());
        this.forEachWithOccurrences((each, occurrences) -> result.add(each));
        return result;
    }

    @Override
    public MutableSortedSet toSortedSet()
    {
        MutableSortedSet result = SortedSets.mutable.empty();
        this.forEachWithOccurrences((each, occurrences) -> result.add(each));
        return result;
    }

    @Override
    public MutableSortedSet toSortedSet(Comparator comparator)
    {
        MutableSortedSet result = SortedSets.mutable.with(comparator);
        this.forEachWithOccurrences((each, occurrences) -> result.add(each));
        return result;
    }

    @Override
    public MutableBag toBag()
    {
        MutableBag result = HashBag.newBag(this.sizeDistinct());
        this.forEachWithOccurrences(result::addOccurrences);
        return result;
    }

    @Override
    public MutableSortedBag toSortedBag()
    {
        MutableSortedBag result = TreeBag.newBag();
        this.forEachWithOccurrences(result::addOccurrences);
        return result;
    }

    @Override
    public MutableSortedBag toSortedBag(Comparator comparator)
    {
        MutableSortedBag result = TreeBag.newBag(comparator);
        this.forEachWithOccurrences(result::addOccurrences);
        return result;
    }

    protected MutableList> occurrencesSortingBy(int n, IntFunction> function, MutableList> returnWhenEmpty)
    {
        if (n < 0)
        {
            throw new IllegalArgumentException("Cannot use a value of n < 0");
        }
        if (n == 0)
        {
            return returnWhenEmpty;
        }
        int keySize = Math.min(n, this.sizeDistinct());
        MutableList> sorted = this.toListWithOccurrences().sortThisByInt(function);
        MutableList> results = sorted.subList(0, keySize).toList();
        while (keySize < sorted.size() && results.getLast().getTwo() == sorted.get(keySize).getTwo())
        {
            results.add(sorted.get(keySize));
            keySize++;
        }
        return results;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy