src/share/classes/com/sun/jndi/ldap/EventSupport.java

Print this page

        

@@ -25,15 +25,13 @@
 
 package com.sun.jndi.ldap;
 
 import java.util.Hashtable;
 import java.util.Vector;
-import java.util.Enumeration;
 import java.util.EventObject;
 
 import javax.naming.*;
-import javax.naming.directory.*;
 import javax.naming.event.*;
 import javax.naming.directory.SearchControls;
 import javax.naming.ldap.UnsolicitedNotificationListener;
 import javax.naming.ldap.UnsolicitedNotificationEvent;
 import javax.naming.ldap.UnsolicitedNotification;

@@ -118,16 +116,17 @@
     private LdapCtx ctx;
 
     /**
      * NamingEventNotifiers; hashed by search arguments;
      */
-    private Hashtable notifiers = new Hashtable(11);
+    private Hashtable<NotifierArgs, NamingEventNotifier> notifiers =
+            new Hashtable<>(11);
 
     /**
      * List of unsolicited notification listeners.
      */
-    private Vector unsolicited = null;
+    private Vector<UnsolicitedNotificationListener> unsolicited = null;
 
     /**
      * Constructs EventSupport for ctx.
      * <em>Do we need to record the name of the target context?
      * Or can we assume that EventSupport is called on a resolved

@@ -153,26 +152,25 @@
 
         if (l instanceof ObjectChangeListener ||
             l instanceof NamespaceChangeListener) {
             NotifierArgs args = new NotifierArgs(nm, scope, l);
 
-            NamingEventNotifier notifier =
-                (NamingEventNotifier) notifiers.get(args);
+            NamingEventNotifier notifier = notifiers.get(args);
             if (notifier == null) {
                 notifier = new NamingEventNotifier(this, ctx, args, l);
                 notifiers.put(args, notifier);
             } else {
                 notifier.addNamingListener(l);
             }
         }
         if (l instanceof UnsolicitedNotificationListener) {
             // Add listener to this's list of unsolicited notifiers
             if (unsolicited == null) {
-                unsolicited = new Vector(3);
+                unsolicited = new Vector<>(3);
             }
 
-            unsolicited.addElement(l);
+            unsolicited.addElement((UnsolicitedNotificationListener)l);
         }
     }
 
     /**
      * Adds <tt>l</tt> to list of listeners interested in <tt>nm</tt>

@@ -183,41 +181,36 @@
 
         if (l instanceof ObjectChangeListener ||
             l instanceof NamespaceChangeListener) {
             NotifierArgs args = new NotifierArgs(nm, filter, ctls, l);
 
-            NamingEventNotifier notifier =
-                (NamingEventNotifier) notifiers.get(args);
+            NamingEventNotifier notifier = notifiers.get(args);
             if (notifier == null) {
                 notifier = new NamingEventNotifier(this, ctx, args, l);
                 notifiers.put(args, notifier);
             } else {
                 notifier.addNamingListener(l);
             }
         }
         if (l instanceof UnsolicitedNotificationListener) {
             // Add listener to this's list of unsolicited notifiers
             if (unsolicited == null) {
-                unsolicited = new Vector(3);
+                unsolicited = new Vector<>(3);
             }
-            unsolicited.addElement(l);
+            unsolicited.addElement((UnsolicitedNotificationListener)l);
         }
     }
 
     /**
      * Removes <tt>l</tt> from all notifiers in this context.
      */
     synchronized void removeNamingListener(NamingListener l) {
-        Enumeration allnotifiers = notifiers.elements();
-        NamingEventNotifier notifier;
-
         if (debug) System.err.println("EventSupport removing listener");
 
         // Go through list of notifiers, remove 'l' from each.
         // If 'l' is notifier's only listener, remove notifier too.
-        while (allnotifiers.hasMoreElements()) {
-            notifier = (NamingEventNotifier)allnotifiers.nextElement();
+        for (NamingEventNotifier notifier : notifiers.values()) {
             if (notifier != null) {
                 if (debug)
                     System.err.println("EventSupport removing listener from notifier");
                 notifier.removeNamingListener(l);
                 if (!notifier.hasNamingListeners()) {

@@ -303,12 +296,12 @@
      * Package private; used by LdapCtx.
      */
     synchronized void cleanup() {
         if (debug) System.err.println("EventSupport clean up");
         if (notifiers != null) {
-            for (Enumeration ns = notifiers.elements(); ns.hasMoreElements(); ) {
-                ((NamingEventNotifier) ns.nextElement()).stop();
+            for (NamingEventNotifier notifier : notifiers.values()) {
+                notifier.stop();
             }
             notifiers = null;
         }
         if (eventQueue != null) {
             eventQueue.stop();

@@ -326,11 +319,12 @@
      * Add the event and vector of listeners to the queue to be delivered.
      * An event dispatcher thread dequeues events from the queue and dispatches
      * them to the registered listeners.
      * Package private; used by NamingEventNotifier to fire events
      */
-    synchronized void queueEvent(EventObject event, Vector vector) {
+    synchronized void queueEvent(EventObject event,
+                                 Vector<? extends NamingListener> vector) {
         if (eventQueue == null)
             eventQueue = new EventQueue();
 
         /*
          * Copy the vector in order to freeze the state of the set

@@ -338,11 +332,13 @@
          * to delivery.  This ensures that any changes made to the
          * Vector from a target listener's method during the delivery
          * of this event will not take effect until after the event is
          * delivered.
          */
-        Vector v = (Vector)vector.clone();
+        @SuppressWarnings("unchecked")
+        Vector<NamingListener> v =
+                (Vector<NamingListener>)vector.clone();
         eventQueue.enqueue(event, v);
     }
 
     // No finalize() needed because EventSupport is always owned by
     // an LdapCtx. LdapCtx's finalize() and close() always call cleanup() so