Re: [Collections] UnmodifiableSet/UnmodifiableList
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 ReadOnlySet/UnmodifiableSet, and that will probably never happen. On Mar 24, 2017 8:57 AM, "Lukasz Lenart"wrote: > 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 > > private UnmodifiableSet excluded; > > > Regards > -- > Łukasz > + 48 606 323 122 http://www.lenart.org.pl/ > > - > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > For additional commands, e-mail: dev-h...@commons.apache.org > >
Re: [Collections] UnmodifiableSet/UnmodifiableList
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 private UnmodifiableSet excluded; Regards -- Łukasz + 48 606 323 122 http://www.lenart.org.pl/ - To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org
Re: [Collections] UnmodifiableSet/UnmodifiableList
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 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 us = (UnmodifiableSet) > UnmodifiableSet.unmodifiableSet(new java.util.HashSet()); 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-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org
Re: [Collections] UnmodifiableSet/UnmodifiableList
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 impossible to treat an UnmodifiableSet as a Set. The best solution is to document where you return an UnmodifiableSet versus a regular Set in your Javadocs. On Mar 23, 2017 08:35, "sebb"wrote: > 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 instance of the type UnmodifiableSet - there > > is no such constructor neither factory method :) > > > > You can create an instance: > > UnmodifiableSet us = (UnmodifiableSet) > UnmodifiableSet.unmodifiableSet(new java.util.HashSet()); > us.clear(); // The compiler is happy with this, but it fails at > runtime > > 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? > > > Regards > > -- > > Łukasz > > + 48 606 323 122 http://www.lenart.org.pl/ > > > > - > > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > > For additional commands, e-mail: dev-h...@commons.apache.org > > > > - > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > For additional commands, e-mail: dev-h...@commons.apache.org > >
Re: [Collections] UnmodifiableSet/UnmodifiableList
On 23 March 2017 at 15:02, Lukasz Lenartwrote: > 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 :) > You can create an instance: UnmodifiableSet us = (UnmodifiableSet) UnmodifiableSet.unmodifiableSet(new java.util.HashSet()); us.clear(); // The compiler is happy with this, but it fails at runtime 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? > Regards > -- > Łukasz > + 48 606 323 122 http://www.lenart.org.pl/ > > - > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > For additional commands, e-mail: dev-h...@commons.apache.org > - To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org
Re: [Collections] UnmodifiableSet/UnmodifiableList
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 :) Regards -- Łukasz + 48 606 323 122 http://www.lenart.org.pl/ - To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org
Re: [Collections] UnmodifiableSet/UnmodifiableList
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, sebbwrote: > 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 be straightforward that > > I expect the UnmodifiableSet and not just a Set. > > > > What about adding a method like in Guava? Something like this: > > > > public static UnmodifiableSet of(Set set) > > > > This allows define UnmodifiableSets in code and use compiler to check > > if everything is ok. > > Not sure I follow. > > What exactly can the compiler check? > > The API for UnmodifiableSet is basically the same as the API for Set. > > Yes, it implements Unmodifiable, but that is a Collections4 class and > means nothing to the compiler. > > It is only at run-time that the the classes throw an error for update > operations. > > > > > Regards > > -- > > Łukasz > > + 48 606 323 122 http://www.lenart.org.pl/ > > > > - > > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > > For additional commands, e-mail: dev-h...@commons.apache.org > > > > - > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > For additional commands, e-mail: dev-h...@commons.apache.org > > -- Matt Sicker
Re: [Collections] UnmodifiableSet/UnmodifiableList
On 23 March 2017 at 07:48, Lukasz Lenartwrote: > 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 be straightforward that > I expect the UnmodifiableSet and not just a Set. > > What about adding a method like in Guava? Something like this: > > public static UnmodifiableSet of(Set set) > > This allows define UnmodifiableSets in code and use compiler to check > if everything is ok. Not sure I follow. What exactly can the compiler check? The API for UnmodifiableSet is basically the same as the API for Set. Yes, it implements Unmodifiable, but that is a Collections4 class and means nothing to the compiler. It is only at run-time that the the classes throw an error for update operations. > > Regards > -- > Łukasz > + 48 606 323 122 http://www.lenart.org.pl/ > > - > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > For additional commands, e-mail: dev-h...@commons.apache.org > - To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org