List.asImmutable returns a Collection

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

List.asImmutable returns a Collection

Edward Povazan
Hello,

I have this (running a 2 day old head build)

The script:
println([].class)
println([].asImmutable().class)
println(((List)[]).asImmutable().class)
println([].asSynchronized().class)

The output:
class java.util.ArrayList
class java.util.Collections$UnmodifiableCollection
class java.util.Collections$SynchronizedCollection
class java.util.Collections$UnmodifiableCollection

According to the GDK docs (and logic), these should be List instances, not
collections right? Somehow the correct overload is not being resolved.
Can anyone confirm this so I can file an issue.

Thanks
-Ed
tog
Reply | Threaded
Open this post in threaded view
|

Re: List.asImmutable returns a Collection

tog
Well this is the expected behaviours as these are wrapping Collections static methods.
If you want an immutable list the quick (and dirty ?) hack is to have:

def a = [1, 2, 3]
List b = Collections.nCopies(1, a)
b.add(4) // throws java.lang.UnsupportedOperationException
println(b.class) // class java.util.Collections$CopiesList

Then the next question is about modifying the current behavior of DGM ?

cheers
tog

On 3/29/06, Edward Povazan <[hidden email]> wrote:
Hello,

I have this (running a 2 day old head build)

The script:
println([].class)
println([].asImmutable().class)
println(((List)[]).asImmutable().class)
println([].asSynchronized().class)

The output:
class java.util.ArrayList
class java.util.Collections$UnmodifiableCollection
class java.util.Collections$SynchronizedCollection
class java.util.Collections$UnmodifiableCollection

According to the GDK docs (and logic), these should be List instances, not
collections right? Somehow the correct overload is not being resolved.
Can anyone confirm this so I can file an issue.

Thanks
-Ed

Reply | Threaded
Open this post in threaded view
|

Re: List.asImmutable returns a Collection

Edward Povazan


tog wrote:
> Well this is the expected behaviours as these are wrapping Collections
> static methods.
How so? From DefaultGroovyMethods.java
public static List asImmutable(List self) {
   return Collections.unmodifiableList(self);
}
which returns a List.
And calling Collections.unmodifiableList([]) return a list too.

Unless I completely misunderstood you (time to sleep anyways!)

-Ed
tog
Reply | Threaded
Open this post in threaded view
|

Re: List.asImmutable returns a Collection

tog
Oops sorry I missed that point ...
Did you get some other answer on this point, I guess you should get back an
UnmodifiableRandomAccessList



On 3/29/06, Edward Povazan <[hidden email]> wrote:


tog wrote:
> Well this is the expected behaviours as these are wrapping Collections
> static methods.
How so? From DefaultGroovyMethods.java
public static List asImmutable(List self) {
   return Collections.unmodifiableList(self);
}
which returns a List.
And calling Collections.unmodifiableList([]) return a list too.

Unless I completely misunderstood you (time to sleep anyways!)

-Ed