src/share/classes/javax/imageio/metadata/IIOMetadataFormatImpl.java

Print this page
rev 9344 : 8034998: Fix raw and unchecked lint warnings in javax.imageio
Reviewed-by: darcy, prr

@@ -88,31 +88,31 @@
     private String resourceBaseName = this.getClass().getName() + "Resources";
 
     private String rootName;
 
     // Element name (String) -> Element
-    private HashMap elementMap = new HashMap();
+    private HashMap<String, Element> elementMap = new HashMap<>();
 
     class Element {
         String elementName;
 
         int childPolicy;
         int minChildren = 0;
         int maxChildren = 0;
 
         // Child names (Strings)
-        List childList = new ArrayList();
+        List<String> childList = new ArrayList<>();
 
         // Parent names (Strings)
-        List parentList = new ArrayList();
+        List<String> parentList = new ArrayList<>();
 
         // List of attribute names in the order they were added
-        List attrList = new ArrayList();
+        List<String> attrList = new ArrayList<>();
         // Attr name (String) -> Attribute
-        Map attrMap = new HashMap();
+        Map<String, Attribute> attrMap = new HashMap<>();
 
-        ObjectValue objectValue;
+        ObjectValue<?> objectValue;
     }
 
     class Attribute {
         String attrName;
 

@@ -120,32 +120,33 @@
         int dataType;
         boolean required;
         String defaultValue = null;
 
         // enumeration
-        List enumeratedValues;
+        List<String> enumeratedValues;
 
         // range
         String minValue;
         String maxValue;
 
         // list
         int listMinLength;
         int listMaxLength;
     }
 
