commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lukasz Lenart <lukaszlen...@apache.org>
Subject Re: [Collections] UnmodifiableSet/UnmodifiableList
Date Fri, 24 Mar 2017 15:54:07 GMT
2017-03-23 16:35 GMT+01:00 sebb <sebbaz@gmail.com>:
> On 23 March 2017 at 15:02, Lukasz Lenart <lukaszlenart@apache.org> wrote:
>> 2017-03-23 14:37 GMT+01:00 sebb <sebbaz@gmail.com>:
>>> Not sure I follow.
>>>
>>> What exactly can the compiler check?
>>
>> You can declare a variable or a field of type UnmodifiableSet but
>> there is no way to create instance of the type UnmodifiableSet - there
>> is no such constructor neither factory method :)
>>
>
> You can create an instance:
>
>         UnmodifiableSet<String> us = (UnmodifiableSet<String>)
> UnmodifiableSet.unmodifiableSet(new java.util.HashSet<String>());

Hmm... it is a solution ...

>         us.clear(); // The compiler is happy with this, but it fails at runtime

I don't care, internal code won't use it, if someone will try call
clear() it will be they problem ;-)

> AFAICT the only way the compiler can check is to create a class or
> interface that does not have the update methods.
>
> One could potentially create a ReadOnlySet interface that is
> implemented by UnmodifiableSet.
> Similarly for the other unmodifiable Collections.
>
> However would it be worth it?

I think it's worth do such thing, ReadOnlySet speaks better than any JavaDoc :)


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message