Update years in copyright line
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / ListNotification.java
1 /*
2  * Sone - ListNotification.java - Copyright © 2010–2015 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                 this(id, key, template, true);
55         }
56
57         /**
58          * Creates a new list notification.
59          *
60          * @param id
61          *            The ID of the notification
62          * @param key
63          *            The key under which to store the elements in the template
64          * @param template
65          *            The template to render
66          * @param dismissable
67          *            {@code true} if this notification should be dismissable by the
68          *            user, {@code false} otherwise
69          */
70         public ListNotification(String id, String key, Template template, boolean dismissable) {
71                 super(id, System.currentTimeMillis(), System.currentTimeMillis(), dismissable, template);
72                 this.key = key;
73                 template.getInitialContext().set(key, elements);
74         }
75
76         /**
77          * Creates a new list notification that copies its ID and the template from
78          * the given list notification.
79          *
80          * @param listNotification
81          *            The list notification to copy
82          */
83         public ListNotification(ListNotification<T> listNotification) {
84                 super(listNotification.getId(), listNotification.getCreatedTime(), listNotification.getLastUpdatedTime(), listNotification.isDismissable(), new Template());
85                 this.key = listNotification.key;
86                 getTemplate().add(listNotification.getTemplate());
87                 getTemplate().getInitialContext().set(key, elements);
88         }
89
90         //
91         // ACTIONS
92         //
93
94         /**
95          * Returns the current list of elements.
96          *
97          * @return The current list of elements
98          */
99         public List<T> getElements() {
100                 return new ArrayList<T>(elements);
101         }
102
103         /**
104          * Sets the elements to show in this notification. This method will not call
105          * {@link #touch()}.
106          *
107          * @param elements
108          *            The elements to show
109          */
110         public void setElements(Collection<? extends T> elements) {
111                 this.elements.clear();
112                 this.elements.addAll(elements);
113                 touch();
114         }
115
116         /**
117          * Returns whether there are any new elements.
118          *
119          * @return {@code true} if there are no new elements, {@code false} if there
120          *         are new elements
121          */
122         public boolean isEmpty() {
123                 return elements.isEmpty();
124         }
125
126         /**
127          * Adds a discovered element.
128          *
129          * @param element
130          *            The new element
131          */
132         public void add(T element) {
133                 elements.add(element);
134                 touch();
135         }
136
137         /**
138          * Removes the given element from the list of new elements.
139          *
140          * @param element
141          *            The element to remove
142          */
143         public void remove(T element) {
144                 while (elements.remove(element)) {
145                         /* do nothing, just remove all instances of the element. */
146                 }
147                 if (elements.isEmpty()) {
148                         dismiss();
149                 }
150                 touch();
151         }
152
153         //
154         // ABSTRACTNOTIFICATION METHODS
155         //
156
157         /**
158          * {@inheritDoc}
159          */
160         @Override
161         public void dismiss() {
162                 super.dismiss();
163                 elements.clear();
164         }
165
166         //
167         // OBJECT METHODS
168         //
169
170         /**
171          * {@inheritDoc}
172          */
173         @Override
174         public int hashCode() {
175                 int hashCode = super.hashCode();
176                 for (T element : elements) {
177                         hashCode ^= element.hashCode();
178                 }
179                 return hashCode;
180         }
181
182         /**
183          * {@inheritDoc}
184          */
185         @Override
186         public boolean equals(Object object) {
187                 if (!(object instanceof ListNotification)) {
188                         return false;
189                 }
190                 ListNotification<?> listNotification = (ListNotification<?>) object;
191                 if (!super.equals(listNotification)) {
192                         return false;
193                 }
194                 if (!key.equals(listNotification.key)) {
195                         return false;
196                 }
197                 if (elements.size() != listNotification.elements.size()) {
198                         return false;
199                 }
200                 for (int index = 0; index < elements.size(); ++index) {
201                         if (!elements.get(index).equals(listNotification.elements.get(index))) {
202                                 return false;
203                         }
204                 }
205                 return true;
206         }
207
208 }