[groovy-user] How to use Enum with Range?

71 views
Skip to first unread message

Pierre Thibault

unread,
May 16, 2008, 4:20:31 PM5/16/08
to us...@groovy.codehaus.org
Hello,

I'am trying to use an Enum with a Range in Groovy to iterate of it. But I am unable to do so. Is it possible or am I force to use EnumSet instead?

Here is my sample code:

public enum Season {
    WINTER, SPRING, SUMMER, FALL;

    Season next() {
        Season[] vals = Season.values();
        if(ordinal() == vals.size()-1) throw new NoSuchElementException();
        return vals[(this.ordinal() + 1) % vals.length];
    }

    Season previous() {
        Season[] vals = Season.values();
        return vals[(this.ordinal() - 1 + vals.length) % vals.length];
    }
   
    int compareTo(Season s)
    {
        ordinal <=> s.ordinal()
    }
   
    boolean hasNext()
    {
        Season[] vals = Season.values();
        return ordinal() < vals.size()-1
    }
}



//(Season.WINTER..Season.FALL).each {  // This is not working
//    println it
//}

EnumSet.range(Season.WINTER, Season.FALL).each {  // This is working well
    println it
}

Regards.

Pierre



Ask a question on any topic and get answers from real people. Go to Yahoo! Answers.

Guillaume Laforge

unread,
May 16, 2008, 4:33:27 PM5/16/08
to us...@groovy.codehaus.org
I don't think you need to implement your own next() or compareTo().

enum Seasons { SPRING, SUMMER, FALL, WINTER }

println "All seasons"
Seasons.each { println it }

println "\nJust a few seasons"
(Seasons.SPRING..Seasons.FALL).each { println it }

--
Guillaume Laforge
Groovy Project Manager
G2One, Inc. Vice-President Technology
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Pierre Thibault

unread,
May 16, 2008, 4:51:29 PM5/16/08
to us...@groovy.codehaus.org
>----- Original Message ----
>From: Guillaume Laforge <glaf...@gmail.com>
>To: us...@groovy.codehaus.org
>Sent: Friday, May 16, 2008 4:33:27 PM
>Subject: Re: [groovy-user] How to use Enum with Range?
>
>I don't think you need to implement your own next() or compareTo().
>
>enum Seasons { SPRING, SUMMER, FALL, WINTER }
>
>println "All seasons"
>Seasons.each { println it }
>
>println "\nJust a few seasons"
>(Seasons.SPRING..Seasons.FALL).each { println it }

Sorry but this gives me:

All seasons
class Seasons

Just a few seasons
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: Seasons.next() is applicable for argument types: () values: {}
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:59)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:169)
    at Seasons.invokeMethod(Test.groovy)
    at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:777)
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:753)
    at groovy.lang.ObjectRange.increment(ObjectRange.java:376)
    at groovy.lang.ObjectRange.size(ObjectRange.java:276)
    at groovy.lang.ObjectRange$1.hasNext(ObjectRange.java:207)
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:989)
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:967)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:51)
    at org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod.invoke(NewInstanceMetaMethod.java:54)
    at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:599)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:904)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:740)
    at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:761)
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:749)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:167)
    at Test.run(Test.groovy:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)
    at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:599)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:904)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:740)
    at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:773)
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:753)
    at org.codehaus.groovy.runtime.InvokerHelper.runScript(InvokerHelper.java:402)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)
    at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:599)
    at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1077)
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:744)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:167)
    at Test.main(Test.groovy)


Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail

Pierre Thibault

unread,
May 16, 2008, 5:11:43 PM5/16/08
to us...@groovy.codehaus.org
OK,

I found that your code is working at the command line but not inside Eclipse. So the problem is with my environment. I am going to check this...

Thank you.


Looking for the perfect gift? Give the gift of Flickr!

Guillaume Laforge

unread,
May 16, 2008, 5:43:21 PM5/16/08
to us...@groovy.codehaus.org
Hmm, alright, that must be a problem of Groovy version you're using.
There were some issues in the past with ranges of enums, but it's been
fixed pretty recently.

Pierre Thibault

unread,
May 16, 2008, 7:01:18 PM5/16/08
to us...@groovy.codehaus.org
I am unable to run this script using the Groovy Eclipse plugin. I have this problem specifically with this script. All my other scripts are working well. Moving the code in another working project does not help and give the same error.

I am now running the script in Eclipse as a external tool calling the groovy binary directly.

Could someone else try the script of Guillaume to run it in Eclipse using the plugin, please?

---------------
Pierre

Ray Tayek

unread,
May 16, 2008, 9:01:11 PM5/16/08
to us...@groovy.codehaus.org
At 04:01 PM 5/16/2008, you wrote:
>I am unable to run this script using the Groovy Eclipse plugin. I
>have this problem specifically with this script. All my other
>scripts are working well. Moving the code in another working project
>does not help and give the same error.
>
>I am now running the script in Eclipse as a external tool calling
>the groovy binary directly.
>
>Could someone else try the script of Guillaume to run it in Eclipse
>using the plugin, please?