-    class ObjectValue {
+    class ObjectValue<T> {
         int valueType = VALUE_NONE;
-        Class classType = null;
-        Object defaultValue = null;
+        // ? extends T So that ObjectValue<Object> can take Class<?>
+        Class<? extends T> classType = null;
+        T defaultValue = null;
 
         // Meaningful only if valueType == VALUE_ENUMERATION
-        List enumeratedValues = null;
+        List<? extends T> enumeratedValues = null;
 
         // Meaningful only if valueType == VALUE_RANGE
-        Comparable minValue = null;
-        Comparable maxValue = null;
+        Comparable<? super T> minValue = null;
+        Comparable<? super T> maxValue = null;
 
         // Meaningful only if valueType == VALUE_LIST
         int arrayMinLength = 0;
         int arrayMaxLength = 0;
     }

@@ -270,11 +271,11 @@
      */
     private Element getElement(String elementName, boolean mustAppear) {
         if (mustAppear && (elementName == null)) {
             throw new IllegalArgumentException("element name is null!");
         }
-        Element element = (Element)elementMap.get(elementName);
+        Element element = elementMap.get(elementName);
         if (mustAppear && (element == null)) {
             throw new IllegalArgumentException("No such element: " +
                                                elementName);
         }
         return element;

@@ -285,11 +286,11 @@
     }
 
     // Utility method for locating an attribute
     private Attribute getAttribute(String elementName, String attrName) {
         Element element = getElement(elementName);
-        Attribute attr = (Attribute)element.attrMap.get(attrName);
+        Attribute attr = element.attrMap.get(attrName);
         if (attr == null) {
             throw new IllegalArgumentException("No such attribute \"" +
                                                attrName + "\"!");
         }
         return attr;

@@ -406,13 +407,13 @@
      * @param elementName the name of the element to be removed.
      */
     protected void removeElement(String elementName) {
         Element element = getElement(elementName, false);
         if (element != null) {
-            Iterator iter = element.parentList.iterator();
+            Iterator<String> iter = element.parentList.iterator();
             while (iter.hasNext()) {
-                String parentName = (String)iter.next();
+                String parentName = iter.next();
                 Element parent = getElement(parentName, false);
                 if (parent != null) {
                     parent.childList.remove(elementName);
                 }
             }

@@ -512,11 +513,11 @@
             throw new IllegalArgumentException("enumeratedValues == null!");
         }
         if (enumeratedValues.size() == 0) {
             throw new IllegalArgumentException("enumeratedValues is empty!");
         }
-        Iterator iter = enumeratedValues.iterator();
+        Iterator<String> iter = enumeratedValues.iterator();
         while (iter.hasNext()) {
             Object o = iter.next();
             if (o == null) {
                 throw new IllegalArgumentException
                     ("enumeratedValues contains a null!");

@@ -679,11 +680,11 @@
      */
     protected void addBooleanAttribute(String elementName,
                                        String attrName,
                                        boolean hasDefaultValue,
                                        boolean defaultValue) {
-        List values = new ArrayList();
+        List<String> values = new ArrayList<>();
         values.add("TRUE");
         values.add("FALSE");
 
         String dval = null;
         if (hasDefaultValue) {

@@ -738,11 +739,11 @@
                                       Class<T> classType,
                                       boolean required,
                                       T defaultValue)
     {
         Element element = getElement(elementName);
-        ObjectValue obj = new ObjectValue();
+        ObjectValue<T> obj = new ObjectValue<>();
         obj.valueType = VALUE_ARBITRARY;
         obj.classType = classType;
         obj.defaultValue = defaultValue;
 
         element.objectValue = obj;

@@ -791,22 +792,22 @@
             throw new IllegalArgumentException("enumeratedValues == null!");
         }
         if (enumeratedValues.size() == 0) {
             throw new IllegalArgumentException("enumeratedValues is empty!");
         }
-        Iterator iter = enumeratedValues.iterator();
+        Iterator<? extends T> iter = enumeratedValues.iterator();
         while (iter.hasNext()) {
             Object o = iter.next();
             if (o == null) {
                 throw new IllegalArgumentException("enumeratedValues contains a null!");
             }
             if (!classType.isInstance(o)) {
                 throw new IllegalArgumentException("enumeratedValues contains a value not of class classType!");
             }
         }
 
-        ObjectValue obj = new ObjectValue();
+        ObjectValue<T> obj = new ObjectValue<>();
         obj.valueType = VALUE_ENUMERATION;
         obj.classType = classType;
         obj.defaultValue = defaultValue;
         obj.enumeratedValues = enumeratedValues;
 

@@ -852,11 +853,11 @@
                        Comparable<? super T> maxValue,
                        boolean minInclusive,
                        boolean maxInclusive)
     {
         Element element = getElement(elementName);
-        ObjectValue obj = new ObjectValue();
+        ObjectValue<T> obj = new ObjectValue<>();
         obj.valueType = VALUE_RANGE;
         if (minInclusive) {
             obj.valueType |= VALUE_RANGE_MIN_INCLUSIVE_MASK;
         }
         if (maxInclusive) {

@@ -893,11 +894,11 @@
     protected void addObjectValue(String elementName,
                                   Class<?> classType,
                                   int arrayMinLength,
                                   int arrayMaxLength) {
         Element element = getElement(elementName);
-        ObjectValue obj = new ObjectValue();
+        ObjectValue<Object> obj = new ObjectValue<>();
         obj.valueType = VALUE_LIST;
         obj.classType = classType;
         obj.arrayMinLength = arrayMinLength;
         obj.arrayMaxLength = arrayMaxLength;
 

@@ -960,14 +961,14 @@
          * accessed via the applet class loader. So first try the context
          * class loader to locate the resource bundle.
          * If that throws MissingResourceException, then try the
          * system class loader.
          */
-        ClassLoader loader = (ClassLoader)
+        ClassLoader loader =
             java.security.AccessController.doPrivileged(
-                new java.security.PrivilegedAction() {
-                   public Object run() {
+                new java.security.PrivilegedAction<ClassLoader>() {
+                   public ClassLoader run() {
                        return Thread.currentThread().getContextClassLoader();
                    }
             });
 
         ResourceBundle bundle = null;

@@ -1035,21 +1036,21 @@
     public String[] getChildNames(String elementName) {
         Element element = getElement(elementName);
         if (element.childPolicy == CHILD_POLICY_EMPTY) {
             return null;
         }
-        return (String[])element.childList.toArray(new String[0]);
+        return element.childList.toArray(new String[0]);
     }
 
     // Attributes
 
     public String[] getAttributeNames(String elementName) {
         Element element = getElement(elementName);
-        List names = element.attrList;
+        List<String> names = element.attrList;
 
         String[] result = new String[names.size()];
-        return (String[])names.toArray(result);
+        return names.toArray(result);
     }
 
     public int getAttributeValueType(String elementName, String attrName) {
         Attribute attr = getAttribute(elementName, attrName);
         return attr.valueType;

@@ -1077,14 +1078,13 @@
         if (attr.valueType != VALUE_ENUMERATION) {
             throw new IllegalArgumentException
                 ("Attribute not an enumeration!");
         }
 
-        List values = attr.enumeratedValues;
-        Iterator iter = values.iterator();
+        List<String> values = attr.enumeratedValues;
         String[] result = new String[values.size()];
-        return (String[])values.toArray(result);
+        return values.toArray(result);
     }
 
     public String getAttributeMinValue(String elementName, String attrName) {
         Attribute attr = getAttribute(elementName, attrName);
         if (attr.valueType != VALUE_RANGE &&

@@ -1168,84 +1168,84 @@
                                           Locale locale) {
         Element element = getElement(elementName);
         if (attrName == null) {
             throw new IllegalArgumentException("attrName == null!");
         }
-        Attribute attr = (Attribute)element.attrMap.get(attrName);
+        Attribute attr = element.attrMap.get(attrName);
         if (attr == null) {
             throw new IllegalArgumentException("No such attribute!");
         }
 
         String key = elementName + "/" + attrName;
         return getResource(key, locale);
     }
 
-    private ObjectValue getObjectValue(String elementName) {
+    private ObjectValue<?> getObjectValue(String elementName) {
         Element element = getElement(elementName);
-        ObjectValue objv = element.objectValue;
+        ObjectValue<?> objv = element.objectValue;
         if (objv == null) {
             throw new IllegalArgumentException("No object within element " +
                                                elementName + "!");
         }
         return objv;
     }
 
     public int getObjectValueType(String elementName) {
         Element element = getElement(elementName);
-        ObjectValue objv = element.objectValue;
+        ObjectValue<?> objv = element.objectValue;
         if (objv == null) {
             return VALUE_NONE;
         }
         return objv.valueType;
     }
 
     public Class<?> getObjectClass(String elementName) {
-        ObjectValue objv = getObjectValue(elementName);
+        ObjectValue<?> objv = getObjectValue(elementName);
         return objv.classType;
     }
 
     public Object getObjectDefaultValue(String elementName) {
-        ObjectValue objv = getObjectValue(elementName);
+        ObjectValue<?> objv = getObjectValue(elementName);
         return objv.defaultValue;
     }
 
     public Object[] getObjectEnumerations(String elementName) {
-        ObjectValue objv = getObjectValue(elementName);
+        ObjectValue<?> objv = getObjectValue(elementName);
         if (objv.valueType != VALUE_ENUMERATION) {
             throw new IllegalArgumentException("Not an enumeration!");
         }
-        List vlist = objv.enumeratedValues;
+        List<?> vlist = objv.enumeratedValues;
         Object[] values = new Object[vlist.size()];
         return vlist.toArray(values);
     }
 
     public Comparable<?> getObjectMinValue(String elementName) {
-        ObjectValue objv = getObjectValue(elementName);
+        ObjectValue<?> objv = getObjectValue(elementName);
         if ((objv.valueType & VALUE_RANGE) != VALUE_RANGE) {
             throw new IllegalArgumentException("Not a range!");
         }
         return objv.minValue;
     }
 
     public Comparable<?> getObjectMaxValue(String elementName) {
-        ObjectValue objv = getObjectValue(elementName);
+        ObjectValue<?> objv = getObjectValue(elementName);
         if ((objv.valueType & VALUE_RANGE) != VALUE_RANGE) {
             throw new IllegalArgumentException("Not a range!");
         }
         return objv.maxValue;
     }
 
     public int getObjectArrayMinLength(String elementName) {
-        ObjectValue objv = getObjectValue(elementName);
+        ObjectValue<?> objv = getObjectValue(elementName);
         if (objv.valueType != VALUE_LIST) {
             throw new IllegalArgumentException("Not a list!");
         }
         return objv.arrayMinLength;
     }
 
     public int getObjectArrayMaxLength(String elementName) {
-        ObjectValue objv = getObjectValue(elementName);
+        ObjectValue<?> objv = getObjectValue(elementName);
         if (objv.valueType != VALUE_LIST) {
             throw new IllegalArgumentException("Not a list!");
         }
         return objv.arrayMaxLength;
     }