Formatting.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / TimeCounterFilter.java
index b5a081d..9b65670 100644 (file)
@@ -21,17 +21,19 @@ import java.io.IOException;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
 
+import net.pterodactylus.sonitus.data.AbstractFilter;
+import net.pterodactylus.sonitus.data.DataPacket;
 import net.pterodactylus.sonitus.data.Filter;
 import net.pterodactylus.sonitus.data.Metadata;
 
 /**
  * {@link Filter} implementation that uses the number of bytes that have been
- * {@link #process(byte[]) processed} together with the {@link Metadata} to
+ * {@link #process(DataPacket) processed} together with the {@link Metadata} to
  * calculate how long a source is already playing.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class TimeCounterFilter extends DummyFilter {
+public class TimeCounterFilter extends AbstractFilter implements Filter {
 
        /** The byte counter. */
        private final AtomicLong counter = new AtomicLong();
@@ -76,7 +78,7 @@ public class TimeCounterFilter extends DummyFilter {
 
        /**
         * Returns the number of milliseconds worth of data that has been passed into
-        * {@link #process(byte[])}. If no metadata has yet been set, {@code 0} is
+        * {@link #process(DataPacket)}. If no metadata has yet been set, {@code 0} is
         * returned.
         *
         * @return The number of milliseconds the current source is already playing
@@ -95,7 +97,7 @@ public class TimeCounterFilter extends DummyFilter {
        }
 
        //
-       // DUMMYFILTER METHODS
+       // FILTER METHODS
        //
 
        @Override
@@ -104,14 +106,14 @@ public class TimeCounterFilter extends DummyFilter {
                if (resetOnMetadataUpdate) {
                        reset();
                }
-               updateTimestamp();
+               updateTimestamp(true);
        }
 
        @Override
-       public void process(byte[] buffer) throws IOException {
-               super.process(buffer);
-               counter.getAndAdd(buffer.length);
-               updateTimestamp();
+       public void process(DataPacket dataPacket) throws IOException {
+               super.process(dataPacket);
+               counter.getAndAdd(dataPacket.buffer().length);
+               updateTimestamp(false);
        }
 
        //
@@ -119,10 +121,10 @@ public class TimeCounterFilter extends DummyFilter {
        //
 
        /** Updates the timestamp in the metadata. */
-       private void updateTimestamp() {
+       private void updateTimestamp(boolean now) {
                long timestamp = getMillis() / 1000;
-               if (lastTimestamp.get() != timestamp) {
-                       super.metadataUpdated(parentMetadata.get().title(String.format("%s (%02d:%02d)", parentMetadata.get().title(), timestamp / 60, timestamp % 60)));
+               if (now || (lastTimestamp.get() != timestamp)) {
+                       super.metadataUpdated(parentMetadata.get().comment(String.format("%s%02d:%02d", (timestamp >= 3600) ? String.format("%d:", timestamp / 3600) : "", (timestamp % 3600) / 60, timestamp % 60)));
                        lastTimestamp.set(timestamp);
                }
        }