From bd2e8599a3a462582745723743ddb13f00b7dc99 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 15 Jul 2011 11:15:57 +0200 Subject: [PATCH] Add filter that uniquifies collections. --- .../sone/template/UniqueElementFilter.java | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/template/UniqueElementFilter.java diff --git a/src/main/java/net/pterodactylus/sone/template/UniqueElementFilter.java b/src/main/java/net/pterodactylus/sone/template/UniqueElementFilter.java new file mode 100644 index 0000000..70dae70 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/template/UniqueElementFilter.java @@ -0,0 +1,46 @@ +/* + * Sone - UniqueElementFilter.java - Copyright © 2011 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.sone.template; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import net.pterodactylus.util.template.Filter; +import net.pterodactylus.util.template.TemplateContext; + +/** + * Filter that reduces a collection to a {@link Set}, removing duplicates. + * + * @author David ‘Bombe’ Roden + */ +public class UniqueElementFilter implements Filter { + + /** + * {@inheritDoc} + */ + @Override + public Object format(TemplateContext templateContext, Object data, Map parameters) { + if (!(data instanceof Collection)) { + return data; + } + return new HashSet((Collection) data); + } + +} -- 2.7.4