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

org.agrona.collections.CollectionUtil Maven / Gradle / Ivy

There is a newer version: 0.9.1
Show newest version
/*
 * Copyright 2014 - 2016 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.agrona.collections;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.ToIntFunction;

/**
 * Utility functions for collection objects.
 */
public class CollectionUtil
{
    /**
     * A getOrDefault that doesn't create garbage if its suppler is non-capturing.
     *
     * @param map to perform the lookup on.
     * @param key on which the lookup is done.
     * @param supplier of the default value if one is not found.
     * @param  type of the key
     * @param  type of the value
     * @return the value if found or a new default which as been added to the map.
     */
    public static  V getOrDefault(final Map map, final K key, final Function supplier)
    {
        V value = map.get(key);
        if (value == null)
        {
            value = supplier.apply(key);
            map.put(key, value);
        }

        return value;
    }

    /**
     * Garbage free sum function.
     *
     * @param values the list of input values
     * @param function function that map each value to an int
     * @param  the value to add up
     * @return the sum of all the int values returned for each member of the list.
     */
    public static  int sum(final List values, final ToIntFunction function)
    {
        int total = 0;

        final int size = values.size();
        for (int i = 0; i < size; i++)
        {
            final V value = values.get(i);
            total += function.applyAsInt(value);
        }

        return total;
    }

    /**
     * Validate that a load factor is greater than 0 and less than 1.0.
     *
     * @param loadFactor to be validated.
     */
    public static void validateLoadFactor(final float loadFactor)
    {
        if (loadFactor <= 0 || loadFactor >= 1.0)
        {
            throw new IllegalArgumentException("Load factors must be > 0.0 and < 1.0");
        }
    }

    /**
     * Validate that a number is a power of two.
     *
     * @param value to be validated.
     */
    public static void validatePositivePowerOfTwo(final int value)
    {
        if (value > 0 && 1 == (value & (value - 1)))
        {
            throw new IllegalStateException("Value must be a positive power of two");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy