![]() |
Re: EnumSet and varargs
Wojtek wrote:
>> The method add() in the EnumSet implementation ALWAYS throws an >> UnsupportedOperationException, so it cannot be used. Zig wrote: > This sounds inconsistent with my knowledge, have you tried this? The hoes say about EnumSet#add(): > This implementation always throws an UnsupportedOperationException. -- Lew - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - [terrorism, nazi, Zionism, fascism, NWO, war crimes, murder, ethnic cleansing, extermination, illuminati] Intelligence Briefs Ariel Sharon has endorsed the shooting of Palestinian children on the West Bank and Gaza. He did so during a visit earlier this week to an Israeli Defence Force base at Glilot, north of Tel Aviv. The base is a training camp for Israeli snipers. Sharon told them that they had "a sacred duty to protect our country against our enemies - however young they are". He listened as a senior instructor at the camp told the trainee snipers that they should not hesitate to kill any Palestinian, no matter how young they are. "If they can hold a weapon, they are a target", the instructor is quoted as saying. Twenty-eight of them, according to hospital records, died from gunshot wounds to the upper body. Over half of those died from single shots to the head. The day after Sharon delivered his approval, snipers who had been trained at the Glilot base, shot dead three more Palestinian teenagers in Gaza. One was only 15 years old. The killings have provoked increasing division within Israel itself. |
EnumSet and varargs
Given the following:
------------------ public class Foo { public enum Bar { ONE,TWO,THREE,FOUR; } private String myTitle; private EnumSet<Bar> myBars; public Foo( String title, Bar... bars) { super(); myTitle = title; myBars = EnumSet.<Bar> ??( bars ); } } ------------------ What method can I use in the place of the two question marks? How do I get "bars" into "myBars"? There is no method which ONLY takes a vararg as an argument. The closest is http://java.sun.com/j2se/1.5.0/docs/...html#of(E,%20E...) , but it needs an initial single parameter. null is not an option. The method add() in the EnumSet implementation ALWAYS throws an UnsupportedOperationException, so it cannot be used. Note: the "title" is useless here, but that is the signature, so I included it. -- Wojtek :-) |
Re: EnumSet and varargs
Wojtek wrote:
> What method can I use in the place of the two question marks? How do I > get "bars" into "myBars"? EnumSet.copyOf(Arrays.asList(bars)); That should do the trick. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth |
Re: EnumSet and varargs
In article <mn.83e77d89a7e2c7f2.70216@a.com>, Wojtek <nowhere@a.com>
wrote: [...] > What method can I use in the place of the two question marks? How do > I get "bars" into "myBars"? List<Bar> list = Arrays.asList(bars); myBars = EnumSet.<Bar>copyOf(list); -- John B. Matthews trashgod at gmail dot com home dot woh dot rr dot com slash jbmatthews |
Re: EnumSet and varargs
On Tue, 16 Sep 2008 23:39:51 GMT, Wojtek <nowhere@a.com> wrote, quoted
or indirectly quoted someone who said : >closest is >http://java.sun.com/j2se/1.5.0/docs/...html#of(E,%20E...) >, but it needs an initial single parameter. null is not an option. All those variants are just ways of handling a EnumSet with a small number of elements more efficiently. You are not supposed to look so closely, just provide 1 to n arguments separated by commas. The trouble comes when you want to feed it an array. They are just simulatining a single E... I suggest you consider copyOf( Arrays.asList( array ) ) ; -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com |
Re: EnumSet and varargs
Wojtek wrote:
> What method can I use in the place of the two question marks? How do I > get "bars" into "myBars"? > > There is no method which ONLY takes a vararg as an argument. The closest > is > http://java.sun.com/j2se/1.5.0/docs/...html#of(E,%20E...) > , but it needs an initial single parameter. null is not an option. > Well it's a set. So you can use some tricks. If you can guarantee at least one item in bars, you can use it. Otherwise, you have an empty set. if( bars.length > 0 ) myBars = EnumSet.of( bars[0], bars ); else myBars = EnumSet.noneOf( Bar.class ); The fact that you include the first element of bars twice doesn't matter, because it's a *set*. (I'm not sure of syntax of that last line, better check it.) Just being different from everyone else... |
Re: EnumSet and varargs
In article <rlo0d41ho6nt85m5jfjibuske8f5stl7hq@4ax.com>,
Roedy Green <see_website@mindprod.com.invalid> wrote: > On Tue, 16 Sep 2008 23:39:51 GMT, Wojtek <nowhere@a.com> wrote, quoted > or indirectly quoted someone who said : > > >closest is > >http://java.sun.com/j2se/1.5.0/docs/...html#of(E,%20E...) > >, but it needs an initial single parameter. null is not an option. > > All those variants are just ways of handling an EnumSet with a small > number of elements more efficiently. [...] Wojtek : In addition to the vararg constructor, consider offering an EnumSet constructor. This would allow clients to use EnumSet's set operations, e.g. union, intersection, complement, etc: <code> import java.util.*; public class EnumTest { enum Bar { ONE, TWO, THREE, FOUR; } public EnumTest(EnumSet<Bar> bars) { showBars(bars); } public EnumTest(Bar... bars) { List<Bar> barList = Arrays.asList(bars); EnumSet<Bar> barSet = EnumSet.copyOf(barList); showBars(barSet); } private static void showBars(EnumSet<Bar> bars) { for (Bar b : bars) System.out.print(b.name() + " "); System.out.println(); } public static void main(String[] args) { EnumTest test1 = new EnumTest( EnumSet.complementOf(EnumSet.of(Bar.THREE))); Bar[] bars = { Bar.ONE, Bar.TWO, Bar.FOUR }; EnumTest test2 = new EnumTest(bars); } } </code> -- John B. Matthews trashgod at gmail dot com home dot woh dot rr dot com slash jbmatthews |
Re: EnumSet and varargs
Zig wrote :
> On Tue, 16 Sep 2008 19:39:51 -0400, Wojtek <nowhere@a.com> wrote: > >> >> What method can I use in the place of the two question marks? How do I get >> "bars" into "myBars"? > > I think Joshua has answered your question on this... > >> The method add() in the EnumSet implementation ALWAYS throws an >> UnsupportedOperationException, so it cannot be used. > > This sounds inconsistent with my knowledge, have you tried this? Yes. At first glance it seemed the way to go. I had a loop which added the "bars" one by one. When I ran it, it threw that exception. I use Eclipse, and hovering over "add" showed a Javadoc which stated that the exception was always thrown. -- Wojtek :-) |
Re: EnumSet and varargs
Mark Space wrote :
> Wojtek wrote: > >> What method can I use in the place of the two question marks? How do I get >> "bars" into "myBars"? >> >> There is no method which ONLY takes a vararg as an argument. The closest is >> http://java.sun.com/j2se/1.5.0/docs/...html#of(E,%20E...) >> , but it needs an initial single parameter. null is not an option. >> > > Well it's a set. So you can use some tricks. If you can guarantee at least > one item in bars, you can use it. Otherwise, you have an empty set. > > if( bars.length > 0 ) > myBars = EnumSet.of( bars[0], bars ); > else > myBars = EnumSet.noneOf( Bar.class ); > > The fact that you include the first element of bars twice doesn't matter, > because it's a *set*. (I'm not sure of syntax of that last line, better > check it.) I had thought of this as I drove home (dang, I need a laptop in my car). > Just being different from everyone else... I like variations... -- Wojtek :-) |
Re: EnumSet and varargs
Zig wrote :
> On Wed, 17 Sep 2008 09:39:09 -0400, Wojtek <nowhere@a.com> wrote: > >>>> The method add() in the EnumSet implementation ALWAYS throws an >>>> UnsupportedOperationException, so it cannot be used. >>> >>> This sounds inconsistent with my knowledge, have you tried this? >> >> Yes. At first glance it seemed the way to go. I had a loop which added the >> "bars" one by one. When I ran it, it threw that exception. > > Interesting. This is my test: > > import java.util.*; > import java.util.concurrent.TimeUnit; > > public class EnumFiddling { > public static void main(String[] args) { > //empty collection > EnumSet<TimeUnit> units=EnumSet.noneOf(TimeUnit.class); <slaps forehead> I forgot to initialize the EnumSet :-( Ok, my only excuse is that it was the end of the day. And I am sticking to that story... > System.out.println(units); > > //add one element > units.add(TimeUnit.SECONDS); > System.out.println(units); > > //add a sequence of elements > Collections.addAll(units, > TimeUnit.MILLISECONDS, > TimeUnit.MICROSECONDS); > System.out.println(units); > } > } -- Wojtek :-) |
| All times are GMT. The time now is 08:34 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.