X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.utils.thread%2Fsrc%2Forg%2Fsimantics%2Futils%2Fthreads%2Finternal%2FListenerList.java;fp=bundles%2Forg.simantics.utils.thread%2Fsrc%2Forg%2Fsimantics%2Futils%2Fthreads%2Finternal%2FListenerList.java;h=b1e6b6e2b0fea224d2b0f0987a1e802b3fd883ee;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;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 new file mode 100644 index 000000000..b1e6b6e2b --- /dev/null +++ b/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/internal/ListenerList.java @@ -0,0 +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