//
// This file is part of the prose package.
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// The Original Code is prose.
//
// The Initial Developer of the Original Code is Andrei Popovici. Portions
// created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
// All Rights Reserved.
//
// Contributor(s):
/*
* Java core library component.
*/
package ch.ethz.prose.engine;
import ch.ethz.prose.Insertable;
public class ListenerList
{
public JoinPointListener[] listenerArray;
public int nrOfListeners;
private int nearestPowerOfTwo;
public ListenerList()
{
listenerArray = new JoinPointListener[16];
nearestPowerOfTwo = 16;
}
private void ensureCapacity(int newCapacity)
{
// Back if we have enough space
if (listenerArray.length >= newCapacity)
return;
nearestPowerOfTwo = 2* nearestPowerOfTwo;
// idea of subtracting 12: from Transvirtual
JoinPointListener[] newListenerArray = new JoinPointListener[nearestPowerOfTwo - 12];
System.arraycopy(listenerArray, 0, newListenerArray, 0, nrOfListeners);
listenerArray = newListenerArray;
}
public synchronized boolean contains(Object elem)
{
for (int i = 0; i < nrOfListeners; i++)
if ( (elem == null && listenerArray[i] == null ) ||
(elem != null && elem.equals(listenerArray[i]) ) )
return true;
return false;
}
static class CrosscutComparator implements java.util.Comparator
{
public int compare(Object o1, Object o2)
{
if (o1 instanceof Insertable && o2 instanceof Insertable)
return ((Insertable)o1).getPriority() - ((Insertable)o2).getPriority();
else
return 0;
}
};
public synchronized boolean add(JoinPointListener element)
{
int index = nrOfListeners;
CrosscutComparator comparator = new CrosscutComparator();
// BUGFIX: ArrayIndexOutOfBoundException -> the error was
// that in the JVM could be loaded only 16 aspects.
for (index = 0; index < nrOfListeners &&
comparator.compare(listenerArray[index],element) < 0; index++);
ensureCapacity(nrOfListeners + 1);
System.arraycopy(listenerArray, index,
listenerArray, index + 1, nrOfListeners - index);
listenerArray[index] = element;
nrOfListeners++;
// our contract
return true;
}
public synchronized boolean remove(Object element)
{
int indexToRemove = -1;
for (int i = 0; i < nrOfListeners; i++)
if (element != null && element.equals(listenerArray[i]) )
{
indexToRemove = i;
remove(indexToRemove);
i--;
}
return (indexToRemove == -1);
}
public synchronized boolean isEmpty()
{
return nrOfListeners == 0;
}
private Object remove(int index)
{
Object old = listenerArray[index]; // exception here is OK
if (index < 0 || index > nrOfListeners - 1)
throw new IndexOutOfBoundsException();
System.arraycopy(listenerArray, index + 1,
listenerArray, index, nrOfListeners - index - 1);
nrOfListeners --;
return old;
}
public String toString()
{
StringBuffer result = new StringBuffer();
for (int i = 0; i<nrOfListeners; i++)
{
result.append(listenerArray[i]);
if (i != (nrOfListeners -1) )
result.append(",");
}
return result.toString();
}
}
|