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

(coll) Check before wrapping at Collections.synchronized* and Collections.unmodifiable*

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P5
    • Resolution: Duplicate
    • Affects Version/s: 6
    • Fix Version/s: tbd_minor
    • Component/s: core-libs
    • Labels:

      Description

      A DESCRIPTION OF THE REQUEST :
      Current implementation of java.util.Collections.synchronizedList (Map, Set) as well as unmodifiableList (Map, Set) blindly wrap given collections into synchronized/unmodifiable counterparts without checking, if, for example, an unmodifiable map is already passed to Collections.unmodifiableMap() as an incoming parameter.


      JUSTIFICATION :
      There can be plenty of situations (e.g. injection) when code does not know what underlying types of collections it operates with. When implementing, for example, immutable behavior, Collections.unmodifiable* methods come in very handy. However, creation of unnecessary objects (see Description) costs extra memory and CPU. And it doesn't seem that it would affect any of existing behavior.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Quick solution:
      If given object is already an instance of java.util.Collections.SynchronizedMap(Set, List), quickly return whatever was passed to that method. Similarly for the unmodifiable counterparts.

      Proper solution:
      Provide a marker interface so that incoming collections could be checked against being instance of synchronized/unmodifiable map (set, list) but not necessarily being a derivative of inner Synchronized* or Unmodifiable* classes of java.util.Collections.
      ACTUAL -
      The incoming collections are blindly wrapped into unmodifiable/synchronized counterparts (e.g. see public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) method of java.util.Collections class).

      CUSTOMER SUBMITTED WORKAROUND :
      When the collections are not injected, a flag needs to be kept outside collection to know whether it was made unmodifiable/synchronized or not.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: