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