Retrieve binary and parameters from subclass.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 16 Mar 2013 00:12:38 +0000 (01:12 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 16 Mar 2013 00:12:38 +0000 (01:12 +0100)
src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java
src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Decoder.java
src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Decoder.java

index ff838d9..c3ab508 100644 (file)
@@ -48,31 +48,12 @@ public abstract class ExternalFilter implements Filter {
        /** The logger. */
        private static final Logger logger = Logger.getLogger(ExternalFilter.class.getName());
 
-       /** The binary to execute. */
-       private final String binary;
-
-       /** The parameters for the binary. */
-       private final Iterable<String> parameters;
-
        /** The format of the source. */
        private Format format;
 
        /** The input stream that will hold the converted source. */
        private PipedInputStream pipedInputStream;
 
-       /**
-        * Creates a new external filter.
-        *
-        * @param binary
-        *              The binary to execute
-        * @param parameters
-        *              The parameter for the binary
-        */
-       public ExternalFilter(String binary, Iterable<String> parameters) {
-               this.binary = binary;
-               this.parameters = parameters;
-       }
-
        //
        // FILTER METHODS
 
@@ -94,7 +75,7 @@ public abstract class ExternalFilter implements Filter {
 
                format = source.format();
                try {
-                       Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.<String>builder().add(binary).addAll(parameters).build(), String.class));
+                       final Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.<String>builder().add(binary(format)).addAll(parameters(format)).build(), String.class));
                        final InputStream processOutput = process.getInputStream();
                        final OutputStream processInput = process.getOutputStream();
                        final InputStream processError = process.getErrorStream();
@@ -142,6 +123,28 @@ public abstract class ExternalFilter implements Filter {
        }
 
        //
+       // SUBCLASS METHODS
+       //
+
+       /**
+        * Returns the location of the binary to execute.
+        *
+        * @param format
+        *              The format being processed
+        * @return The location of the binary to execute
+        */
+       protected abstract String binary(Format format);
+
+       /**
+        * Returns the parameters for the binary.
+        *
+        * @param format
+        *              The format being processed
+        * @return The parameters for the binary
+        */
+       protected abstract Iterable<String> parameters(Format format);
+
+       //
        // STATIC METHODS
        //
 
index ecdee68..88afa30 100644 (file)
@@ -29,19 +29,7 @@ import net.pterodactylus.sonitus.data.Source;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class ExternalMp3Decoder extends ExternalFilter {
-
-       /**
-        * Creates a new external MP3 decoder.
-        *
-        * @param binary
-        *              The binary to execute
-        * @param parameters
-        *              The parameters for the binary
-        */
-       public ExternalMp3Decoder(String binary, Iterable<String> parameters) {
-               super(binary, parameters);
-       }
+public abstract class ExternalMp3Decoder extends ExternalFilter {
 
        @Override
        public Format format() {
index d824041..aae01fe 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sonitus.data.filter;
 
+import net.pterodactylus.sonitus.data.Format;
+
 import com.google.common.collect.ImmutableList;
 
 /**
@@ -26,6 +28,12 @@ import com.google.common.collect.ImmutableList;
  */
 public class LameMp3Decoder extends ExternalMp3Decoder {
 
+       /** The location of the binary. */
+       private final String binary;
+
+       /** Whether to swap bytes in the decoded output. */
+       private final boolean swapBytes;
+
        /**
         * Creates a new LAME MP3 decoder.
         *
@@ -36,22 +44,21 @@ public class LameMp3Decoder extends ExternalMp3Decoder {
         *              endianness
         */
        public LameMp3Decoder(String binary, boolean swapBytes) {
-               super(binary, generateParameters(swapBytes));
+               this.binary = binary;
+               this.swapBytes = swapBytes;
        }
 
        //
-       // STATIC METHODS
+       // EXTERNALFILTER METHODS
        //
 
-       /**
-        * Generates the parameters for LAME.
-        *
-        * @param swapBytes
-        *              {@code true} to swap the decoded bytes, {@code false} to use platform
-        *              endianness
-        * @return The parameters for LAME
-        */
-       private static Iterable<String> generateParameters(boolean swapBytes) {
+       @Override
+       protected String binary(Format format) {
+               return binary;
+       }
+
+       @Override
+       protected Iterable<String> parameters(Format format) {
                ImmutableList.Builder parameters = ImmutableList.builder();
                parameters.add("--mp3input").add("--decode").add("-t");
                if (swapBytes) {