package com.king.consistenthash.continuum;

import com.king.consistenthash.configuration.NodeConfiguration;

import java.util.SortedSet;

public interface ConfigurableContinuum<NODE extends Comparable<? super NODE>> extends Continuum<NODE> {
    /**
     * Sets a new configuration for the Continuum. Nodes that are new, changed or removed will be passed to the
     * registered NodeListeners. All 'onNodeAdded' callbacks will be invoked before the new configuration can
     * be accessed through 'getNode' and all 'onNodeRemoved' callbacks will be invoked afterwards.
     *
     * @param configuration The new or initial configuration for the Continuum.
     */
    void setConfiguration(SortedSet<NodeConfiguration<NODE>> configuration);

    /**
     * Registers a NodeListener to be able to perform actions based on newly added or removed nodes.
     *
     * @param listener
     */
    void addNodeConfigurationListener(NodeListener<NODE> listener);

    interface NodeListener<NODE extends Comparable<? super NODE>> {
        /**
         * Called for each new node added to a configuration.
         *
         * @param nodeConfiguration The configuration for the added node.
         */
        void onNodeAdded(NodeConfiguration<NODE> nodeConfiguration);

        /**
         * Called for each node that has removed from the configuration.
         *
         * @param nodeConfiguration The configuration for the removed node.
         */
        void onNodeRemoved(NodeConfiguration<NODE> nodeConfiguration);
    }
}
