52cb365c1fe8cb8bbaf5c1eee23ec558983dba51
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / Metadata.java
1 /*
2  * Sonitus - Metainfo.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 import com.google.common.base.Optional;
21
22 /**
23  * Metadata contains information about a source, e.g. the number of channels,
24  * the frequency, the encoding, the name of the content, the artist performing
25  * it, dates, comments, URLs, etc.
26  * <p/>
27  * Metadata, once created, is immutable.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class Metadata {
32
33         /** The format metadata. */
34         private final FormatMetadata formatMetadata;
35
36         /** The content metadata. */
37         private final ContentMetadata contentMetadata;
38
39         /** Creates empty metadata. */
40         public Metadata() {
41                 this(new FormatMetadata(), new ContentMetadata());
42         }
43
44         /**
45          * Creates metadata from the given format and content metadata.
46          *
47          * @param formatMetadata
48          *              The format metadata
49          * @param contentMetadata
50          *              The content metadata
51          */
52         public Metadata(FormatMetadata formatMetadata, ContentMetadata contentMetadata) {
53                 this.formatMetadata = formatMetadata;
54                 this.contentMetadata = contentMetadata;
55         }
56
57         //
58         // ACCESSORS
59         //
60
61         /**
62          * Returns the number of channels of this metadata.
63          *
64          * @return The number of channels of this metadata
65          */
66         public int channels() {
67                 return formatMetadata.channels();
68         }
69
70         /**
71          * Returns a metadata with the same parameters as this metadata and the given
72          * number of channels.
73          *
74          * @param channels
75          *              The new number of channels
76          * @return A new metadata with the given number of channels
77          */
78         public Metadata channels(int channels) {
79                 return new Metadata(formatMetadata.channels(channels), contentMetadata);
80         }
81
82         /**
83          * Returns the sampling frequency of this metadata.
84          *
85          * @return The sampling frequency of this metadata
86          */
87         public int frequency() {
88                 return formatMetadata.frequency();
89         }
90
91         /**
92          * Returns a new metadata with the same parameters as this metadata and the
93          * given frequency.
94          *
95          * @param frequency
96          *              The new frequency
97          * @return A new metadata with the given frequency
98          */
99         public Metadata frequency(int frequency) {
100                 return new Metadata(formatMetadata.frequency(frequency), contentMetadata);
101         }
102
103         /**
104          * Returns the encoding of this metadata
105          *
106          * @return The encoding of this metadata
107          */
108         public String encoding() {
109                 return formatMetadata.encoding();
110         }
111
112         /**
113          * Returns a new metadata with the same parameters as this metadata and the
114          * given encoding.
115          *
116          * @param encoding
117          *              The new encoding
118          * @return A new metadata with the given encoding
119          */
120         public Metadata encoding(String encoding) {
121                 return new Metadata(formatMetadata.encoding(encoding), contentMetadata);
122         }
123
124         /**
125          * Returns the artist, if any.
126          *
127          * @return The artist, or {@link Optional#absent()}
128          */
129         public Optional<String> artist() {
130                 return contentMetadata.artist();
131         }
132
133         /**
134          * Returns new metadata with the same attributes as this metadata, except for
135          * the artist.
136          *
137          * @param artist
138          *              The new artist
139          * @return New metadata with a changed artist
140          */
141         public Metadata artist(String artist) {
142                 return new Metadata(formatMetadata, contentMetadata.artist(artist));
143         }
144
145         /**
146          * Returns the name of the content, if any.
147          *
148          * @return The name, or {@link Optional#absent()}
149          */
150         public Optional<String> name() {
151                 return contentMetadata.name();
152         }
153
154         /**
155          * Returns new metadata with the same attributes as this metadata, except for
156          * the name.
157          *
158          * @param name
159          *              The new name
160          * @return New metadata with a changed name
161          */
162         public Metadata name(String name) {
163                 return new Metadata(formatMetadata, contentMetadata.name(name));
164         }
165
166         /**
167          * Returns the title of the content.
168          *
169          * @return The title of the content
170          */
171         public String title() {
172                 return contentMetadata.title();
173         }
174
175         /**
176          * Returns new metadata with the same attributes as this metadata but with the
177          * title changed to the given title.
178          *
179          * @param title
180          *              The new title
181          * @return The new metadata
182          */
183         public Metadata title(String title) {
184                 return new Metadata(formatMetadata, contentMetadata.title(title));
185         }
186
187         //
188         // OBJECT METHODS
189         //
190
191         @Override
192         public int hashCode() {
193                 return formatMetadata.hashCode() ^ contentMetadata.hashCode();
194         }
195
196         @Override
197         public boolean equals(Object object) {
198                 if (!(object instanceof Metadata)) {
199                         return false;
200                 }
201                 Metadata metadata = (Metadata) object;
202                 return formatMetadata.equals(metadata.formatMetadata) && contentMetadata.equals(metadata.contentMetadata);
203         }
204
205         @Override
206         public String toString() {
207                 return String.format("%s: %s", formatMetadata, contentMetadata);
208         }
209
210 }