Move event and metadata handling into abstract base class.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / source / FileSource.java
index dfa1665..9aff8d4 100644 (file)
@@ -24,7 +24,11 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
 
+import net.pterodactylus.sonitus.data.AbstractControlledComponent;
+import net.pterodactylus.sonitus.data.Controller;
 import net.pterodactylus.sonitus.data.Metadata;
 import net.pterodactylus.sonitus.data.Source;
 import net.pterodactylus.sonitus.io.IdentifyingInputStream;
@@ -37,14 +41,11 @@ import com.google.common.base.Optional;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class FileSource implements Source {
+public class FileSource extends AbstractControlledComponent implements Source {
 
        /** The path of the file. */
        private final String path;
 
-       /** The identified metadata of the file. */
-       private final Metadata metadata;
-
        /** The input stream. */
        private InputStream fileInputStream;
 
@@ -57,20 +58,30 @@ public class FileSource implements Source {
         *              if the file can not be found, or an I/O error occurs
         */
        public FileSource(String path) throws IOException {
+               super(path);
                this.path = checkNotNull(path, "path must not be null");
                fileInputStream = new FileInputStream(path);
 
                /* identify file type. */
                Optional<IdentifyingInputStream> identifyingInputStream = IdentifyingInputStream.create(new FileInputStream(path));
                if (identifyingInputStream.isPresent()) {
-                       metadata = identifyingInputStream.get().metadata();
+                       metadataUpdated(identifyingInputStream.get().metadata());
                } else {
                        /* fallback. */
-                       metadata = new Metadata().name(path);
+                       metadataUpdated(new Metadata().name(path));
                }
        }
 
        //
+       // CONTROLLED METHODS
+       //
+
+       @Override
+       public List<Controller<?>> controllers() {
+               return Collections.emptyList();
+       }
+
+       //
        // SOURCE METHODS
        //
 
@@ -84,18 +95,13 @@ public class FileSource implements Source {
                return Arrays.copyOf(buffer, read);
        }
 
-       @Override
-       public Metadata metadata() {
-               return metadata;
-       }
-
        //
        // OBJECT METHODS
        //
 
        @Override
        public String toString() {
-               return String.format("%s (%s)", path, metadata);
+               return String.format("%s (%s)", path, metadata());
        }
 
 }