Compare formats’ encodings disregarding case.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / Format.java
1 /*
2  * Sonitus - Format.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.sonitus.data;
19
20 /**
21  * A format is a combination of a number of channels, a sampling frequency, and
22  * an encoding scheme.
23  *
24  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25  */
26 public class Format {
27
28         /** Constant for an unknown number of channels. */
29         public static final int UNKNOWN_CHANNELS = -1;
30
31         /** Constant for an unknown frequency. */
32         public static final int UNKNOWN_FREQUENCY = -1;
33
34         /** Constant for an unknown format. */
35         public static final String UNKNOWN_ENCODING = "UNKNOWN";
36
37         /** The number of channels of this format. */
38         private final int channels;
39
40         /** The sampling frequency of this format. */
41         private final int frequency;
42
43         /** The encoding of this format. */
44         private final String encoding;
45
46         /**
47          * Creates a new format.
48          *
49          * @param channels
50          *              The number of channels of this format
51          * @param frequency
52          *              The sampling frequency of this format
53          * @param encoding
54          *              The encoding of this format
55          */
56         public Format(int channels, int frequency, String encoding) {
57                 this.channels = channels;
58                 this.frequency = frequency;
59                 this.encoding = encoding;
60         }
61
62         //
63         // ACCESSORS
64         //
65
66         /**
67          * Returns the number of channels of this format.
68          *
69          * @return The number of channels of this format
70          */
71         public int channels() {
72                 return channels;
73         }
74
75         /**
76          * Returns the sampling frequency of this format.
77          *
78          * @return The sampling frequency of this format
79          */
80         public int frequency() {
81                 return frequency;
82         }
83
84         /**
85          * Returns the encoding of this format
86          *
87          * @return The encoding of this format
88          */
89         public String encoding() {
90                 return encoding;
91         }
92
93         //
94         // MUTATORS
95         //
96
97         /**
98          * Returns a format with the same parameters as this format and the given
99          * number of channels.
100          *
101          * @param channels
102          *              The new number of channels
103          * @return A new format with the given number of channels
104          */
105         public Format channels(int channels) {
106                 return new Format(channels, frequency, encoding);
107         }
108
109         /**
110          * Returns a new format with the same parameters as this format and the given
111          * frequency.
112          *
113          * @param frequency
114          *              The new frequency
115          * @return A new format with the given frequency
116          */
117         public Format frequency(int frequency) {
118                 return new Format(channels, frequency, encoding);
119         }
120
121         /**
122          * Returns a new format with the same parameters as this format and the given
123          * encoding.
124          *
125          * @param encoding
126          *              The new encoding
127          * @return A new format with the given encoding
128          */
129         public Format encoding(String encoding) {
130                 return new Format(channels, frequency, encoding);
131         }
132
133         //
134         // OBJECT METHODS
135         //
136
137         @Override
138         public int hashCode() {
139                 return (channels << 16) ^ frequency ^ encoding.toUpperCase().hashCode();
140         }
141
142         @Override
143         public boolean equals(Object object) {
144                 if ((object == null) || (getClass() != object.getClass())) {
145                         return false;
146                 }
147                 Format format = (Format) object;
148                 return (format.channels == channels) && (format.frequency == frequency) && format.encoding.equalsIgnoreCase(encoding());
149         }
150
151         @Override
152         public String toString() {
153                 return String.format("%d Channel%s, %d Hz, %s", channels, channels != 1 ? "s" : "", frequency, encoding);
154         }
155
156 }