From 6327d5b5e4c24ad6ce6c74957dcaef91e9a06755 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 28 Jul 2012 23:38:20 +0200 Subject: [PATCH] Add duration filter to all templates. --- .../demoscenemusic/core/TemplateServlet.java | 2 + .../demoscenemusic/template/DurationFilter.java | 47 ++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/main/java/net/pterodactylus/demoscenemusic/template/DurationFilter.java diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java b/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java index a159e8a..b6107a6 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java @@ -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 index 0000000..5f45c4b --- /dev/null +++ b/src/main/java/net/pterodactylus/demoscenemusic/template/DurationFilter.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +public class DurationFilter implements Filter { + + /** + * {@inheritDoc} + */ + @Override + public Object format(TemplateContext templateContext, Object data, Map 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); + } + +} -- 2.7.4