From 2889127f645d17c4ae0a650e935d1481d9cbd727 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 17 Nov 2010 21:44:09 +0100 Subject: [PATCH] Add generlized list notification. --- .../sone/notify/ListNotification.java | 103 +++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/notify/ListNotification.java diff --git a/src/main/java/net/pterodactylus/sone/notify/ListNotification.java b/src/main/java/net/pterodactylus/sone/notify/ListNotification.java new file mode 100644 index 0000000..64c2ecb --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/notify/ListNotification.java @@ -0,0 +1,103 @@ +/* + * Sone - ListNotification.java - Copyright © 2010 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.notify; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import net.pterodactylus.util.notify.TemplateNotification; +import net.pterodactylus.util.template.Template; + +/** + * Notification that maintains a list of new elements. + * + * @param + * The type of the items + * @author David ‘Bombe’ Roden + */ +public class ListNotification extends TemplateNotification { + + /** The list of new elements. */ + private final List elements = Collections.synchronizedList(new ArrayList()); + + /** + * Creates a new list notification. + * + * @param id + * The ID of the notification + * @param key + * The key under which to store the elements in the template + * @param template + * The template to render + */ + public ListNotification(String id, String key, Template template) { + super(id, template); + template.set(key, elements); + } + + // + // ACTIONS + // + + /** + * Returns whether there are any new elements. + * + * @return {@code true} if there are no new elements, {@code false} if there + * are new elements + */ + public boolean isEmpty() { + return elements.isEmpty(); + } + + /** + * Adds a discovered element. + * + * @param element + * The new element + */ + public void add(T element) { + elements.add(element); + touch(); + } + + /** + * Removes the given element from the list of new elements. + * + * @param element + * The element to remove + */ + public void remove(T element) { + elements.remove(element); + touch(); + } + + // + // ABSTRACTNOTIFICATION METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public void dismiss() { + super.dismiss(); + elements.clear(); + } + +} -- 2.7.4