Add duration filter to all templates.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 28 Jul 2012 21:38:20 +0000 (23:38 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 28 Jul 2012 21:38:20 +0000 (23:38 +0200)
src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java
src/main/java/net/pterodactylus/demoscenemusic/template/DurationFilter.java [new file with mode: 0644]

index a159e8a..b6107a6 100644 (file)
@@ -44,6 +44,7 @@ import net.pterodactylus.demoscenemusic.data.Track;
 import net.pterodactylus.demoscenemusic.data.TrackDerivative;
 import net.pterodactylus.demoscenemusic.data.User;
 import net.pterodactylus.demoscenemusic.page.ServletRequest;
+import net.pterodactylus.demoscenemusic.template.DurationFilter;
 import net.pterodactylus.demoscenemusic.template.PropertiesAccessor;
 import net.pterodactylus.demoscenemusic.template.TrackDerivativeAccessor;
 import net.pterodactylus.demoscenemusic.template.UserAccessor;
@@ -119,6 +120,7 @@ public class TemplateServlet extends HttpServlet {
                });
                templateContextFactory.addFilter("sort", sortFilter);
                templateContextFactory.addFilter("matches", new MatchFilter());
+               templateContextFactory.addFilter("time", new DurationFilter());
 
                templateContextFactory.addTemplateObject("core", core);
                templateContextFactory.addTemplateObject("dataManager", core.getDataManager());
diff --git a/src/main/java/net/pterodactylus/demoscenemusic/template/DurationFilter.java b/src/main/java/net/pterodactylus/demoscenemusic/template/DurationFilter.java
new file mode 100644 (file)
index 0000000..5f45c4b
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * DemosceneMusic - DurationFilter.java - Copyright © 2012 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.demoscenemusic.template;
+
+import java.util.Map;
+
+import net.pterodactylus.util.number.Numbers;
+import net.pterodactylus.util.template.Filter;
+import net.pterodactylus.util.template.TemplateContext;
+
+/**
+ * {@link Filter} that formats a single number (representing seconds) as a
+ * duration , displayed as 12:34 (if less than an hour) or 1:23:45 (if more than
+ * an hour).
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DurationFilter implements Filter {
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
+               long duration = Numbers.safeParseLong(data, 0L);
+               if (duration >= 3600) {
+                       return String.format("%d:%02d:%02d", duration / 3600, duration % 3600 / 60, duration % 60);
+               }
+               return String.format("%02d:%02d", duration % 3600 / 60, duration % 60);
+       }
+
+}