git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[EGIT PATCH v2 02/12] Use Set instead of array to keep track of change listeners

From
Tor Arne Vestbø <torarnv@gmail.com>
Date
Feb 11, 2009, 18:40 UTC
Message-ID
<1234377614-23798-3-git-send-email-torarnv@gmail.com>
In-Reply-To
<1234377614-23798-2-git-send-email-torarnv@gmail.com>
Also, add method for removing listeners.
Signed-off-by: Tor Arne Vestbø <torarnv@gmail.com>
---
 .../spearce/egit/core/project/GitProjectData.java  |   40 ++++++++++++-------
 1 files changed, 25 insertions(+), 15 deletions(-)
diff --git a/org.spearce.egit.core/src/org/spearce/egit/core/project/GitProjectData.java b/org.spearce.egit.core/src/org/spearce/egit/core/project/GitProjectData.java
index db5f20b..b12a85f 100644
--- a/org.spearce.egit.core/src/org/spearce/egit/core/project/GitProjectData.java
+++ b/org.spearce.egit.core/src/org/spearce/egit/core/project/GitProjectData.java
@@ -52,7 +52,7 @@
 
 	private static final Map<File, WeakReference> repositoryCache = new HashMap<File, WeakReference>();
 
-	private static RepositoryChangeListener[] repositoryChangeListeners = {};
+	private static Set<RepositoryChangeListener> repositoryChangeListeners = new HashSet<RepositoryChangeListener>();
 
 	@SuppressWarnings("synthetic-access")
 	private static final IResourceChangeListener rcl = new RCL();
@@ -112,16 +112,18 @@ public static synchronized void addRepositoryChangeListener(
 			final RepositoryChangeListener objectThatCares) {
 		if (objectThatCares == null)
 			throw new NullPointerException();
-		for (int k = repositoryChangeListeners.length - 1; k >= 0; k--) {
-			if (repositoryChangeListeners[k] == objectThatCares)
-				return;
-		}
-		final int p = repositoryChangeListeners.length;
-		final RepositoryChangeListener[] n;
-		n = new RepositoryChangeListener[p + 1];
-		System.arraycopy(repositoryChangeListeners, 0, n, 0, p);
-		n[p] = objectThatCares;
-		repositoryChangeListeners = n;
+		repositoryChangeListeners.add(objectThatCares);
+	}
+
+	/**
+	 * Remove a registered {@link RepositoryChangeListener}
+	 * 
+	 * @param objectThatCares
+	 *            The listener to remove
+	 */
+	public static synchronized void removeRepositoryChangeListener(
+			final RepositoryChangeListener objectThatCares) {
+		repositoryChangeListeners.remove(objectThatCares);
 	}
 
 	/**
@@ -131,13 +133,21 @@ public static synchronized void addRepositoryChangeListener(
 	 *            the repository which has had changes occur within it.
 	 */
 	static void fireRepositoryChanged(final RepositoryMapping which) {
-		final RepositoryChangeListener[] e = getRepositoryChangeListeners();
-		for (int k = e.length - 1; k >= 0; k--)
-			e[k].repositoryChanged(which);
+		for (RepositoryChangeListener listener : getRepositoryChangeListeners())
+			listener.repositoryChanged(which);
 	}
 
+	/**
+	 * Get a copy of the current set of repository change listeners
+	 * <p>
+	 * The array has no references, so is safe for iteration and modification
+	 * 
+	 * @return a copy of the current repository change listeners
+	 */
 	private static synchronized RepositoryChangeListener[] getRepositoryChangeListeners() {
-		return repositoryChangeListeners;
+		return repositoryChangeListeners
+				.toArray(new RepositoryChangeListener[repositoryChangeListeners
+						.size()]);
 	}
 
 	/**
-- 
1.6.1.2.309.g2ea3
Previous: Tor Arne VestbøNext: Tor Arne Vestbø
Message 3 of 21 in “Support customizable label decorations”
  1. 00/12 Support customizable label decorationsTor Arne Vestbø, Feb 11, 2009
  2. 01/12 Add support code to handle plugin property changesTor Arne Vestbø, Feb 11, 2009
  3. 02/12 Use Set instead of array to keep track of change listenersTor Arne Vestbø, Feb 11, 2009
  4. 03/12 Add a specialized team exception for GitTor Arne Vestbø, Feb 11, 2009
  5. 04/12 Add new class ExceptionCollector for grouping exceptionsTor Arne Vestbø, Feb 11, 2009
  6. 05/12 Add new class SWTUtils with helper-methods for creating controlsTor Arne Vestbø, Feb 11, 2009
  7. 06/12 Implement basic customizable label decorations with preferencesTor Arne Vestbø, Feb 11, 2009
  8. 07/12 Add binding for name of the current branchTor Arne Vestbø, Feb 11, 2009
  9. 08/12 Add icon decoration for tracked and untracked resourcesTor Arne Vestbø, Feb 11, 2009
  10. 09/12 Implement icon and text decorations of various resource statesTor Arne Vestbø, Feb 11, 2009
  11. 10/12 Don't decorate every single resource on repository changeTor Arne Vestbø, Feb 11, 2009
  12. 11/12 Expose the underlying resource entries in ContainerTreeIteratorTor Arne Vestbø, Feb 11, 2009
  13. 12/12 Implement label decorations for folders and projectsTor Arne Vestbø, Feb 11, 2009
  14. Robin RosenbergFeb 12, 2009
  15. Robin RosenbergFeb 11, 2009
  16. 08/12 Add icon decoration for tracked and untracked resourcesTor Arne Vestbø, Feb 11, 2009
  17. Robin RosenbergFeb 16, 2009
  18. Tor Arne VestbøFeb 16, 2009
  19. Robin RosenbergFeb 17, 2009
  20. 13/12 Add new file tree iterator that can adapt into a ContainerTreeIteratorTor Arne Vestbø, Feb 17, 2009
  21. 14/12 Allow project decorations regardless of repository root locationTor Arne Vestbø, Feb 17, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.