2188b2a4995d98692873df5c215dfc3b78bfd326
[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         // OBJECT METHODS
95         //
96
97         @Override
98         public int hashCode() {
99                 return (channels << 16) ^ frequency ^ encoding.hashCode();
100         }
101
102         @Override
103         public boolean equals(Object object) {
104                 if ((object == null) || (getClass() != object.getClass())) {
105                         return false;
106                 }
107                 Format format = (Format) object;
108                 return (format.channels == channels) && (format.frequency == frequency) && format.encoding.equals(encoding());
109         }
110
111         @Override
112         public String toString() {
113                 return String.format("%d Channels, %d Hz, %s", channels, frequency, encoding);
114         }
115
116 }