Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6323374

(coll) Optimize Collections.unmodifiable* and synchronized*

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 6
    • Fix Version/s: 17
    • Component/s: core-libs
    • Labels:

      Description

      A DESCRIPTION OF THE REQUEST :
      When calling an unmodifiableMap (for example) it is sufficient that the function return an unmodifiable class. As there is no way to detremine if the class is already unmodifyable (with attempting to modify it) this method can be called just in case. This can result in an unmodifiable collection needless wrapped again and again.

      The same holds for synchronized collections. Once a class is wrapped in a synchronised class, it does not need to be wrapped again.

      JUSTIFICATION :
      It is quite simple for the methods to check is a map, list,set collection is already wrapped in an unmodifable or synchronized wrapper, but it is ugly to attempt to do this outside the class as the rwapping classes are all package protected.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Once a collection is wrapped with an unmodifiable wrapper, it should not be wrapped again if the method is called again.

      The same holds for synchronized wrappers.
      ACTUAL -
      A collection can be wrapped any number of times to make it very, very unmodifiable or very, very synchronized.

      ---------- BEGIN SOURCE ----------
      Here is an example diff for the synchronized methods.

      *** Collections.java Thu Sep 8 00:09:56 2005
      --- CollectionsNew.java Mon Sep 12 09:54:59 2005
      ***************
      *** 1529,1534 ****
      --- 1529,1540 ----
             * @return a synchronized view of the specified collection.
             */
            public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
      + Class<?> cClass = c.getClass();
      + if (cClass == SynchronizedCollection.class ||
      + cClass == SynchronizedList.class ||
      + cClass == SynchronizedSet.class ||
      + cClass == SynchronizedSortedSet.class )
      + return c;
         return new SynchronizedCollection<T>(c);
            }
        
      ***************
      *** 1633,1638 ****
      --- 1639,1647 ----
             * @return a synchronized view of the specified set.
             */
            public static <T> Set<T> synchronizedSet(Set<T> s) {
      + Class<?> sClass = s.getClass();
      + if (sClass == SynchronizedSet.class || sClass == SynchronizedSortedSet.class)
      + return s;
         return new SynchronizedSet<T>(s);
            }
        
      ***************
      *** 1701,1706 ****
      --- 1710,1717 ----
             * @return a synchronized view of the specified sorted set.
             */
            public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) {
      + if (s.getClass() == SynchronizedSortedSet.class)
      + return s;
         return new SynchronizedSortedSet<T>(s);
            }
        
      ***************
      *** 1779,1784 ****
      --- 1790,1799 ----
             * @return a synchronized view of the specified list.
             */
            public static <T> List<T> synchronizedList(List<T> list) {
      + Class<?> listClass = list.getClass();
      + if (listClass == SynchronizedRandomAccessList.class || listClass == SynchronizedList.class)
      + return list;
      +
         return (list instanceof RandomAccess ?
                        new SynchronizedRandomAccessList<T>(list) :
                        new SynchronizedList<T>(list));
      ***************
      *** 1937,1942 ****
      --- 1952,1960 ----
             * @return a synchronized view of the specified map.
             */
            public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
      + Class<?> mClass = m.getClass();
      + if (mClass == SynchronizedMap.class || mClass == SynchronizedSortedMap.class)
      + return m;
         return new SynchronizedMap<K,V>(m);
            }
        
      ***************
      *** 2077,2082 ****
      --- 2095,2102 ----
             * @return a synchronized view of the specified sorted map.
             */
            public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) {
      + if (m.getClass() == SynchronizedSortedMap.class)
      + return m;
         return new SynchronizedSortedMap<K,V>(m);
            }
        

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Create a utility class which determines the classes created by the various methods and then uses these to detremine if the wrappers are required.
      This is fairly ugly.

      OR Create a copy of the Collections class and correct the methods which can be optimised.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              igraves Ian Graves
              Reporter:
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: