> e.g. because they're going to use "for (element : collection)" style
>> e.g. because they're going to use "for (element : collection)" style
> iteration anyway>
>
> That construct is overloaded for array types also.
Yes. Hence the "because", meaning that "the fact that this construct is
overloaded" is the cause of "the client code doesn't care whether it's an
array or a collection
that they receive on the other end".
> Just wanted to make sure there wasn't a good reason to stick with arrays.
> Having not seen so much less 1.5 code than what went before, it just seems
> a
> bit odd passing around Collections.
If you don't want the client code to resize the return value, using an
array might be a good idea. If you want the return value to be completely
immutable though, it's probably better to return your own implementation of
Collection which doesn't implement any of the add/remove or other mutator
methods.
Which one is better to use, like any design choice, depends on the
context.
- Oliver
Thomas G. Marshall - 16 Nov 2005 04:59 GMT
Oliver Wong coughed up:
...[rip]...
> If you want the return value to be completely
> immutable though, it's probably better to return your own implementation
> of
> Collection which doesn't implement any of the add/remove or other mutator
> methods.
Tough to do cleanly, since the mutators are part of the Collection
interface. You could always break the OO rule and implement them, but have
them punt or do nothing. Squashing part of a superclass's (or java
interface's) interface is a no-no, but done from time to time anyway.
...[rip]...

Signature
Sometimes life just sucks and then you live.
Asbjørn "L. Johansen" - 16 Nov 2005 11:03 GMT
> Oliver Wong coughed up:
> > If you want the return value to be completely
[quoted text clipped - 7 lines]
> them punt or do nothing. Squashing part of a superclass's (or java
> interface's) interface is a no-no, but done from time to time anyway.
These operations are marked as optional in the API, and if you read the
docs you will see that a Collection can throw an
UnsupportedOperationException from these methods.
--
Asbjrn L. Johansen
asbjorjo@stud.ntnu.no
Thomas G. Marshall - 16 Nov 2005 20:30 GMT
Asbjørn L. Johansen coughed up:
>> Oliver Wong coughed up:
>>> If you want the return value to be completely
[quoted text clipped - 12 lines]
> the docs you will see that a Collection can throw an
> UnsupportedOperationException from these methods.
Fair enough, mea culpa. I had forgotten that entirely, thanks.
Chris Smith - 21 Nov 2005 16:36 GMT
> If you want the return value to be completely
> immutable though, it's probably better to return your own implementation of
> Collection which doesn't implement any of the add/remove or other mutator
> methods.
Eh? Why not use Collections.unmodifiableList(...)?

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Oliver Wong - 21 Nov 2005 16:49 GMT
>> If you want the return value to be completely
>> immutable though, it's probably better to return your own implementation
[quoted text clipped - 3 lines]
>
> Eh? Why not use Collections.unmodifiableList(...)?
'Cause I couldn't find it... While working on some code, I had to return
an unmodifiable list, and I was pretty sure I had seen an API call to do
just that, but I couldn't the call in the JavaDocs so I ended up writing my
own. Anyway, thanks for showing me where it is.
- Oliver
Thomas G. Marshall - 21 Nov 2005 21:01 GMT
Chris Smith said something like:
>> If you want the return value to be completely
>> immutable though, it's probably better to return your own implementation
[quoted text clipped - 3 lines]
>
> Eh? Why not use Collections.unmodifiableList(...)?
Probably because he forgot it......like I did. {embarrassed}

Signature
Onedoctortoanother:"Ifthisismyrectalthermometer,wherethehell'smypen???"