If Read-only set/UnmodifiableSet doesn't implement the Set interface, you
won't be able to use it for functions with arguments of type Set. You won't
be able to cast back to a Set because a ReadOnlySet isn't a type of Set.
The only way this would work is if java.util.Set extended
2017-03-23 16:55 GMT+01:00 Javen O'Neal :
> The best solution is to document where you return an UnmodifiableSet versus
> a regular Set in your Javadocs.
Disagree, compare these two examples:
/**
* An immutable set, do not try to modify it
*/
private Set excluded;
vs
2017-03-23 16:35 GMT+01:00 sebb :
> On 23 March 2017 at 15:02, Lukasz Lenart wrote:
>> 2017-03-23 14:37 GMT+01:00 sebb :
>>> Not sure I follow.
>>>
>>> What exactly can the compiler check?
>>
>> You can declare a variable or a field of
Because either (1) UnmodifiableSet implents the Set interface and throws an
Invalid operations working at runtime for methods that would modify the set
or (2) UnmodifiableSet does not implement the full Set interface, making it
possible to catch errors at compile time, but also making it
On 23 March 2017 at 15:02, Lukasz Lenart wrote:
> 2017-03-23 14:37 GMT+01:00 sebb :
>> 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
2017-03-23 14:37 GMT+01:00 sebb :
> 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
I think the idea is giving a subset of the Set methods that are read-only.
Any write operations wouldn't be available on the interface, so you
couldn't compile it.
On 23 March 2017 at 08:37, sebb wrote:
> On 23 March 2017 at 07:48, Lukasz Lenart
On 23 March 2017 at 07:48, Lukasz Lenart wrote:
> Hi,
>
> I see that the Collections4 provides those classes. Anyway I wonder
> why I the constructor is private and why the factory method
> unmodifiableSet() returns Set?
>
> I would love to use those classes directly, to