this works for me from the command line with groovy 1.5.5, but fails
in eclipse which has the plugin
org.codehaus.groovy.eclipse.ui_1.5.1.200801232227.jar. i get the: "
... No signature of method: Seasons.next() is applicable for argument
types: () values: {} ... "

thanks

> <<mailto:pthib...@yahoo.ca>pthib...@yahoo.ca>

> > <http://www.g2one.com>http://www.g2one.com


> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >

> <http://xircles.codehaus.org/manage_email>http://xircles.codehaus.org/manage_email


> >
> >
> >
> > ________________________________
> > Be smarter than spam. See how smart SpamGuard is at giving junk email the
> > boot with the All-new Yahoo! Mail
> > ________________________________
> > Looking for the perfect gift? Give the gift of Flickr!
>
>
>
>--
>Guillaume Laforge
>Groovy Project Manager
>G2One, Inc. Vice-President Technology

><http://www.g2one.com>http://www.g2one.com


>
>---------------------------------------------------------------------
>To unsubscribe from this list, please visit:
>
>

><http://xircles.codehaus.org/manage_email>http://xircles.codehaus.org/manage_email


>
>
>
>
>Be smarter than spam. See how smart SpamGuard is at giving junk
>email the boot with the

><http://ca.promos.yahoo.com/newmail/overview2/>All-new Yahoo! Mail

---
vice-chair http://ocjug.org/

Pierre Thibault

unread,
May 16, 2008, 10:14:34 PM5/16/08
to us...@groovy.codehaus.org
OK,

So to use the plugin I have to build from the svn version to be up to date? It would be nice to synchronize the release of the plugin with the release of Groovy.

A+

Pierre Thibault

unread,
May 17, 2008, 4:43:36 PM5/17/08
to us...@groovy.codehaus.org
Hello,

I made an update by building the Groovy Eclipse from the svn version. Now things are working just fine. :-)

---------------
Pierre

abraham.m...@wipro.com

unread,
May 20, 2008, 9:40:50 AM5/20/08
to us...@groovy.codehaus.org
Hi

I have a map that takes a string as key and an ArrayList as value. The
arraylist contains a list of strings.

if(documentMap."$theWord")
{
(documentMap."$theWord").add(processedLine)
}
else
{
ArrayList textList = new ArrayList();
textList.add(processedLine);
documentMap.put(theWord,textList);
}

The q is, can I do it in one line? At least the else part?

Something like
documentMap[theWord] = (documentMap.get[theWord,new
ArrayList().add(processedLine)])

Where the "get" method will put in a "default" value for the ArrayList
the very first time and next time onwards it will just add values to the
arraylist.

Thanks,
Abraham.

Please do not print this email unless it is absolutely necessary.

The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.

WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.

www.wipro.com

Jochen Theodorou

unread,
May 20, 2008, 9:51:46 AM5/20/08
to us...@groovy.codehaus.org
abraham.m...@wipro.com schrieb:

> Hi
>
> I have a map that takes a string as key and an ArrayList as value. The
> arraylist contains a list of strings.
>
> if(documentMap."$theWord")
> {
> (documentMap."$theWord").add(processedLine)
> }
> else
> {
> ArrayList textList = new ArrayList();
> textList.add(processedLine);
> documentMap.put(theWord,textList);
> }
>
> The q is, can I do it in one line? At least the else part?
>
> Something like
> documentMap[theWord] = (documentMap.get[theWord,new
> ArrayList().add(processedLine)])
>
> Where the "get" method will put in a "default" value for the ArrayList
> the very first time and next time onwards it will just add values to the
> arraylist.

I have a method like this... and it is called get ;)

documentMap.get(theWord,[processedLine])

a few comments:
* [processedLine] will create an ArrayList and add the claues in []
* documentMap."$theWord" will use a GString to lookup the key in the
map, but GStrings are not totally equal to String, better don't use them
as key for storing or getting values.

bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

abraham.m...@wipro.com

unread,
May 20, 2008, 10:39:14 AM5/20/08
to us...@groovy.codehaus.org
Blackdrag,

Thank You! This works beautifully.

Now my code reads like
documentMap[theWord] ? (documentMap.get(theWord)).add(processedLine) :
documentMap.get(theWord,[processedLine])

With no messy array initializations!! And its all in one line...:D

Thanks,
Abraham

documentMap.get(theWord,[processedLine])

bye blackdrag

http://xircles.codehaus.org/manage_email

Please do not print this email unless it is absolutely necessary.

The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.

WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.

www.wipro.com

---------------------------------------------------------------------

Reply all
Reply to author
Forward
0 new messages