X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fnotify%2FListNotification.java;h=5f7c1eb7d0778352b251f1ebb86748f9ac71df72;hb=fbd56494f4d26e59ae3872ac2276f631a81da085;hp=c7e5b7c31b0867819e4a16f7fac50fe3c990f730;hpb=9e4db46b86d084eba9029906e779ec1d96f78ac4;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/notify/ListNotification.java b/src/main/java/net/pterodactylus/sone/notify/ListNotification.java index c7e5b7c..5f7c1eb 100644 --- a/src/main/java/net/pterodactylus/sone/notify/ListNotification.java +++ b/src/main/java/net/pterodactylus/sone/notify/ListNotification.java @@ -18,8 +18,9 @@ package net.pterodactylus.sone.notify; import java.util.ArrayList; -import java.util.Collections; +import java.util.Collection; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import net.pterodactylus.util.notify.TemplateNotification; import net.pterodactylus.util.template.Template; @@ -33,8 +34,11 @@ import net.pterodactylus.util.template.Template; */ public class ListNotification extends TemplateNotification { + /** The key under which to store the elements in the template. */ + private final String key; + /** The list of new elements. */ - private final List elements = Collections.synchronizedList(new ArrayList()); + private final List elements = new CopyOnWriteArrayList(); /** * Creates a new list notification. @@ -48,9 +52,24 @@ public class ListNotification extends TemplateNotification { */ public ListNotification(String id, String key, Template template) { super(id, template); + this.key = key; template.getInitialContext().set(key, elements); } + /** + * Creates a new list notification that copies its ID and the template from + * the given list notification. + * + * @param listNotification + * The list notification to copy + */ + public ListNotification(ListNotification listNotification) { + super(listNotification.getId(), new Template()); + this.key = listNotification.key; + getTemplate().add(listNotification.getTemplate()); + getTemplate().getInitialContext().set(key, elements); + } + // // ACTIONS // @@ -65,6 +84,18 @@ public class ListNotification extends TemplateNotification { } /** + * Sets the elements to show in this notification. + * + * @param elements + * The elements to show + */ + public void setElements(Collection elements) { + this.elements.clear(); + this.elements.addAll(elements); + touch(); + } + + /** * Returns whether there are any new elements. * * @return {@code true} if there are no new elements, {@code false} if there