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 number of channels of this metadata.
64 * @return The number of channels of this metadata
66 public int channels() {
67 return formatMetadata.channels();
71 * Returns a metadata with the same parameters as this metadata and the given
75 * The new number of channels
76 * @return A new metadata with the given number of channels
78 public Metadata channels(int channels) {
79 return new Metadata(formatMetadata.channels(channels), contentMetadata);
83 * Returns the sampling frequency of this metadata.
85 * @return The sampling frequency of this metadata
87 public int frequency() {
88 return formatMetadata.frequency();
92 * Returns a new metadata with the same parameters as this metadata and the
97 * @return A new metadata with the given frequency
99 public Metadata frequency(int frequency) {
100 return new Metadata(formatMetadata.frequency(frequency), contentMetadata);
104 * Returns the encoding of this metadata
106 * @return The encoding of this metadata
108 public String encoding() {
109 return formatMetadata.encoding();
113 * Returns a new metadata with the same parameters as this metadata and the
118 * @return A new metadata with the given encoding
120 public Metadata encoding(String encoding) {
121 return new Metadata(formatMetadata.encoding(encoding), contentMetadata);
125 * Returns the artist, if any.
127 * @return The artist, or {@link Optional#absent()}
129 public Optional<String> artist() {
130 return contentMetadata.artist();
134 * Returns new metadata with the same attributes as this metadata, except for
139 * @return New metadata with a changed artist
141 public Metadata artist(String artist) {
142 return new Metadata(formatMetadata, contentMetadata.artist(artist));
146 * Returns the name of the content, if any.
148 * @return The name, or {@link Optional#absent()}
150 public Optional<String> name() {
151 return contentMetadata.name();
155 * Returns new metadata with the same attributes as this metadata, except for
160 * @return New metadata with a changed name
162 public Metadata name(String name) {
163 return new Metadata(formatMetadata, contentMetadata.name(name));
167 * Returns the title of the content.
169 * @return The title of the content
171 public String title() {
172 return contentMetadata.title();
176 * Returns new metadata with the same attributes as this metadata but with the
177 * title changed to the given title.
181 * @return The new metadata
183 public Metadata title(String title) {
184 return new Metadata(formatMetadata, contentMetadata.title(title));
192 public int hashCode() {
193 return formatMetadata.hashCode() ^ contentMetadata.hashCode();
197 public boolean equals(Object object) {
198 if (!(object instanceof Metadata)) {
201 Metadata metadata = (Metadata) object;
202 return formatMetadata.equals(metadata.formatMetadata) && contentMetadata.equals(metadata.contentMetadata);
206 public String toString() {
207 return String.format("%s: %s", formatMetadata, contentMetadata);