View Javadoc
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  /** Manages a thread-safe list of {@link RepositoryListener}s. */
52  public class ListenerList {
53  	private final ConcurrentMap<Class<? extends RepositoryListener>, CopyOnWriteArrayList<ListenerHandle>> lists = new ConcurrentHashMap<Class<? extends RepositoryListener>, CopyOnWriteArrayList<ListenerHandle>>();
54  
55  	/**
56  	 * Register an IndexChangedListener.
57  	 *
58  	 * @param listener
59  	 *            the listener implementation.
60  	 * @return handle to later remove the listener.
61  	 */
62  	public ListenerHandle addIndexChangedListener(IndexChangedListener listener) {
63  		return addListener(IndexChangedListener.class, listener);
64  	}
65  
66  	/**
67  	 * Register a RefsChangedListener.
68  	 *
69  	 * @param listener
70  	 *            the listener implementation.
71  	 * @return handle to later remove the listener.
72  	 */
73  	public ListenerHandle addRefsChangedListener(RefsChangedListener listener) {
74  		return addListener(RefsChangedListener.class, listener);
75  	}
76  
77  	/**
78  	 * Register a ConfigChangedListener.
79  	 *
80  	 * @param listener
81  	 *            the listener implementation.
82  	 * @return handle to later remove the listener.
83  	 */
84  	public ListenerHandle addConfigChangedListener(
85  			ConfigChangedListener listener) {
86  		return addListener(ConfigChangedListener.class, listener);
87  	}
88  
89  	/**
90  	 * Add a listener to the list.
91  	 *
92  	 * @param <T>
93  	 *            the type of listener being registered.
94  	 * @param type
95  	 *            type of listener being registered.
96  	 * @param listener
97  	 *            the listener instance.
98  	 * @return a handle to later remove the registration, if desired.
99  	 */
100 	public <T extends RepositoryListener> ListenerHandle addListener(
101 			Class<T> type, T listener) {
102 		ListenerHandle handle = new ListenerHandle(this, type, listener);
103 		add(handle);
104 		return handle;
105 	}
106 
107 	/**
108 	 * Dispatch an event to all interested listeners.
109 	 * <p>
110 	 * Listeners are selected by the type of listener the event delivers to.
111 	 *
112 	 * @param event
113 	 *            the event to deliver.
114 	 */
115 	@SuppressWarnings("unchecked")
116 	public void dispatch(RepositoryEvent event) {
117 		List<ListenerHandle> list = lists.get(event.getListenerType());
118 		if (list != null) {
119 			for (ListenerHandle handle : list)
120 				event.dispatch(handle.listener);
121 		}
122 	}
123 
124 	private void add(ListenerHandle handle) {
125 		List<ListenerHandle> list = lists.get(handle.type);
126 		if (list == null) {
127 			CopyOnWriteArrayList<ListenerHandle> newList;
128 
129 			newList = new CopyOnWriteArrayList<ListenerHandle>();
130 			list = lists.putIfAbsent(handle.type, newList);
131 			if (list == null)
132 				list = newList;
133 		}
134 		list.add(handle);
135 	}
136 
137 	void remove(ListenerHandle handle) {
138 		List<ListenerHandle> list = lists.get(handle.type);
139 		if (list != null)
140 			list.remove(handle);
141 	}
142 }