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 /** The format metadata. */
34 private final FormatMetadata formatMetadata;
36 /** The content metadata. */
37 private final ContentMetadata contentMetadata;
39 /** Creates empty metadata. */
41 this(new FormatMetadata(), new ContentMetadata());
45 * Creates metadata from the given format and content metadata.
47 * @param formatMetadata
49 * @param contentMetadata
50 * The content metadata
52 public Metadata(FormatMetadata formatMetadata, ContentMetadata contentMetadata) {
53 this.formatMetadata = formatMetadata;
54 this.contentMetadata = contentMetadata;
62 * Returns the embedded format metadata.
64 * @return The format metadata
66 public FormatMetadata format() {
67 return formatMetadata;
71 * Returns the embedded content metadata.
73 * @return The content metadata
75 public ContentMetadata content() {
76 return contentMetadata;
80 * Returns the number of channels of this metadata.
82 * @return The number of channels of this metadata
84 public int channels() {
85 return formatMetadata.channels();
89 * Returns a metadata with the same parameters as this metadata and the given
93 * The new number of channels
94 * @return A new metadata with the given number of channels
96 public Metadata channels(int channels) {
97 return new Metadata(formatMetadata.channels(channels), contentMetadata);
101 * Returns the sampling frequency of this metadata.
103 * @return The sampling frequency of this metadata
105 public int frequency() {
106 return formatMetadata.frequency();
110 * Returns a new metadata with the same parameters as this metadata and the
115 * @return A new metadata with the given frequency
117 public Metadata frequency(int frequency) {
118 return new Metadata(formatMetadata.frequency(frequency), contentMetadata);
122 * Returns the encoding of this metadata
124 * @return The encoding of this metadata
126 public String encoding() {
127 return formatMetadata.encoding();
131 * Returns a new metadata with the same parameters as this metadata and the
136 * @return A new metadata with the given encoding
138 public Metadata encoding(String encoding) {
139 return new Metadata(formatMetadata.encoding(encoding), contentMetadata);
143 * Returns the artist, if any.
145 * @return The artist, or {@link Optional#absent()}
147 public Optional<String> artist() {
148 return contentMetadata.artist();
152 * Returns new metadata with the same attributes as this metadata, except for
157 * @return New metadata with a changed artist
159 public Metadata artist(String artist) {
160 return new Metadata(formatMetadata, contentMetadata.artist(artist));
164 * Returns the name of the content, if any.
166 * @return The name, or {@link Optional#absent()}
168 public Optional<String> name() {
169 return contentMetadata.name();
173 * Returns new metadata with the same attributes as this metadata, except for
178 * @return New metadata with a changed name
180 public Metadata name(String name) {
181 return new Metadata(formatMetadata, contentMetadata.name(name));
185 * Returns the title of the content.
187 * @return The title of the content
189 public String title() {
190 return contentMetadata.title();
194 * Returns new metadata with the same attributes as this metadata but with the
195 * title changed to the given title.
199 * @return The new metadata
201 public Metadata title(String title) {
202 return new Metadata(formatMetadata, contentMetadata.title(title));
210 public int hashCode() {
211 return formatMetadata.hashCode() ^ contentMetadata.hashCode();
215 public boolean equals(Object object) {
216 if (!(object instanceof Metadata)) {
219 Metadata metadata = (Metadata) object;
220 return formatMetadata.equals(metadata.formatMetadata) && contentMetadata.equals(metadata.contentMetadata);
224 public String toString() {
225 return String.format("%s%s%s", formatMetadata, contentMetadata.toString().length() > 0 ? ": " : "", contentMetadata);