src/share/classes/com/sun/jndi/ldap/pool/Pool.java

Print this page

        

@@ -28,11 +28,10 @@
 import java.util.Map;
 import java.util.WeakHashMap;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
-import java.util.Set;
 import java.util.LinkedList;
 
 import java.io.PrintStream;
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;

@@ -81,21 +80,22 @@
     static final boolean debug = com.sun.jndi.ldap.LdapPoolManager.debug;
 
     /*
      * Used for connections cleanup
      */
-    private static final ReferenceQueue queue = new ReferenceQueue();
-    private static final Collection weakRefs =
-                Collections.synchronizedList(new LinkedList());
+    private static final ReferenceQueue<ConnectionsRef> queue =
+        new ReferenceQueue<>();
+    private static final Collection<Reference<ConnectionsRef>> weakRefs =
+        Collections.synchronizedList(new LinkedList<Reference<ConnectionsRef>>());
 
     final private int maxSize;    // max num of identical conn per pool
     final private int prefSize;   // preferred num of identical conn per pool
     final private int initSize;   // initial number of identical conn to create
-    final private Map map;
+    final private Map<Object, ConnectionsRef> map;
 
     public Pool(int initSize, int prefSize, int maxSize) {
-        map = new WeakHashMap();
+        map = new WeakHashMap<>();
         this.prefSize = prefSize;
         this.maxSize = maxSize;
         this.initSize = initSize;
     }
 

@@ -133,11 +133,12 @@
                     factory);
                 ConnectionsRef connsRef = new ConnectionsRef(conns);
                 map.put(id, connsRef);
 
                 // Create a weak reference to ConnectionsRef
-                Reference weakRef = new ConnectionsWeakRef(connsRef, queue);
+                Reference<ConnectionsRef> weakRef =
+                        new ConnectionsWeakRef(connsRef, queue);
 
                 // Keep the weak reference through the element of a linked list
                 weakRefs.add(weakRef);
             }
         }

@@ -146,11 +147,11 @@
 
         return conns.get(timeout, factory); // get one connection from list
     }
 
     private Connections getConnections(Object id) {
-        ConnectionsRef ref = (ConnectionsRef) map.get(id);
+        ConnectionsRef ref = map.get(id);
         return (ref != null) ? ref.getConnections() : null;
     }
 
     /**
      * Goes through the connections in this Pool and expires ones that

@@ -161,15 +162,14 @@
      * @param threshold connections idle before 'threshold' should be closed
      *          and removed.
      */
     public void expire(long threshold) {
         synchronized (map) {
-            Collection coll = map.values();
-            Iterator iter = coll.iterator();
+            Iterator<ConnectionsRef> iter = map.values().iterator();
             Connections conns;
             while (iter.hasNext()) {
-                conns = ((ConnectionsRef) (iter.next())).getConnections();
+                conns = iter.next().getConnections();
                 if (conns.expire(threshold)) {
                     d("expire(): removing ", conns);
                     iter.remove();
                 }
             }

@@ -200,27 +200,22 @@
          }
     }
 
 
     public void showStats(PrintStream out) {
-        Map.Entry entry;
         Object id;
         Connections conns;
 
         out.println("===== Pool start ======================");
         out.println("maximum pool size: " + maxSize);
         out.println("preferred pool size: " + prefSize);
         out.println("initial pool size: " + initSize);
         out.println("current pool size: " + map.size());
 
-        Set entries = map.entrySet();
-        Iterator iter = entries.iterator();
-
-        while (iter.hasNext()) {
-            entry = (Map.Entry) iter.next();
+        for (Map.Entry<Object, ConnectionsRef> entry : map.entrySet()) {
             id = entry.getKey();
-            conns = ((ConnectionsRef) entry.getValue()).getConnections();
+            conns = entry.getValue().getConnections();
             out.println("   " + id + ":" + conns.getStats());
         }
 
         out.println("====== Pool end =====================");
     }