File Source: CallbackManager.java

     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.parser.irc.callbacks;
    24  
    25  import com.dmdirc.parser.irc.IRCParser;
    26  import com.dmdirc.parser.irc.callbacks.interfaces.*;
    27  
    28  import java.util.Hashtable;
    29  import java.util.Map;
    30  
    31  /**
    32   * IRC Parser Callback Manager.
    33   * Manages adding/removing/calling callbacks.
    34   *
    35   * @author            Shane Mc Cormack
    36   */
    37  public final class CallbackManager {
    38  
             /* 
    P/P       *  Method: com.dmdirc.parser.irc.callbacks.CallbackManager__static_init
              * 
              *  Postconditions:
              *    CLASSES == &new Class[](CallbackManager__static_init#1)
              *    new Class[](CallbackManager__static_init#1) num objects == 1
              *    CLASSES.length == 59
              */
    39      private static final Class[] CLASSES = {
    40          IAwayState.class, IAwayStateOther.class, IChannelAwayStateOther.class,
    41          IChannelAction.class, IChannelCTCP.class, IChannelCTCPReply.class,
    42          IChannelGotListModes.class, IChannelGotNames.class, IChannelJoin.class,
    43          IChannelKick.class, IChannelMessage.class, IChannelModeChanged.class,
    44          IChannelNickChanged.class, IChannelNonUserModeChanged.class,
    45          IChannelNotice.class, IChannelPart.class, IChannelQuit.class,
    46          IChannelSelfJoin.class, IChannelSingleModeChanged.class,
    47          IChannelTopic.class, IChannelUserModeChanged.class, IConnectError.class,
    48          IDataIn.class, IDataOut.class, IDebugInfo.class, IErrorInfo.class,
    49          IGotNetwork.class, IInvite.class, IMOTDEnd.class, IMOTDLine.class,
    50          IMOTDStart.class, INickChanged.class, INickInUse.class,
    51          INoticeAuth.class, INumeric.class, IPasswordRequired.class,
    52          IPingFailed.class, IPingSuccess.class, IPingSent.class, IPrivateAction.class,
    53          IPrivateCTCP.class, IPrivateCTCPReply.class, IPrivateMessage.class,
    54          IPrivateNotice.class, IPost005.class, IQuit.class, IServerError.class,
    55          IServerReady.class, ISocketClosed.class, IUnknownAction.class,
    56          IUnknownCTCP.class, IUnknownCTCPReply.class, IUnknownMessage.class,
    57          IUnknownNotice.class, IUserModeChanged.class, IUserModeDiscovered.class,
    58          IWallDesync.class, IWallop.class, IWalluser.class,
    59      };
    60  
    61  	/** Reference to the parser object that owns this CallbackManager. */
    62  	IRCParser myParser;
    63  	
    64  	/** Hashtable used to store the different types of callback known. */
    65  	private final Map<String, CallbackObject> callbackHash = new Hashtable<String, CallbackObject>();
    66  	
    67  	/**
    68  	 * Constructor to create a CallbackManager.
    69  	 *
    70  	 * @param parser IRCParser that owns this callback manager.
    71  	 */
        	 /* 
    P/P 	  *  Method: void com.dmdirc.parser.irc.callbacks.CallbackManager(IRCParser)
        	  * 
        	  *  Preconditions:
        	  *    (soft) CLASSES[...] != null
        	  * 
        	  *  Presumptions:
        	  *    java.lang.Class:asSubclass(...)@77 != null
        	  *    java.lang.Class:asSubclass(...)@80 != null
        	  * 
        	  *  Postconditions:
        	  *    this.callbackHash == &amp;new Hashtable(CallbackManager#1)
        	  *    this.myParser == parser
        	  *    init'ed(this.myParser)
        	  *    new Hashtable(CallbackManager#1) num objects == 1
        	  * 
        	  *  Test Vectors:
        	  *    java.lang.Class:isAnnotationPresent(...)@76: {0}, {1}
        	  */
    72  	public CallbackManager(final IRCParser parser) {
    73  		myParser = parser;
    74  		
    75          for (Class<?> type : CLASSES) {
    76              if (type.isAnnotationPresent(SpecificCallback.class)) {
    77                  addCallbackType(new CallbackObjectSpecific(myParser, this,
    78                          type.asSubclass(ICallbackInterface.class)));
    79              } else {
    80                  addCallbackType(new CallbackObject(myParser, this,
    81                          type.asSubclass(ICallbackInterface.class)));
    82              }
    83          }
    84  	}
    85  
    86  	/**
    87  	 * Add new callback type.
    88  	 *
    89  	 * @param callback CallbackObject subclass for the callback.
    90  	 * @return if adding succeeded or not.
    91  	 */
    92  	public boolean addCallbackType(final CallbackObject callback) {
        		 /* 
    P/P 		  *  Method: bool addCallbackType(CallbackObject)
        		  * 
        		  *  Preconditions:
        		  *    callback != null
        		  *    callback.type != null
        		  *    this.callbackHash != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    java.util.Map:containsKey(...)@93: {1}, {0}
        		  */
    93  		if (!callbackHash.containsKey(callback.getLowerName())) {
    94  			callbackHash.put(callback.getLowerName(), callback);
    95  			return true;
    96  		}
    97  		return false;
    98  	}
    99  	
   100  	/**
   101  	 * Remove a callback type.
   102  	 *
   103  	 * @param callback CallbackObject subclass to remove.
   104  	 * @return if removal succeeded or not.
   105  	 */
   106  	public boolean delCallbackType(final CallbackObject callback) {
        		 /* 
    P/P 		  *  Method: bool delCallbackType(CallbackObject)
        		  * 
        		  *  Preconditions:
        		  *    callback != null
        		  *    callback.type != null
        		  *    this.callbackHash != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    java.util.Map:containsKey(...)@107: {0}, {1}
        		  */
   107  		if (callbackHash.containsKey(callback.getLowerName())) {
   108  			callbackHash.remove(callback.getLowerName());
   109  			return true;
   110  		}
   111  		return false;
   112  	}
   113  	
   114  	/**
   115  	 * Get reference to callback object.
   116  	 *
   117  	 * @param callbackName Name of callback object.
   118  	 * @return CallbackObject returns the callback object for this type
   119  	 */
   120  	public CallbackObject getCallbackType(final String callbackName) {
        		 /* 
    P/P 		  *  Method: CallbackObject getCallbackType(String)
        		  * 
        		  *  Preconditions:
        		  *    callbackName != null
        		  *    this.callbackHash != null
        		  * 
        		  *  Presumptions:
        		  *    java.util.Map:containsKey(...)@121 == 1
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  */
   121  		if (!callbackHash.containsKey(callbackName.toLowerCase())) {
   122  			throw new CallbackNotFoundException("Callback not found: " + callbackName);
   123  		}
   124  		
   125  		return callbackHash.get(callbackName.toLowerCase());
   126  	}
   127  	
   128  	/**
   129  	 * Remove all callbacks associated with a specific object.
   130  	 *
   131  	 * @param o instance of ICallbackInterface to remove.
   132  	 */
   133  	public void delAllCallback(final ICallbackInterface o) {
        		 /* 
    P/P 		  *  Method: void delAllCallback(ICallbackInterface)
        		  * 
        		  *  Preconditions:
        		  *    this.callbackHash != null
        		  * 
        		  *  Presumptions:
        		  *    cb.callbackInfo@134 != null
        		  *    java.util.Map:values(...)@134 != null
        		  * 
        		  *  Test Vectors:
        		  *    java.util.Iterator:hasNext(...)@134: {0}, {1}
        		  *    java.util.Iterator:next(...)@134: Addr_Set{null}, Inverse{null}
        		  */
   134  		for (CallbackObject cb : callbackHash.values()) {
   135  			if (cb != null) { cb.del(o); }
   136  		}
   137  	}
   138  	
   139  	/**
   140  	 * Add all callbacks.
   141  	 *
   142  	 * @param o instance of ICallbackInterface to add.
   143  	 */
   144  	public void addAllCallback(final ICallbackInterface o) {
        		 /* 
    P/P 		  *  Method: void addAllCallback(ICallbackInterface)
        		  * 
        		  *  Preconditions:
        		  *    this.callbackHash != null
        		  * 
        		  *  Presumptions:
        		  *    cb.callbackInfo@145 != null
        		  *    java.util.Map:values(...)@145 != null
        		  * 
        		  *  Test Vectors:
        		  *    java.util.Iterator:hasNext(...)@145: {0}, {1}
        		  *    java.util.Iterator:next(...)@145: Addr_Set{null}, Inverse{null}
        		  */
   145  		for (CallbackObject cb : callbackHash.values()) {
   146  			if (cb != null) { cb.add(o); }
   147  		}
   148  	}
   149  	
   150  	/**
   151  	 * Add a callback.
   152   	 * This method will throw a CallbackNotFoundException if the callback does not exist.
   153  	 *
   154  	 * @param callbackName Name of callback object.
   155  	 * @param o instance of ICallbackInterface to add.
   156  	 * @throws CallbackNotFoundException If callback is not found.
   157  	 * @throws NullPointerException If 'o' is null
   158  	 */
   159  	public void addCallback(final String callbackName, final ICallbackInterface o) throws CallbackNotFoundException {
        		 /* 
    P/P 		  *  Method: void addCallback(String, ICallbackInterface)
        		  * 
        		  *  Preconditions:
        		  *    callbackName != null
        		  *    o != null
        		  *    this.callbackHash != null
        		  * 
        		  *  Presumptions:
        		  *    cb.callbackInfo != null
        		  */
   160  		if (o == null) {
   161  			throw new NullPointerException("CallbackInterface is null");
   162  		}
   163  		
   164  		final CallbackObject cb = getCallbackType(callbackName);
   165  		
   166  		if (cb != null) { cb.add(o); }
   167  	}
   168  	
   169  	/**
   170  	 * Add a callback with a specific target.
   171   	 * This method will throw a CallbackNotFoundException if the callback does not exist.
   172  	 *
   173  	 * @param callbackName Name of callback object.
   174  	 * @param o instance of ICallbackInterface to add.
   175  	 * @param target Parameter to specify that a callback should only fire for specific things
   176  	 * @throws CallbackNotFoundException If callback is not found.
   177  	 * @throws NullPointerException If 'o' is null
   178  	 */
   179  	public void addCallback(final String callbackName, final ICallbackInterface o, final String target) throws CallbackNotFoundException {
        		 /* 
    P/P 		  *  Method: void addCallback(String, ICallbackInterface, String)
        		  * 
        		  *  Preconditions:
        		  *    callbackName != null
        		  *    o != null
        		  *    target != null
        		  *    this.callbackHash != null
        		  * 
        		  *  Presumptions:
        		  *    getCallbackType(...).callbackInfo != null
        		  *    getCallbackType(...).specificData != null
        		  */
   180  		if (o == null) { throw new NullPointerException("CallbackInterface is null"); }
   181  		((CallbackObjectSpecific) getCallbackType(callbackName)).add(o,target);
   182  	}
   183  	
   184  	/**
   185  	 * Add a callback without an exception.
   186  	 * This should be used if a callback is not essential for execution (ie the DebugOut callback)
   187  	 *
   188  	 * @param callbackName Name of callback object.
   189  	 * @param o instance of ICallbackInterface to add.
   190  	 * @return true/false if the callback was added or not.
   191  	 */
   192  	public boolean addNonCriticalCallback(final String callbackName, final ICallbackInterface o)  {
   193  		try {
        			 /* 
    P/P 			  *  Method: bool addNonCriticalCallback(String, ICallbackInterface)
        			  * 
        			  *  Preconditions:
        			  *    (soft) callbackName != null
        			  *    (soft) o != null
        			  *    (soft) this.callbackHash != null
        			  * 
        			  *  Postconditions:
        			  *    init'ed(return_value)
        			  */
   194  			addCallback(callbackName, o);
   195  			return true;
   196  		} catch (CallbackNotFoundException e) { return false; }
   197  	}
   198  	
   199  	/**
   200  	 * Add a callback with a specific target.
   201  	 * This should be used if a callback is not essential for execution
   202  	 *
   203  	 * @param callbackName Name of callback object.
   204  	 * @param o instance of ICallbackInterface to add.
   205  	 * @param target Parameter to specify that a callback should only fire for specific things
   206  	 * @return true/false if the callback was added or not.
   207  	 */
   208  	public boolean addNonCriticalCallback(final String callbackName, final ICallbackInterface o, final String target) {
   209  		try {
        			 /* 
    P/P 			  *  Method: bool addNonCriticalCallback(String, ICallbackInterface, String)
        			  * 
        			  *  Preconditions:
        			  *    (soft) callbackName != null
        			  *    (soft) o != null
        			  *    (soft) target != null
        			  *    (soft) this.callbackHash != null
        			  * 
        			  *  Postconditions:
        			  *    init'ed(return_value)
        			  */
   210  			addCallback(callbackName, o, target);
   211  			return true;
   212  		} catch (CallbackNotFoundException e) { return false;	}
   213  	}
   214  	
   215  	
   216  	/**
   217  	 * Remove a callback.
   218  	 *
   219  	 * @param callbackName Name of callback object.
   220  	 * @param o instance of ICallbackInterface to remove.
   221  	 */
   222  	public void delCallback(final String callbackName, final ICallbackInterface o) {
        		 /* 
    P/P 		  *  Method: void delCallback(String, ICallbackInterface)
        		  * 
        		  *  Preconditions:
        		  *    callbackName != null
        		  *    this.callbackHash != null
        		  * 
        		  *  Presumptions:
        		  *    getCallbackType(...).callbackInfo != null
        		  */
   223  		getCallbackType(callbackName).del(o);
   224  	}
   225  
   226  }








SofCheck Inspector Build Version : 2.17854
CallbackManager.java 2009-Jun-25 01:54:24
CallbackManager.class 2009-Sep-02 17:04:12