Add equals() method that ignores the comment of the content metadata.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / Metadata.java
index 52cb365..da0aadc 100644 (file)
@@ -30,6 +30,9 @@ import com.google.common.base.Optional;
  */
 public class Metadata {
 
+       /** Marker for unknown metadata. */
+       public static final Metadata UNKNOWN = new Metadata();
+
        /** The format metadata. */
        private final FormatMetadata formatMetadata;
 
@@ -59,6 +62,24 @@ public class Metadata {
        //
 
        /**
+        * Returns the embedded format metadata.
+        *
+        * @return The format metadata
+        */
+       public FormatMetadata format() {
+               return formatMetadata;
+       }
+
+       /**
+        * Returns the embedded content metadata.
+        *
+        * @return The content metadata
+        */
+       public ContentMetadata content() {
+               return contentMetadata;
+       }
+
+       /**
         * Returns the number of channels of this metadata.
         *
         * @return The number of channels of this metadata
@@ -184,6 +205,54 @@ public class Metadata {
                return new Metadata(formatMetadata, contentMetadata.title(title));
        }
 
+       /**
+        * Returns the comment of the content, if any.
+        *
+        * @return The comment of the content
+        */
+       public Optional<String> comment() {
+               return contentMetadata.comment();
+       }
+
+       /**
+        * Returns new metadata with the same attributes as this metadata but with the
+        * comment changed to the given comment.
+        *
+        * @param comment
+        *              The new comment
+        * @return The new metadata
+        */
+       public Metadata comment(String comment) {
+               return new Metadata(formatMetadata, contentMetadata.comment(comment));
+       }
+
+       /**
+        * Returns the title with the comment appended in parantheses, if a comment has
+        * been set.
+        *
+        * @return The title with the comment appended
+        */
+       public String fullTitle() {
+               return String.format("%s%s", title(), comment().isPresent() ? String.format(" (%s)", comment().get()) : "");
+       }
+
+       /**
+        * Returns whether this metadata object equals the given object if the comments
+        * of this and the given object are ignored.
+        *
+        * @param object
+        *              The object to compare to this one
+        * @return {@code true} if the given object and this object are equal if the
+        *         comments are ignored, {@code false} otherwise
+        */
+       public boolean equalsIgnoreComment(Object object) {
+               if (!(object instanceof Metadata)) {
+                       return false;
+               }
+               Metadata metadata = (Metadata) object;
+               return formatMetadata.equals(metadata.formatMetadata) && contentMetadata.equalsIgnoreComment(metadata.contentMetadata);
+       }
+
        //
        // OBJECT METHODS
        //
@@ -204,7 +273,7 @@ public class Metadata {
 
        @Override
        public String toString() {
-               return String.format("%s: %s", formatMetadata, contentMetadata);
+               return String.format("%s%s%s", formatMetadata, contentMetadata.toString().length() > 0 ? ": " : "", contentMetadata);
        }
 
 }