2 * Sone - ListNotification.java - Copyright © 2010–2019 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.notify
20 import net.pterodactylus.util.notify.*
21 import net.pterodactylus.util.template.*
22 import java.lang.System.*
23 import java.util.concurrent.*
26 * Notification that maintains a list of elements.
29 * The type of the items
31 class ListNotification<T> : TemplateNotification {
33 private val key: String
34 private val realElements = CopyOnWriteArrayList<T>()
36 val elements: List<T> get() = realElements.toList()
39 get() = elements.isEmpty()
42 constructor(id: String, key: String, template: Template, dismissable: Boolean = true) : super(id, currentTimeMillis(), currentTimeMillis(), dismissable, template) {
44 template.initialContext.set(key, realElements)
47 constructor(listNotification: ListNotification<T>) : super(listNotification.id, listNotification.createdTime, listNotification.lastUpdatedTime, listNotification.isDismissable, Template()) {
48 this.key = listNotification.key
49 template.add(listNotification.template)
50 template.initialContext.set(key, realElements)
53 fun setElements(elements: Collection<T>) {
55 realElements.addAll(elements.distinct())
60 if (element !in realElements) {
61 realElements.add(element)
66 fun remove(element: T) {
67 while (realElements.remove(element)) {
68 /* do nothing, just remove all instances of the element. */
70 if (realElements.isEmpty()) {
76 override fun dismiss() {
81 override fun hashCode() =
82 realElements.fold(super.hashCode()) { hash, element -> hash xor element.hashCode() }
84 override fun equals(other: Any?): Boolean {
85 if (other !is ListNotification<*>) {
88 val listNotification = other as ListNotification<*>?
89 if (!super.equals(listNotification)) {
92 return (key == listNotification.key) && (realElements == listNotification.realElements)