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