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

com.hotels.styx.api.service.spi.AbstractRegistry Maven / Gradle / Ivy

There is a newer version: 1.0.0.beta9
Show newest version
/*
  Copyright (C) 2013-2018 Expedia 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.hotels.styx.api.service.spi;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.MapDifference;
import com.hotels.styx.api.Announcer;
import com.hotels.styx.api.Id;
import com.hotels.styx.api.Identifiable;
import org.slf4j.Logger;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Maps.difference;
import static com.google.common.collect.Maps.filterKeys;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.StreamSupport.stream;
import static org.slf4j.LoggerFactory.getLogger;

/**
 * Abstract registry.
 *
 * @param  the type of resource to store
 */
public abstract class AbstractRegistry implements Registry {
    private static final Logger LOG = getLogger(AbstractRegistry.class);

    private final Announcer announcer = Announcer.to(ChangeListener.class);
    private final AtomicReference> snapshot = new AtomicReference<>(emptyList());
    private final Predicate> resourceConstraint;

    /**
     * Constructs an abstract registry with no resource constraint.
     */
    public AbstractRegistry() {
        this(any -> true);
    }

    /**
     * Constructs an abstract registry that will not allow resources to be set if they do not
     * satisfy a particular constraint.
     *
     * @param resourceConstraint a constraint to be satisfied
     */
    public AbstractRegistry(Predicate> resourceConstraint) {
        this.resourceConstraint = requireNonNull(resourceConstraint);
    }

    @Override
    public Iterable get() {
        return snapshot.get();
    }

    protected void notifyListeners(Changes changes) {
        LOG.info("notifying about services={} to listeners={}", changes, announcer.listeners());
        announcer.announce().onChange(changes);
    }

    @Override
    public Registry addListener(ChangeListener changeListener) {
        announcer.addListener(changeListener);
        changeListener.onChange(added(snapshot.get()));
        return this;
    }

    /**
     * Sets the contents of this registry to the supplied resources.
     *
     * @param newObjects new contents
     * @throws IllegalStateException if the resource constraint is not satisfied
     */
    public void set(Iterable newObjects) throws IllegalStateException {
        ImmutableList newSnapshot = ImmutableList.copyOf(newObjects);

        checkState(resourceConstraint.test(newSnapshot), "Resource constraint failure");

        Iterable oldSnapshot = snapshot.get();
        snapshot.set(newSnapshot);

        Changes changes = changes(newSnapshot, oldSnapshot);
        if (!changes.isEmpty()) {
            notifyListeners(changes);
        }
    }

    private Changes added(Iterable ch) {
        return new Changes.Builder().added(ch).build();
    }

    @Override
    public Registry removeListener(ChangeListener changeListener) {
        announcer.removeListener(changeListener);
        return this;
    }

    protected static  Changes changes(Iterable newResources, Iterable currentResources) {
        Map newIdsToResource = mapById(newResources);
        Map currentIdsToResource = mapById(currentResources);

        MapDifference diff = difference(newIdsToResource, currentIdsToResource);

        Map> diffs = diff.entriesDiffering();
        return new Changes.Builder()
                .added(diff.entriesOnlyOnLeft().values())
                .removed(diff.entriesOnlyOnRight().values())
                .updated(filterKeys(newIdsToResource, diffs::containsKey).values())
                .build();
    }

    private static  Map mapById(Iterable resources) {
        return stream(resources.spliterator(), false)
                .collect(toMap(T::id, identity()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy