001/*
002 * VM-Operator
003 * Copyright (C) 2024 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.vmoperator.manager.events;
020
021import java.util.Collection;
022import java.util.Optional;
023import java.util.Set;
024import org.jgrapes.core.Channel;
025
026/**
027 * Supports the lookup of a channel by a name (an id). As a convenience,
028 * it is possible to additionally associate arbitrary data with the entry
029 * (and thus with the channel). Note that this interface defines a
030 * read-only view of the dictionary.
031 *
032 * @param <K> the key type
033 * @param <C> the channel type
034 * @param <A> the type of the associated data
035 */
036public interface ChannelDictionary<K, C extends Channel, A> {
037
038    /**
039     * Combines the channel and the associated data.
040     *
041     * @param <C> the channel type
042     * @param <A> the type of the associated data
043     * @param channel the channel
044     * @param associated the associated
045     */
046    public record Value<C extends Channel, A>(C channel, A associated) {
047    }
048
049    /**
050     * Returns all known keys.
051     *
052     * @return the keys
053     */
054    Set<K> keys();
055
056    /**
057     * Return all known values.
058     *
059     * @return the collection
060     */
061    Collection<Value<C, A>> values();
062
063    /**
064     * Returns the channel and associates data registered for the key
065     * or an empty optional if no entry exists.
066     * 
067     * @param key the key
068     * @return the result
069     */
070    Optional<Value<C, A>> value(K key);
071
072    /**
073     * Return all known channels.
074     *
075     * @return the collection
076     */
077    default Collection<C> channels() {
078        return values().stream().map(v -> v.channel).toList();
079    }
080
081    /**
082     * Returns the channel registered for the key or an empty optional
083     * if no mapping exists.
084     *
085     * @param key the key
086     * @return the optional
087     */
088    default Optional<C> channel(K key) {
089        return value(key).map(b -> b.channel);
090    }
091
092    /**
093     * Returns all known associated data.
094     *
095     * @return the collection
096     */
097    default Collection<A> associated() {
098        return values().stream()
099            .filter(v -> v.associated() != null)
100            .map(v -> v.associated).toList();
101    }
102
103    /**
104     * Return the data associated with the entry for the channel.
105     *
106     * @param key the key
107     * @return the data
108     */
109    default Optional<A> associated(K key) {
110        return value(key).map(b -> b.associated);
111    }
112}