/* * Copyright (c) 2015 Spotify AB * * 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.spotify.dns; import java.util.Set; /** * A change notifier represents a watched lookup from a {@link DnsSrvWatcher}. * *

The records can be of any type. Usually something that directly reflects what your * application will use the records for. * *

A {@link Listener} can be attached to listen to change events on the watched set of records. * * @param The records type */ public interface ChangeNotifier { /** * Get the current set of records. * * @return The current set of records */ Set current(); /** * Set a listener to be called when the set of records change. * *

One one listener can be added. Multiple calls to this method is an error. * * @param listener The listener to set * @param fire Fire the notification event immediately. Can be used to ensure that no updates * are missed when setting the listener * @throws IllegalStateException if called more than once */ void setListener(Listener listener, boolean fire); /** * Close this {@link ChangeNotifier}, releasing any resources allocated. Once closed, no more * {@link Listener} events will be fired. Implementations of {@link ChangeNotifier} are not * allowed to throw checked exceptions from close(). */ void close(); /** * A listener which will be called when the set of records change */ @FunctionalInterface interface Listener { /** * Signal that set of records changed. * * @param changeNotification An object containing details about the change */ void onChange(ChangeNotification changeNotification); } /** * A change event containing the current and previous set of records. */ interface ChangeNotification { Set current(); Set previous(); } }