X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils.thread%2Fsrc%2Forg%2Fsimantics%2Futils%2Fthreads%2Finternal%2FListenerList.java;h=b73c10ffceaf4ba8333110df2a1b2eb726fa9482;hb=965fa411938344172cff82a2ec0b6c50b7cb2b7c;hp=b1e6b6e2b0fea224d2b0f0987a1e802b3fd883ee;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/internal/ListenerList.java b/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/internal/ListenerList.java index b1e6b6e2b..b73c10ffc 100644 --- a/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/internal/ListenerList.java +++ b/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/internal/ListenerList.java @@ -1,120 +1,120 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -/* - * 12.6.2007 - */ -package org.simantics.utils.threads.internal; - -import java.lang.reflect.Array; - -/** - * ListenerList is an efficient and synchronized list of listeners. - * The class is optimized for quick getListeners and slow add/remove. - * - * @author Toni Kalajainen - */ -public class ListenerList { - - /** - * Array of listeners - */ - private volatile T [] array; - - /** - * The class of T - */ - private final Class componentType; - - /** - * Construct new Listener List - * @param componentType the class of the listener type - */ - public ListenerList(Class componentType) - { - this.componentType = componentType; - array = createArray(0); - } - - public T[] getListeners() - { - return array; - } - - public synchronized void add(T listener) - { - int oldLength = array.length; - int newLength = oldLength + 1; - T newArray[] = createArray(newLength); - System.arraycopy(array, 0, newArray, 0, oldLength); - newArray[oldLength] = listener; - array = newArray; - } - - /** - * Removes the first occurance of listener. - * If the listener is added multiple times, then it must be removed - * as many times. - * - * @param listener a listener - * @return the listener that was removed from the list - */ - public synchronized boolean remove(T listener) - { - int pos = getPos(listener); - if (pos<0) return false; - - int oldLength = array.length; - int newLength = oldLength -1; - T newArray[] = createArray(newLength); - - // Copy beginning - if (pos>0) - System.arraycopy(array, 0, newArray, 0, pos); - - // Copy ending - if (pos { + + /** + * Array of listeners + */ + private volatile T [] array; + + /** + * The class of T + */ + private final Class componentType; + + /** + * Construct new Listener List + * @param componentType the class of the listener type + */ + public ListenerList(Class componentType) + { + this.componentType = componentType; + array = createArray(0); + } + + public T[] getListeners() + { + return array; + } + + public synchronized void add(T listener) + { + int oldLength = array.length; + int newLength = oldLength + 1; + T newArray[] = createArray(newLength); + System.arraycopy(array, 0, newArray, 0, oldLength); + newArray[oldLength] = listener; + array = newArray; + } + + /** + * Removes the first occurance of listener. + * If the listener is added multiple times, then it must be removed + * as many times. + * + * @param listener a listener + * @return the listener that was removed from the list + */ + public synchronized boolean remove(T listener) + { + int pos = getPos(listener); + if (pos<0) return false; + + int oldLength = array.length; + int newLength = oldLength -1; + T newArray[] = createArray(newLength); + + // Copy beginning + if (pos>0) + System.arraycopy(array, 0, newArray, 0, pos); + + // Copy ending + if (pos