1 /*
2 * Copyright (C) 2010, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 package org.eclipse.jgit.events;
45
46 import java.util.List;
47 import java.util.concurrent.ConcurrentHashMap;
48 import java.util.concurrent.ConcurrentMap;
49 import java.util.concurrent.CopyOnWriteArrayList;
50
51 /**
52 * Manages a thread-safe list of {@link org.eclipse.jgit.events.RepositoryListener}s.
53 */
54 public class ListenerList {
55 private final ConcurrentMap<Class<? extends RepositoryListener>, CopyOnWriteArrayList<ListenerHandle>> lists = new ConcurrentHashMap<>();
56
57 /**
58 * Register a {@link org.eclipse.jgit.events.WorkingTreeModifiedListener}.
59 *
60 * @param listener
61 * the listener implementation.
62 * @return handle to later remove the listener.
63 * @since 4.9
64 */
65 public ListenerHandle addWorkingTreeModifiedListener(
66 WorkingTreeModifiedListener listener) {
67 return addListener(WorkingTreeModifiedListener.class, listener);
68 }
69
70 /**
71 * Register an IndexChangedListener.
72 *
73 * @param listener
74 * the listener implementation.
75 * @return handle to later remove the listener.
76 */
77 public ListenerHandle addIndexChangedListener(IndexChangedListener listener) {
78 return addListener(IndexChangedListener.class, listener);
79 }
80
81 /**
82 * Register a RefsChangedListener.
83 *
84 * @param listener
85 * the listener implementation.
86 * @return handle to later remove the listener.
87 */
88 public ListenerHandle addRefsChangedListener(RefsChangedListener listener) {
89 return addListener(RefsChangedListener.class, listener);
90 }
91
92 /**
93 * Register a ConfigChangedListener.
94 *
95 * @param listener
96 * the listener implementation.
97 * @return handle to later remove the listener.
98 */
99 public ListenerHandle addConfigChangedListener(
100 ConfigChangedListener listener) {
101 return addListener(ConfigChangedListener.class, listener);
102 }
103
104 /**
105 * Add a listener to the list.
106 *
107 * @param type
108 * type of listener being registered.
109 * @param listener
110 * the listener instance.
111 * @return a handle to later remove the registration, if desired.
112 */
113 public <T extends RepositoryListener> ListenerHandle addListener(
114 Class<T> type, T listener) {
115 ListenerHandle handle = new ListenerHandle(this, type, listener);
116 add(handle);
117 return handle;
118 }
119
120 /**
121 * Dispatch an event to all interested listeners.
122 * <p>
123 * Listeners are selected by the type of listener the event delivers to.
124 *
125 * @param event
126 * the event to deliver.
127 */
128 @SuppressWarnings("unchecked")
129 public void dispatch(RepositoryEvent event) {
130 List<ListenerHandle> list = lists.get(event.getListenerType());
131 if (list != null) {
132 for (ListenerHandle handle : list)
133 event.dispatch(handle.listener);
134 }
135 }
136
137 private void add(ListenerHandle handle) {
138 List<ListenerHandle> list = lists.get(handle.type);
139 if (list == null) {
140 CopyOnWriteArrayList<ListenerHandle> newList;
141
142 newList = new CopyOnWriteArrayList<>();
143 list = lists.putIfAbsent(handle.type, newList);
144 if (list == null)
145 list = newList;
146 }
147 list.add(handle);
148 }
149
150 void remove(ListenerHandle handle) {
151 List<ListenerHandle> list = lists.get(handle.type);
152 if (list != null)
153 list.remove(handle);
154 }
155 }