Java: Does Collections.unmodifiableXYZ (...) make a collection object fly in special cases?

Possible answers are never or , it depends .

Personally, I would say it depends .

The following usage will make the collection appear (for me) like flies:

public final static List<Integer> SOME_LIST = 
  Collections.unmodifiableList(
    new LinkedList<Integer>(){ // scope begins
      {
        add(1);
        add(2);
        add(3);
      }
    } // scope ends
  );

      

Right? You cannot ever change it, because that is the only place where the "original" collection object is known (that can be changed) is the scope inside the unmodifiableList parameter list, which ends immediately.

Second, when you retrieve an item from the list, it is Integer , which is itself a fly.

Other obvious cases where the final static and unmodifiable list is not used are not considered a fly.

Did I miss something?
Should I consider some internal aspects of LinkedList that could be a fly compromise?

+1


a source to share


4 answers


I think you mean flies. The basic idea behind this pattern is that you are dealing with complex objects whose instances can be reused and exposed to different views using their methods.

for such an object to work correctly, it must be immutable.

immutability is explicitly given when creating the list as you described. but since there is no external object / parameters on which SOME_LISt runs, I would not call this a fly example.

another typical property of the pattern fly is the "internment" of such objects. when creating only one instance of an object, it doesn't make sense.

if you are dealing with lists that are passed from one object to another and you want to enforce immutability, using Google-Collections might be your best bet .



final static ImmutableList<Integer> someList = ImmutableList.of(1, 2, 3);

      

of course, it is also possible to build more complex Immutable objects with Builders.

this creates an instance of an immutable list. it will still implement the List interface, but will refuse to do any add (), addAll () set (), remove () operations.

so you can still pass it to methods when the List interface is required, but make sure its contents are not changed.

+3


a source


I think your example for immutable objects, flyweight is something else entirely. Immutable objects are candidates for flies, but flies don't have to be immutable, it just needs to be designed to save memory.



+1


a source


The presence of the library reveals that the mutable was List

not otherwise escaped, this is a bit of a question, although theoretically possible.

If you serialize the returned object, then trusted code can look at the internal object. Although the serialized form of the class is documented, it has not documented that the method uses these classes.

In practical terms, any cache is not available to the API user.

(Why LinkedList

for an immutable list, btw? Besides that, the unmodifiable implementation changes.)

+1


a source


An integer is just flies from -128 to 127.

See also http://www.javaworld.com/javaworld/jw-07-2003/jw-0725-designpatterns.html .

0


a source







All Articles