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

org.zodiac.actuate.health.AppNamedContributorsMapAdapter Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.actuate.health;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;

import org.springframework.util.Assert;

abstract class AppNamedContributorsMapAdapter implements AppNamedContributors {

    private final Map map;

    private final Function valueAdapter;

    AppNamedContributorsMapAdapter(Map map, Function valueAdapter) {
        Assert.notNull(map, "Map must not be null");
        Assert.notNull(valueAdapter, "ValueAdapter must not be null");
        map.keySet().forEach((key) -> Assert.notNull(key, "Map must not contain null keys"));
        map.values().stream().map(valueAdapter)
            .forEach((value) -> Assert.notNull(value, "Map must not contain null values"));
        this.map = Collections.unmodifiableMap(new LinkedHashMap<>(map));
        this.valueAdapter = valueAdapter;
    }

    @Override
    public Iterator> iterator() {
        Iterator> iterator = this.map.entrySet().iterator();
        return new Iterator>() {

            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @Override
            public AppNamedContributor next() {
                Entry entry = iterator.next();
                return AppNamedContributor.of(entry.getKey(), adapt(entry.getValue()));
            }

        };
    }

    @Override
    public C getContributor(String name) {
        return adapt(this.map.get(name));
    }

    private C adapt(V value) {
        return (value != null) ? this.valueAdapter.apply(value) : null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy