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

com.proofpoint.discovery.ConfigStore Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016 Proofpoint, Inc.
 *
 * 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 com.proofpoint.discovery;

import com.google.auto.value.AutoValue;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Multimap;
import com.google.common.collect.Table;

import javax.inject.Inject;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.stream.Stream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNullElse;

public class ConfigStore
{
    private final Table> table;

    @Inject
    public ConfigStore(final ConfigStoreConfig config)
    {
        Multimap multimap = HashMultimap.create();
        for (Entry entry : config.getAnnouncements().entrySet()) {
            Service service = new Service(
                    Id.valueOf(UUID.nameUUIDFromBytes(entry.getKey().getBytes(UTF_8))),
                    null,
                    entry.getValue().getType(),
                    entry.getValue().getPool(),
                    "/somewhere/" + entry.getKey(),
                    entry.getValue().getProperties());
            multimap.put(new AutoValue_ConfigStore_TypeAndPool(entry.getValue().getType(), entry.getValue().getPool()), service);
        }

        ImmutableTable.Builder> builder = ImmutableTable.builder();
        for (Entry> entry : multimap.asMap().entrySet()) {
            builder.put(entry.getKey().getType(), entry.getKey().getPool(), List.copyOf(entry.getValue()));
        }

        table = builder.build();
    }

    public Stream getAll()
    {
        return table.values().stream()
                .flatMap(Collection::stream);
    }

    public Stream get(String type)
    {
        return table.row(type).values().stream()
                .flatMap(Collection::stream);
    }

    public Stream get(String type, final String pool)
    {
        return requireNonNullElse(table.get(type, pool), List.of()).stream();
    }

    @AutoValue
    abstract static class TypeAndPool
    {
        abstract String getType();

        abstract String getPool();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy