X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Ffilter%2FTimeCounterFilter.java;h=96b40524ddcbe9adddb0d7c114de201fc467316d;hb=4371ab7e900c5cc47aa4c18e6b125581a3accb2e;hp=3bb4415d3593ed446f48beb38ad695656e49a581;hpb=beffc0e31783f17abdd68caede40856ff9fc1778;p=sonitus.git diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/TimeCounterFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/TimeCounterFilter.java index 3bb4415..96b4052 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/TimeCounterFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/TimeCounterFilter.java @@ -19,6 +19,7 @@ package net.pterodactylus.sonitus.data.filter; import java.io.IOException; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; import net.pterodactylus.sonitus.data.Filter; import net.pterodactylus.sonitus.data.Metadata; @@ -35,25 +36,37 @@ public class TimeCounterFilter extends DummyFilter { /** The byte counter. */ private final AtomicLong counter = new AtomicLong(); + /** The parent’s metdata. */ + private final AtomicReference parentMetadata = new AtomicReference(); + /** Whether to reset the counter on a metadata update. */ private final boolean resetOnMetadataUpdate; + /** The last displayed timestamp. */ + private final AtomicLong lastTimestamp = new AtomicLong(-1); + /** * Creates a new time counter filter that automatically resets the counter when * the metadata is {@link #metadataUpdated(Metadata) updated}. + * + * @param name + * The name of the filter */ - public TimeCounterFilter() { - this(true); + public TimeCounterFilter(String name) { + this(name, true); } /** * Creates a new time counter filter. * + * @param name + * The name of the filter * @param resetOnMetadataUpdate * {@code true} if the counter should automatically be reset if the metadata * is updated, {@code false} otherwise */ - public TimeCounterFilter(boolean resetOnMetadataUpdate) { + public TimeCounterFilter(String name, boolean resetOnMetadataUpdate) { + super(name); this.resetOnMetadataUpdate = resetOnMetadataUpdate; } @@ -87,16 +100,31 @@ public class TimeCounterFilter extends DummyFilter { @Override public void metadataUpdated(Metadata metadata) { - super.metadataUpdated(metadata); + parentMetadata.set(metadata); if (resetOnMetadataUpdate) { reset(); } + updateTimestamp(); } @Override public void process(byte[] buffer) throws IOException { super.process(buffer); counter.getAndAdd(buffer.length); + updateTimestamp(); + } + + // + // PRIVATE METHODS + // + + /** Updates the timestamp in the metadata. */ + private void updateTimestamp() { + long timestamp = getMillis() / 1000; + if (lastTimestamp.get() != timestamp) { + super.metadataUpdated(parentMetadata.get().comment(String.format("%02d:%02d", timestamp / 60, timestamp % 60))); + lastTimestamp.set(timestamp); + } } }