/*
 * Copyright 2014 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 uk.co.real_logic.agrona.collections;

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

/**
 * Utility functions for collection objects in general.
 */
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 <K> type of the key
     * @param <V> type of the value
     * @return the value if found or a new default which as been added to the map.
     */
    public static <K, V> V getOrDefault(final Map<K, V> map, final K key, final Function<K, V> supplier)
    {
        V value = map.get(key);
        if (value == null)
        {
            value = supplier.apply(key);
            map.put(key, value);
        }

        return value;
    }
}

@@ -634,16 +634,39 @@
 tions;%0A%0A
+import java.util.List;%0A
 import j
@@ -714,16 +714,57 @@
 unction;
+%0Aimport java.util.function.ToIntFunction;
 %0A%0A/**%0A *
@@ -1558,11 +1558,633 @@
 e;%0A    %7D
+%0A%0A    /**%0A     * Garbage Free sum function.%0A     *%0A     * @param values the list of input values%0A     * @param function function that map each value to an int%0A     * @param %3CV%3E the value to add up%0A     * @return the sum of all the int values returned for each member of the list.%0A     */%0A    public static %3CV%3E int sum(final List%3CV%3E values, final ToIntFunction%3CV%3E function)%0A    %7B%0A        int total = 0;%0A%0A        final int size = values.size();%0A        for (int i = 0; i %3C size; i++)%0A        %7B%0A            final V value = values.get(i);%0A            total += function.applyAsInt(value);%0A        %7D%0A%0A        return total;%0A    %7D
 %0A%7D%0A

add a garbage free sum function