1 6 package org.logicalcobwebs.proxool.util; 7 8 9 34 public abstract class AbstractListenerContainer implements ListenerContainerIF { 35 36 private Object [] listeners = EMPTY_LISTENERS; 37 private static final Object [] EMPTY_LISTENERS = new Object []{}; 38 39 40 43 public synchronized void addListener(Object listener) 44 { 45 if(listener==null) 46 throw new NullPointerException ("Unexpected NULL listener argument received"); 47 48 Object [] newListeners = new Object [listeners.length+1]; 50 51 System.arraycopy(listeners, 0, newListeners, 0, listeners.length); 53 54 newListeners[listeners.length] = listener; 56 57 listeners = newListeners; 59 } 60 61 62 65 public synchronized boolean removeListener(Object listener) 66 { 67 if(listener==null) 68 throw new NullPointerException ("Unexpected NULL listener argument received"); 69 70 int index=-1; 72 for(int i=0; i<listeners.length; i++) { 73 if( listeners[i]==listener ) { 74 index = i; 75 break; 76 } 77 } 78 79 if( index==-1 ) 81 return false; 82 83 Object [] newListeners = new Object [listeners.length-1]; 85 86 if( index > 0 ) 88 System.arraycopy(listeners, 0, newListeners, 0, index); 89 90 if( index < listeners.length-1 ) 91 System.arraycopy(listeners, index+1, newListeners, index, listeners.length-index-1); 92 93 listeners = newListeners; 95 return true; 96 } 97 98 99 104 protected Object [] getListeners() { 105 return listeners; 106 } 107 108 109 112 public boolean isEmpty() { 113 return listeners.length==0; 114 } 115 } 116 117 | Popular Tags |