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

org.javafunk.funk.Maps Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2011-Present Funk committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 */
package org.javafunk.funk;

import org.javafunk.funk.functors.Mapper;
import org.javafunk.funk.functors.functions.NullaryFunction;
import org.javafunk.funk.functors.functions.UnaryFunction;

import java.util.Map;
import java.util.Set;

public class Maps {
    private Maps() {}

    public static  V getOrAdd(Map map, U key, UnaryFunction mapper) {
        if (map.containsKey(key)) {
            return map.get(key);
        } else {
            V newValue = mapper.call(key);
            map.put(key, newValue);
            return newValue;
        }
    }

    public static  V getOrAdd(Map map, U key, final NullaryFunction factory) {
        return getOrAdd(map, key, new Mapper() {
            @Override public V map(U input) {
                return factory.call();
            }
        });
    }

    public static  Mapper, Set>> toEntrySet() {
        return new Mapper, Set>>() {
            @Override public Set> map(Map input) {
                return input.entrySet();
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy