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

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

The 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.zodiac.sdk.toolkit.util.AssertUtil;

abstract class AppNamedContributorsMapAdapter implements AppNamedContributors {

    private final Map map;

    private final Function valueAdapter;

    AppNamedContributorsMapAdapter(Map map, Function valueAdapter) {
        AssertUtil.notNull(map, "Map must not be null");
        AssertUtil.notNull(valueAdapter, "ValueAdapter must not be null");
        map.keySet().forEach((key) -> AssertUtil.notNull(key, "Map must not contain null keys"));
        map.values().stream().map(valueAdapter)
            .forEach((value) -> AssertUtil.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