src/share/classes/com/sun/jndi/toolkit/dir/HierMemDirCtx.java

Print this page

        

@@ -40,12 +40,12 @@
 public class HierMemDirCtx implements DirContext {
 
     static private final boolean debug = false;
     private static final NameParser defaultParser = new HierarchicalNameParser();
 
-    protected Hashtable myEnv;
-    protected Hashtable bindings;
+    protected Hashtable<String, Object> myEnv;
+    protected Hashtable<Name, Object> bindings;
     protected Attributes attrs;
     protected boolean ignoreCase = false;
     protected NamingException readOnlyEx = null;
     protected NameParser myParser = defaultParser;
 

@@ -68,25 +68,25 @@
 
     public HierMemDirCtx(boolean ignoreCase) {
         this(null, ignoreCase, false);
     }
 
-    public HierMemDirCtx(Hashtable environment, boolean ignoreCase) {
+    public HierMemDirCtx(Hashtable<String, Object> environment, boolean ignoreCase) {
         this(environment, ignoreCase, false);
     }
 
-    protected HierMemDirCtx(Hashtable environment, boolean ignoreCase,
-        boolean useFac) {
+    protected HierMemDirCtx(Hashtable<String, Object> environment,
+        boolean ignoreCase, boolean useFac) {
         myEnv = environment;
         this.ignoreCase = ignoreCase;
         init();
         this.alwaysUseFactory = useFac;
     }
 
     private void init() {
         attrs = new BasicAttributes(ignoreCase);
-        bindings = new Hashtable(11, 0.75f);
+        bindings = new Hashtable<>(11, 0.75f);
     }
 
     public Object lookup(String name) throws NamingException {
         return lookup(myParser.parse(name));
     }

@@ -324,34 +324,34 @@
         }
 
         bindings.put(newname, oldBinding);
     }
 
-    public NamingEnumeration list(String name) throws NamingException {
+    public NamingEnumeration<NameClassPair> list(String name) throws NamingException {
         return list(myParser.parse(name));
     }
 
-    public NamingEnumeration list(Name name) throws NamingException {
+    public NamingEnumeration<NameClassPair> list(Name name) throws NamingException {
         HierMemDirCtx ctx = (HierMemDirCtx) doLookup(name, false);
         return ctx.doList();
     }
 
-    protected NamingEnumeration doList () throws NamingException {
+    protected NamingEnumeration<NameClassPair> doList () throws NamingException {
         return new FlatNames(bindings.keys());
     }
 
 
-    public NamingEnumeration listBindings(String name) throws NamingException {
+    public NamingEnumeration<Binding> listBindings(String name) throws NamingException {
         return listBindings(myParser.parse(name));
     }
 
-    public NamingEnumeration listBindings(Name name) throws NamingException {
+    public NamingEnumeration<Binding> listBindings(Name name) throws NamingException {
         HierMemDirCtx ctx = (HierMemDirCtx)doLookup(name, false);
         return ctx.doListBindings(alwaysUseFactory);
     }
 
-    protected NamingEnumeration doListBindings(boolean useFactory)
+    protected NamingEnumeration<Binding> doListBindings(boolean useFactory)
         throws NamingException {
         return new FlatBindings(bindings, myEnv, useFactory);
     }
 
     public void destroySubcontext(String name) throws NamingException {

@@ -445,32 +445,36 @@
         Name result = (Name)(prefix.clone());
         result.addAll(name);
         return result;
     }
 
+    @SuppressWarnings("unchecked") // clone()
     public Object addToEnvironment(String propName, Object propVal)
             throws NamingException {
-        myEnv = (myEnv == null) ?
-            new Hashtable(11, 0.75f) : (Hashtable)myEnv.clone();
+        myEnv = (myEnv == null)
+                ? new Hashtable<String, Object>(11, 0.75f)
+                : (Hashtable<String, Object>)myEnv.clone();
 
         return myEnv.put(propName, propVal);
     }
 
+    @SuppressWarnings("unchecked") // clone()
     public Object removeFromEnvironment(String propName)
             throws NamingException {
         if (myEnv == null)
             return null;
 
-        myEnv = (Hashtable)myEnv.clone();
+        myEnv = (Hashtable<String, Object>)myEnv.clone();
         return myEnv.remove(propName);
     }
 
-    public Hashtable getEnvironment() throws NamingException {
+    @SuppressWarnings("unchecked") // clone()
+    public Hashtable<String, Object> getEnvironment() throws NamingException {
         if (myEnv == null) {
-            return new Hashtable(5, 0.75f);
+            return new Hashtable<>(5, 0.75f);
         } else {
-            return (Hashtable)myEnv.clone();
+            return (Hashtable<String, Object>)myEnv.clone();
         }
     }
 
     public Attributes getAttributes(String name)
        throws NamingException {

@@ -527,14 +531,14 @@
             throw new IllegalArgumentException(
                 "Cannot modify without an attribute");
         }
 
         // turn it into a modification Enumeration and pass it on
-        NamingEnumeration attrEnum = attrs.getAll();
+        NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll();
         ModificationItem[] mods = new ModificationItem[attrs.size()];
         for (int i = 0; i < mods.length && attrEnum.hasMoreElements(); i++) {
-            mods[i] = new ModificationItem(mod_op, (Attribute)attrEnum.next());
+            mods[i] = new ModificationItem(mod_op, attrEnum.next());
         }
 
         modifyAttributes(name, mods);
     }
 

@@ -562,11 +566,11 @@
     protected static Attributes applyMods(ModificationItem[] mods,
         Attributes orig) throws NamingException {
 
         ModificationItem mod;
         Attribute existingAttr, modAttr;
-        NamingEnumeration modVals;
+        NamingEnumeration<?> modVals;
 
         for (int i = 0; i < mods.length; i++) {
             mod = mods[i];
             modAttr = mod.getAttribute();
 

@@ -617,31 +621,31 @@
         }
 
         return orig;
     }
 
-    public NamingEnumeration search(String name,
+    public NamingEnumeration<SearchResult> search(String name,
                                     Attributes matchingAttributes)
         throws NamingException {
         return search(name, matchingAttributes, null);
     }
 
-    public NamingEnumeration search(Name name,
+    public NamingEnumeration<SearchResult> search(Name name,
                                     Attributes matchingAttributes)
         throws NamingException {
             return search(name, matchingAttributes, null);
     }
 
-     public NamingEnumeration search(String name,
+     public NamingEnumeration<SearchResult> search(String name,
                                     Attributes matchingAttributes,
                                     String[] attributesToReturn)
         throws NamingException {
         return search(myParser.parse(name), matchingAttributes,
             attributesToReturn);
     }
 
-     public NamingEnumeration search(Name name,
+     public NamingEnumeration<SearchResult> search(Name name,
                                     Attributes matchingAttributes,
                                     String[] attributesToReturn)
          throws NamingException {
 
         HierMemDirCtx target = (HierMemDirCtx) doLookup(name, false);

@@ -654,11 +658,11 @@
             new ContainmentFilter(matchingAttributes),
             cons, this, myEnv,
             false); // alwaysUseFactory ignored because objReturnFlag == false
     }
 
-    public NamingEnumeration search(Name name,
+    public NamingEnumeration<SearchResult> search(Name name,
                                     String filter,
                                     SearchControls cons)
         throws NamingException {
         DirContext target = (DirContext) doLookup(name, false);
 

@@ -669,28 +673,28 @@
                 SearchControls.ONELEVEL_SCOPE),
             stringfilter,
             cons, this, myEnv, alwaysUseFactory);
     }
 
-     public NamingEnumeration search(Name name,
+     public NamingEnumeration<SearchResult> search(Name name,
                                     String filterExpr,
                                     Object[] filterArgs,
                                     SearchControls cons)
             throws NamingException {
 
         String strfilter = SearchFilter.format(filterExpr, filterArgs);
         return search(name, strfilter, cons);
     }
 
-    public NamingEnumeration search(String name,
+    public NamingEnumeration<SearchResult> search(String name,
                                     String filter,
                                     SearchControls cons)
         throws NamingException {
         return search(myParser.parse(name), filter, cons);
     }
 
-    public NamingEnumeration search(String name,
+    public NamingEnumeration<SearchResult> search(String name,
                                     String filterExpr,
                                     Object[] filterArgs,
                                     SearchControls cons)
             throws NamingException {
         return search(myParser.parse(name), filterExpr, filterArgs, cons);

@@ -759,15 +763,18 @@
 
     public void setNameParser(NameParser parser) {
         myParser = parser;
     }
 
-    // Class for enumerating name/class pairs
-    private class FlatNames implements NamingEnumeration {
-        Enumeration names;
+    /*
+     * Common base class for FlatNames and FlatBindings.
+     * Inheritors can implement different NamingEnumeration<T> interfaces.
+     */
+    private class BaseFlatNames {
+        Enumeration<Name> names;
 
-        FlatNames (Enumeration names) {
+        BaseFlatNames (Enumeration<Name> names) {
             this.names = names;
         }
 
         public boolean hasMoreElements() {
             try {

@@ -779,44 +786,55 @@
 
         public boolean hasMore() throws NamingException {
             return names.hasMoreElements();
         }
 
-        public Object nextElement() {
+        public NameClassPair nextElement() {
             try {
                 return next();
             } catch (NamingException e) {
                 throw new NoSuchElementException(e.toString());
             }
         }
 
-        public Object next() throws NamingException {
-            Name name = (Name)names.nextElement();
+        public NameClassPair next() throws NamingException {
+            Name name = names.nextElement();
             String className = bindings.get(name).getClass().getName();
             return new NameClassPair(name.toString(), className);
         }
 
         public void close() {
             names = null;
         }
     }
 
+    // Class for enumerating name/class pairs
+    private class FlatNames extends BaseFlatNames
+                            implements NamingEnumeration<NameClassPair> {
+        FlatNames (Enumeration<Name> names) {
+            super(names);
+        }
+    }
+
    // Class for enumerating bindings
-    private final class FlatBindings extends FlatNames {
-        private Hashtable bds;
-        private Hashtable env;
+    private final class FlatBindings extends BaseFlatNames
+                                     implements NamingEnumeration<Binding> {
+        private Hashtable<Name, Object> bds;
+        private Hashtable<String, Object> env;
         private boolean useFactory;
 
-        FlatBindings(Hashtable bindings, Hashtable env, boolean useFactory) {
+        FlatBindings(Hashtable<Name, Object> bindings,
+                     Hashtable<String, Object> env,
+                     boolean useFactory) {
             super(bindings.keys());
             this.env = env;
             this.bds = bindings;
             this.useFactory = useFactory;
         }
 
-        public Object next() throws NamingException {
-            Name name = (Name)names.nextElement();
+        public Binding next() throws NamingException {
+            Name name = names.nextElement();
 
             HierMemDirCtx obj = (HierMemDirCtx)bds.get(name);
 
             Object answer = obj;
             if (useFactory) {

@@ -834,10 +852,18 @@
                 }
             }
 
             return new Binding(name.toString(), answer);
         }
+        
+        public Binding nextElement() {
+            try {
+                return next();
+            } catch (NamingException e) {
+                throw new NoSuchElementException(e.toString());
+            }
+        }
     }
 
     public class HierContextEnumerator extends ContextEnumerator {
         public HierContextEnumerator(Context context, int scope)
             throws NamingException {

@@ -847,11 +873,11 @@
         protected HierContextEnumerator(Context context, int scope,
             String contextName, boolean returnSelf) throws NamingException {
             super(context, scope, contextName, returnSelf);
         }
 
-        protected NamingEnumeration getImmediateChildren(Context ctx)
+        protected NamingEnumeration<Binding> getImmediateChildren(Context ctx)
             throws NamingException {
                 return ((HierMemDirCtx)ctx).doListBindings(false);
         }
 
         protected ContextEnumerator newEnumerator(Context ctx, int scope,

@@ -870,18 +896,18 @@
 final class HierarchicalName extends CompoundName {
     private int hashValue = -1;
 
     // Creates an empty name
     HierarchicalName() {
-        super(new Enumeration() {
+        super(new Enumeration<String>() {
             public boolean hasMoreElements() {return false;}
-            public Object nextElement() {throw new NoSuchElementException();}
+                  public String nextElement() {throw new NoSuchElementException();}
         },
             HierarchicalNameParser.mySyntax);
     }
 
-    HierarchicalName(Enumeration comps, Properties syntax) {
+    HierarchicalName(Enumeration<String> comps, Properties syntax) {
         super(comps, syntax);
     }
 
     HierarchicalName(String n, Properties syntax) throws InvalidNameException {
         super(n, syntax);

@@ -905,16 +931,16 @@
 
         return hashValue;
     }
 
     public Name getPrefix(int posn) {
-        Enumeration comps = super.getPrefix(posn).getAll();
+        Enumeration<String> comps = super.getPrefix(posn).getAll();
         return (new HierarchicalName(comps, mySyntax));
     }
 
     public Name getSuffix(int posn) {
-        Enumeration comps = super.getSuffix(posn).getAll();
+        Enumeration<String> comps = super.getSuffix(posn).getAll();
         return (new HierarchicalName(comps, mySyntax));
     }
 
     public Object clone() {
         return (new HierarchicalName(getAll(), mySyntax));