Also, add method for removing listeners. Signed-off-by: Tor Arne Vestbø <torarnv@xxxxxxxxx> --- .../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 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html