package com.team254.lib.util;

import java.util.Hashtable;
import java.util.Vector;

/**
 * Controls all message subscriptions for Listeners.
 * Also allows Listeners to subscribe and unsubscribe from messages.
 *
 * @author stephen@team254.com (Stephen Pinkerton)
 * @author art.kalb96@gmail.com (Arthur Kalb)
 */
public class Notifier {

  private Hashtable listeners;

  public Notifier() {
    listeners = new Hashtable();
  }

  /**
   * Sends a message to all of the subscribed listeners.
   * @param key: the hash table key that points to a vector of listeners
   * @param value: the value of the message
   */
  public void publish(Integer key, double value) {
    Vector v = (Vector)listeners.get(key);
    for (int i = 0; i < v.size(); i++) {
      ((Listener)(v.elementAt(i))).receive(key.intValue(),value);
    }
  }
  
  /**
   * Subscribes a listener to a specific message.
   * @param key: message key to subscribe the listener to
   * @param listener: the listener to subscribe to the message key
   */
  public void subscribe(Integer key, Listener listener) {
    if (listeners.get(key) == null) {
      listeners.put(key, new Vector());
    }
    ((Vector)listeners.get(key)).addElement(listener);
  }

  /**
   * Unsubscribes a listener from a specified message (if it exists).
   * @param key: message key to unsubscribe the listener from
   * @param listener: the listener being unsubscribed from the message key
   */
  public void unsubscribe(Integer key, Listener listener){
    Vector v = (Vector)listeners.get(key);
    if(v != null) {
      v.removeElement(listener);
    }
  }
}

@@ -343,16 +343,23 @@
 private 
+static 
 Hashtabl
@@ -375,62 +375,343 @@
 rs;%0A
-%0A
   p
-ublic Notifier() %7B%0A    listeners = new Hashtable()
+rivate static Notifier instance;%0A  %0A  private Notifier() %7B%0A    listeners = new Hashtable();%0A  %7D%0A%0A  /**%0A   * Ensures Notifier is a singleton and returns the instance%0A   * @return: instance of Notifier%0A   */%0A  public static Notifier getInstance() %7B%0A    if(instance == null) %7B%0A      instance = new Notifier();%0A    %7D%0A    return instance
 ;%0A  
@@ -904,24 +904,31 @@
 */%0A  public 
+static 
 void publish
@@ -996,24 +996,45 @@
 s.get(key);%0A
+    if(v != null)%7B%0A  
     for (int
@@ -1058,24 +1058,26 @@
 e(); i++) %7B%0A
+  
       ((List
@@ -1130,16 +1130,24 @@
 value);%0A
+      %7D%0A
     %7D%0A  
@@ -1343,24 +1343,31 @@
 */%0A  public 
+static 
 void subscri
@@ -1775,16 +1775,23 @@
  public 
+static 
 void uns

Make Notifier a singleton, make methods static, check for null Vectors