X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.procore%2Fsrc%2Ffi%2Fvtt%2Fsimantics%2Fprocore%2Finternal%2FListenerList.java;h=3b9333e12aa56090fceb29fd2a4ce54893ced5d5;hp=f818124097d66d6afcc964028e1a36be9cf830a9;hb=HEAD;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ListenerList.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ListenerList.java index f81812409..3b9333e12 100644 --- a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ListenerList.java +++ b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ListenerList.java @@ -1,117 +1,117 @@ -/******************************************************************************* - * 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 - *******************************************************************************/ -package fi.vtt.simantics.procore.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