2 * Sonitus - Metainfo.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.data;
20 import com.google.common.base.Optional;
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.
27 * Metadata, once created, is immutable.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class Metadata {
33 /** Marker for unknown metadata. */
34 public static final Metadata UNKNOWN = new Metadata();
36 /** The format metadata. */
37 private final FormatMetadata formatMetadata;
39 /** The content metadata. */
40 private final ContentMetadata contentMetadata;
42 /** Creates empty metadata. */
44 this(new FormatMetadata(), new ContentMetadata());
48 * Creates metadata from the given format and content metadata.
50 * @param formatMetadata
52 * @param contentMetadata
53 * The content metadata
55 public Metadata(FormatMetadata formatMetadata, ContentMetadata contentMetadata) {
56 this.formatMetadata = formatMetadata;
57 this.contentMetadata = contentMetadata;
65 * Returns the embedded format metadata.
67 * @return The format metadata
69 public FormatMetadata format() {
70 return formatMetadata;
74 * Returns the embedded content metadata.
76 * @return The content metadata
78 public ContentMetadata content() {
79 return contentMetadata;
83 * Returns the number of channels of this metadata.
85 * @return The number of channels of this metadata
87 public int channels() {
88 return formatMetadata.channels();
92 * Returns a metadata with the same parameters as this metadata and the given
96 * The new number of channels
97 * @return A new metadata with the given number of channels
99 public Metadata channels(int channels) {
100 return new Metadata(formatMetadata.channels(channels), contentMetadata);
104 * Returns the sampling frequency of this metadata.
106 * @return The sampling frequency of this metadata
108 public int frequency() {
109 return formatMetadata.frequency();
113 * Returns a new metadata with the same parameters as this metadata and the
118 * @return A new metadata with the given frequency
120 public Metadata frequency(int frequency) {
121 return new Metadata(formatMetadata.frequency(frequency), contentMetadata);
125 * Returns the encoding of this metadata
127 * @return The encoding of this metadata
129 public String encoding() {
130 return formatMetadata.encoding();
134 * Returns a new metadata with the same parameters as this metadata and the
139 * @return A new metadata with the given encoding
141 public Metadata encoding(String encoding) {
142 return new Metadata(formatMetadata.encoding(encoding), contentMetadata);
146 * Returns the artist, if any.
148 * @return The artist, or {@link Optional#absent()}
150 public Optional<String> artist() {
151 return contentMetadata.artist();
155 * Returns new metadata with the same attributes as this metadata, except for
160 * @return New metadata with a changed artist
162 public Metadata artist(String artist) {
163 return new Metadata(formatMetadata, contentMetadata.artist(artist));
167 * Returns the name of the content, if any.
169 * @return The name, or {@link Optional#absent()}
171 public Optional<String> name() {
172 return contentMetadata.name();
176 * Returns new metadata with the same attributes as this metadata, except for
181 * @return New metadata with a changed name
183 public Metadata name(String name) {
184 return new Metadata(formatMetadata, contentMetadata.name(name));
188 * Returns the title of the content.
190 * @return The title of the content
192 public String title() {
193 return contentMetadata.title();
197 * Returns new metadata with the same attributes as this metadata but with the
198 * title changed to the given title.
202 * @return The new metadata
204 public Metadata title(String title) {
205 return new Metadata(formatMetadata, contentMetadata.title(title));
209 * Returns the comment of the content, if any.
211 * @return The comment of the content
213 public Optional<String> comment() {
214 return contentMetadata.comment();
218 * Returns new metadata with the same attributes as this metadata but with the
219 * comment changed to the given comment.
223 * @return The new metadata
225 public Metadata comment(String comment) {
226 return new Metadata(formatMetadata, contentMetadata.comment(comment));
230 * Returns the title with the comment appended in parantheses, if a comment has
233 * @return The title with the comment appended
235 public String fullTitle() {
236 return String.format("%s%s", title(), comment().isPresent() ? String.format(" (%s)", comment().get()) : "");
240 * Returns whether this metadata object equals the given object if the comments
241 * of this and the given object are ignored.
244 * The object to compare to this one
245 * @return {@code true} if the given object and this object are equal if the
246 * comments are ignored, {@code false} otherwise
248 public boolean equalsIgnoreComment(Object object) {
249 if (!(object instanceof Metadata)) {
252 Metadata metadata = (Metadata) object;
253 return formatMetadata.equals(metadata.formatMetadata) && contentMetadata.equalsIgnoreComment(metadata.contentMetadata);
261 public int hashCode() {
262 return formatMetadata.hashCode() ^ contentMetadata.hashCode();
266 public boolean equals(Object object) {
267 if (!(object instanceof Metadata)) {
270 Metadata metadata = (Metadata) object;
271 return formatMetadata.equals(metadata.formatMetadata) && contentMetadata.equals(metadata.contentMetadata);
275 public String toString() {
276 return String.format("%s%s%s", formatMetadata, contentMetadata.toString().length() > 0 ? ": " : "", contentMetadata);