Format printed channels nicely.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / data / Channel.java
1 /*
2  * XdccDownloader - Channel.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.xdcc.data;
19
20 import static java.lang.String.format;
21
22 import com.google.common.base.Function;
23
24 /**
25  * Defines a channel in a {@link Network}.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class Channel {
30
31         /** Function to convert a channel to its network. */
32         public static final Function<Channel, Network> TO_NETWORK = new Function<Channel, Network>() {
33                 @Override
34                 public Network apply(Channel channel) {
35                         return channel.network();
36                 }
37         };
38
39         /** The network this channel belongs to. */
40         private final Network network;
41
42         /** The name of the channel. */
43         private final String name;
44
45         /**
46          * Creates a new channel.
47          *
48          * @param network
49          *              The network the channel belongs to
50          * @param name
51          *              The name of the channel
52          */
53         public Channel(Network network, String name) {
54                 this.network = network;
55                 this.name = name;
56         }
57
58         //
59         // ACCESSORS
60         //
61
62         /**
63          * Returns the network this channel belongs to
64          *
65          * @return The network this channel belongs to
66          */
67         public Network network() {
68                 return network;
69         }
70
71         /**
72          * Returns the name of this channel.
73          *
74          * @return The name of this channel
75          */
76         public String name() {
77                 return name;
78         }
79
80         //
81         // OBJECT METHODS
82         //
83
84         @Override
85         public boolean equals(Object object) {
86                 if (!(object instanceof Channel)) {
87                         return false;
88                 }
89                 Channel channel = (Channel) object;
90                 if (!network().equals(channel.network())) {
91                         return false;
92                 }
93                 if (!name().equalsIgnoreCase(channel.name())) {
94                         return false;
95                 }
96                 return true;
97         }
98
99         @Override
100         public int hashCode() {
101                 return network().hashCode() ^ name().hashCode();
102         }
103
104         @Override
105         public String toString() {
106                 return format("%s/%s", name(), network().name());
107         }
108
109 }