Compare formats’ encodings disregarding case.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / Format.java
index 0657e33..fab7967 100644 (file)
@@ -25,6 +25,15 @@ package net.pterodactylus.sonitus.data;
  */
 public class Format {
 
+       /** Constant for an unknown number of channels. */
+       public static final int UNKNOWN_CHANNELS = -1;
+
+       /** Constant for an unknown frequency. */
+       public static final int UNKNOWN_FREQUENCY = -1;
+
+       /** Constant for an unknown format. */
+       public static final String UNKNOWN_ENCODING = "UNKNOWN";
+
        /** The number of channels of this format. */
        private final int channels;
 
@@ -50,6 +59,10 @@ public class Format {
                this.encoding = encoding;
        }
 
+       //
+       // ACCESSORS
+       //
+
        /**
         * Returns the number of channels of this format.
         *
@@ -77,4 +90,67 @@ public class Format {
                return encoding;
        }
 
+       //
+       // MUTATORS
+       //
+
+       /**
+        * Returns a format with the same parameters as this format and the given
+        * number of channels.
+        *
+        * @param channels
+        *              The new number of channels
+        * @return A new format with the given number of channels
+        */
+       public Format channels(int channels) {
+               return new Format(channels, frequency, encoding);
+       }
+
+       /**
+        * Returns a new format with the same parameters as this format and the given
+        * frequency.
+        *
+        * @param frequency
+        *              The new frequency
+        * @return A new format with the given frequency
+        */
+       public Format frequency(int frequency) {
+               return new Format(channels, frequency, encoding);
+       }
+
+       /**
+        * Returns a new format with the same parameters as this format and the given
+        * encoding.
+        *
+        * @param encoding
+        *              The new encoding
+        * @return A new format with the given encoding
+        */
+       public Format encoding(String encoding) {
+               return new Format(channels, frequency, encoding);
+       }
+
+       //
+       // OBJECT METHODS
+       //
+
+       @Override
+       public int hashCode() {
+               return (channels << 16) ^ frequency ^ encoding.toUpperCase().hashCode();
+       }
+
+       @Override
+       public boolean equals(Object object) {
+               if ((object == null) || (getClass() != object.getClass())) {
+                       return false;
+               }
+               Format format = (Format) object;
+               return (format.channels == channels) && (format.frequency == frequency) && format.encoding.equalsIgnoreCase(encoding());
+       }
+
+       @Override
+       public String toString() {
+               return String.format("%d Channel%s, %d Hz, %s", channels, channels != 1 ? "s" : "", frequency, encoding);
+       }
+
 }