Override hashCode(), equals(), and toString().
[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         /** The number of channels of this format. */
29         private final int channels;
30
31         /** The sampling frequency of this format. */
32         private final int frequency;
33
34         /** The encoding of this format. */
35         private final String encoding;
36
37         /**
38          * Creates a new format.
39          *
40          * @param channels
41          *              The number of channels of this format
42          * @param frequency
43          *              The sampling frequency of this format
44          * @param encoding
45          *              The encoding of this format
46          */
47         public Format(int channels, int frequency, String encoding) {
48                 this.channels = channels;
49                 this.frequency = frequency;
50                 this.encoding = encoding;
51         }
52
53         //
54         // ACCESSORS
55         //
56
57         /**
58          * Returns the number of channels of this format.
59          *
60          * @return The number of channels of this format
61          */
62         public int channels() {
63                 return channels;
64         }
65
66         /**
67          * Returns the sampling frequency of this format.
68          *
69          * @return The sampling frequency of this format
70          */
71         public int frequency() {
72                 return frequency;
73         }
74
75         /**
76          * Returns the encoding of this format
77          *
78          * @return The encoding of this format
79          */
80         public String encoding() {
81                 return encoding;
82         }
83
84         //
85         // OBJECT METHODS
86         //
87
88         @Override
89         public int hashCode() {
90                 return (channels << 16) ^ frequency ^ encoding.hashCode();
91         }
92
93         @Override
94         public boolean equals(Object object) {
95                 if ((object == null) || (getClass() != object.getClass())) {
96                         return false;
97                 }
98                 Format format = (Format) object;
99                 return (format.channels == channels) && (format.frequency == frequency) && format.encoding.equals(encoding());
100         }
101
102         @Override
103         public String toString() {
104                 return String.format("%d Channels, %d Hz, %s", channels, frequency, encoding);
105         }
106
107 }