2 * Sonitus - ContentMetadata.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 java.util.Arrays;
22 import com.google.common.base.Joiner;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Preconditions;
27 * The part of the {@link Metadata} that contains information about the content
28 * of a {@link Source}, such as the name of the track, the artist, or other
31 * Content metadata also contains a “title” which is an amalgamation of all
32 * information in the content metadata. If not given, it will be automatically
33 * constructed from all other information. If can also be specified manually to
34 * override the default.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class ContentMetadata {
41 private final Optional<String> artist;
44 private final Optional<String> name;
46 /** The all-in-one title. */
47 private final String title;
49 /** Creates empty content metadata. */
50 public ContentMetadata() {
55 * Creates content metadata containing the given title.
58 * The title of the metadata
59 * @throws NullPointerException
60 * if {@code title} is {@code null}
62 public ContentMetadata(String title) throws NullPointerException {
63 this(null, null, title);
67 * Creates content metadata.
70 * The artist of the track
72 * The name of the track
74 public ContentMetadata(String artist, String name) {
75 this(artist, name, joinStrings(artist, name));
79 * Creates content metadata.
82 * The artist of the track (may be null)
84 * The name of the track (may be null)
86 * The title of the track
87 * @throws NullPointerException
88 * if {@code title} is {@code null}
90 private ContentMetadata(String artist, String name, String title) throws NullPointerException {
91 this.artist = Optional.fromNullable(artist);
92 this.name = Optional.fromNullable(name);
93 this.title = Preconditions.checkNotNull(title, "title must not be null");
101 * Returns the artist of the track, if it has been set.
103 * @return The artist of the track
105 public Optional<String> artist() {
110 * Returns the name of the track, if it has been set.
112 * @return The name of the track
114 public Optional<String> name() {
119 * Returns the title of the track.
121 * @return The title of the track
123 public String title() {
132 * Creates new content metadata that is a copy of this content metadata but
133 * with the artist changed. The title will be reconstructed from the new artist
134 * and the existing name.
138 * @return The new content metadata
140 public ContentMetadata artist(String artist) {
141 return new ContentMetadata(artist, name().orNull(), joinStrings(artist, name().orNull()));
145 * Creates new content metadata that is a copy of this content metadata but
146 * with the name changed. The title will be reconstructed from the existing
147 * artist and the new name.
151 * @return The new content metadata
153 public ContentMetadata name(String name) {
154 return new ContentMetadata(artist().orNull(), name, joinStrings(artist().orNull(), name));
158 * Creates new content metadata that is a copy of this content metadata but
159 * with the title changed.
163 * @return The new content metadata
165 public ContentMetadata title(String title) {
166 return new ContentMetadata(artist().orNull(), name().orNull(), title);
174 public int hashCode() {
175 return artist().hashCode() ^ name().hashCode() ^ title().hashCode();
179 public boolean equals(Object object) {
180 if (!(object instanceof ContentMetadata)) {
183 ContentMetadata contentMetadata = (ContentMetadata) object;
184 return artist().equals(contentMetadata.artist()) && name().equals(contentMetadata.name()) && title().equals(contentMetadata.title());
188 public String toString() {
197 * Joins the given strings, concatenating them with “ - ” and ignoring {@code
201 * The strings to join
202 * @return The joined strings
204 private static String joinStrings(String... strings) {
205 return Joiner.on(" - ").skipNulls().join(Arrays.asList(strings));