001package silicondust;
002/*-
003 * ##########################################################################
004 * TV H/W, EPGs, and Recording
005 * $Id: Channel.java 5999 2020-05-18 16:41:28Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/silicondust/trunk/src/main/java/silicondust/Channel.java $
007 * %%
008 * Copyright (C) 2013 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import java.util.Collection;
024import java.util.Collections;
025import java.util.Comparator;
026import java.util.Objects;
027import java.util.SortedMap;
028import java.util.SortedSet;
029import java.util.TreeMap;
030import java.util.TreeSet;
031
032/**
033 * TV {@link Channel}.
034 *
035 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
036 * @version $Revision: 5999 $
037 */
038public class Channel implements Comparable<Channel> {
039    private static final Comparator<? super Channel> COMPARATOR =
040        Comparator
041        .comparingInt(Channel::getNumber)
042        .thenComparingInt(Channel::getFrequency)
043        .thenComparing(Channel::getMapName);
044
045    /**
046     * Frequency/{@link Channel} {@link SortedMap} of all known
047     * {@link Channel}s.
048     */
049    public static final SortedMap<Integer,SortedSet<Channel>> MAP;
050
051    static {
052        TreeMap<Integer,SortedSet<Channel>> map = new TreeMap<>();
053
054        for (ChannelRangeMap value : ChannelRangeMap.MAP.values()) {
055            for (Channel channel : value.channelSet()) {
056                if (! map.containsKey(channel.getFrequency())) {
057                    map.put(channel.getFrequency(), new TreeSet<Channel>());
058                }
059
060                map.get(channel.getFrequency()).add(channel);
061            }
062        }
063
064        for (int key : new TreeSet<Integer>(map.keySet())) {
065            map.put(key, Collections.unmodifiableSortedSet(map.get(key)));
066        }
067
068        MAP = Collections.unmodifiableSortedMap(map);
069    }
070
071    private final String map;
072    private final int number;
073    private final int frequency;
074
075    /**
076     * Sole constructor.
077     *
078     * @param   map             The {@link Channel} map name.
079     * @param   number          The {@link Channel} number.
080     * @param   frequency       The {@link Channel} frequency.
081     */
082    public Channel(String map, int number, int frequency) {
083        this.map = map;
084        this.number = number;
085        this.frequency = frequency;
086    }
087
088    /**
089     * @return  The {@link Channel} map name.
090     */
091    public String getMapName() { return map; }
092
093    /**
094     * @return  The {@link Channel} number.
095     */
096    public int getNumber() { return number; }
097
098    /**
099     * @return  {@link Channel} (digital center) frequency.
100     */
101    public int getFrequency() { return frequency; }
102
103    @Override
104    public int compareTo(Channel that) {
105        return Objects.compare(this, that, COMPARATOR);
106    }
107
108    @Override
109    public boolean equals(Object object) {
110        return ((object instanceof Channel)
111                    ? (object != null && this.compareTo((Channel) object) == 0)
112                    : super.equals(object));
113    }
114
115    @Override
116    public int hashCode() {
117        return Objects.hash(getNumber(), getFrequency(), getMapName());
118    }
119
120    @Override
121    public String toString() {
122        return getMapName() + ":" +  getNumber() + ":" + getFrequency();
123    }
124}