Details

    • Type: Sub-task
    • Status: In Progress
    • Priority: P3
    • Resolution: Unresolved
    • Affects Version/s: 10
    • Fix Version/s: 10
    • Component/s: core-libs
    • Labels:

      Description

      Probably something like this on List:

          static <T> List<T> copyOf(Collection<? extends T> coll)

      This would return an unmodifiable List. There would be similar factory methods for Set and Map. This would be implemented the obvious way for most cases.

      In addition, this could check the implementation of the collection being copied from, and if it's a class that's known to be immutable, it would simply return that reference.

      This would be a shallow copy.

      This would also allow variant aliasing of collections, which generally isn't permitted. For example,

          List<String> list = List.of(...);
          List<CharSequence> newList = List.copyOf(list);

      This can't be done for mutable collections, since it can lead to heap pollution. Since these implementations are unmodifiable, this should be safe.

      Short-circuiting of copying is one of the oft-cited use cases for exposing immutable collection types. It's also one of the reasons people sometimes ask for a test method that returns whether the collection is or isn't unmodifiable. The copy factories could help mitigate some of the demand for those mechanisms.

        Issue Links

          Activity

          There are no comments yet on this issue.

            People

            • Assignee:
              smarks Stuart Marks
              Reporter:
              smarks Stuart Marks
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: