From: David ‘Bombe’ Roden Date: Wed, 17 Nov 2010 20:44:09 +0000 (+0100) Subject: Add generlized list notification. X-Git-Tag: 0.3-RC1~13 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2889127f645d17c4ae0a650e935d1481d9cbd727;p=Sone.git Add generlized list notification. --- 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(); + } + +}