Remove notification if no posts and replies remain.
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / ListNotification.java
1 /*
2  * Sone - ListNotification.java - Copyright © 2010 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.notify;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.concurrent.CopyOnWriteArrayList;
24
25 import net.pterodactylus.util.notify.TemplateNotification;
26 import net.pterodactylus.util.template.Template;
27
28 /**
29  * Notification that maintains a list of new elements.
30  *
31  * @param <T>
32  *            The type of the items
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class ListNotification<T> extends TemplateNotification {
36
37         /** The key under which to store the elements in the template. */
38         private final String key;
39
40         /** The list of new elements. */
41         private final List<T> elements = new CopyOnWriteArrayList<T>();
42
43         /**
44          * Creates a new list notification.
45          *
46          * @param id
47          *            The ID of the notification
48          * @param key
49          *            The key under which to store the elements in the template
50          * @param template
51          *            The template to render
52          */
53         public ListNotification(String id, String key, Template template) {
54                 super(id, template);
55                 this.key = key;
56                 template.getInitialContext().set(key, elements);
57         }
58
59         /**
60          * Creates a new list notification that copies its ID and the template from
61          * the given list notification.
62          *
63          * @param listNotification
64          *            The list notification to copy
65          */
66         public ListNotification(ListNotification<T> listNotification) {
67                 super(listNotification.getId(), new Template());
68                 this.key = listNotification.key;
69                 getTemplate().add(listNotification.getTemplate());
70                 getTemplate().getInitialContext().set(key, elements);
71         }
72
73         //
74         // ACTIONS
75         //
76
77         /**
78          * Returns the current list of elements.
79          *
80          * @return The current list of elements
81          */
82         public List<T> getElements() {
83                 return new ArrayList<T>(elements);
84         }
85
86         /**
87          * Sets the elements to show in this notification.
88          *
89          * @param elements
90          *            The elements to show
91          */
92         public void setElements(Collection<? extends T> elements) {
93                 this.elements.clear();
94                 this.elements.addAll(elements);
95                 touch();
96         }
97
98         /**
99          * Returns whether there are any new elements.
100          *
101          * @return {@code true} if there are no new elements, {@code false} if there
102          *         are new elements
103          */
104         public boolean isEmpty() {
105                 return elements.isEmpty();
106         }
107
108         /**
109          * Adds a discovered element.
110          *
111          * @param element
112          *            The new element
113          */
114         public void add(T element) {
115                 elements.add(element);
116                 touch();
117         }
118
119         /**
120          * Removes the given element from the list of new elements.
121          *
122          * @param element
123          *            The element to remove
124          */
125         public void remove(T element) {
126                 elements.remove(element);
127                 if (elements.isEmpty()) {
128                         dismiss();
129                 }
130                 touch();
131         }
132
133         //
134         // ABSTRACTNOTIFICATION METHODS
135         //
136
137         /**
138          * {@inheritDoc}
139          */
140         @Override
141         public void dismiss() {
142                 super.dismiss();
143                 elements.clear();
144         }
145
146 }