groovy datatypes - ranges

Upload: fasih-khatib

Post on 02-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Groovy Datatypes - Ranges

    1/4

    This post will introduce you to the concept of Ranges in Groovy. The best way to introduce it is with a simple script:

    [code language="groovy"]def range = 0..10for(each in range){ println each}[/code]The output for the above script is:012345678910

    It was fairly intuitive what the output was going to be, right? That is the whole point of ranges - they make your code more expressive. You could have done the same with a traditional for(init;condition;increment/decrement) loop but that isn't all that expressive. It would look as follows:[code language="java"]for(int i = 0; i

  • 8/10/2019 Groovy Datatypes - Ranges

    2/4

    def range = 0..10

    // 1.assert range.contains(0)assert range.contains(5)assert range.contains(10)

    // 2.range = 0..

  • 8/10/2019 Groovy Datatypes - Ranges

    3/4

    [/code]

    // 1. shows that the ranges are inclusive unless explicitly stated otherwise. // 2. shows a half-inclusive range in which teh range does not contain 10. // 3. shows that ranges can also be created from their explicit constructors. // 4. shows how a range can be used for dates. Using ranges for dates is possible because Groovy has added the previous() and next() methods to date to make them usable in ranges. // 5. the same applies for strings. // 6. shows using an inclusive, reverse range in a for-each loop. // 7. shows using a half-exclusive, reverse range in a for-each loop. // 8. shows how ranges can be used as cases in a switch statement. // 9. shows collections can be filtered by grep() by using ranges. // 10. shows using a closure with a range's each() method.

    And finally, here is how you can ensure that your class can be used in ranges:

    [code language="groovy"]// Example taken from the book 'Groovy In Action'class Day implements Comparable{ static final DAYS = ['Sat','Sun','Mon','Tue','Wed','Thu','Fri'] private int index = 0

    Day(String day){ index = DAYS.indexOf(day) }

    // 1. Day next(){ return new Day(DAYS[(index+1) % DAYS.size()])

    } // 2.

    Day previous(){ return new Day(DAYS[index-1]) }

    // 3. int compareTo(Object other){ return this.index other.index }

    // 4.

    String toString(){ return DAYS[index] }}

    // 5.def mon = new Day('Mon')def fri = new Day('Fri')def worklog = ''for (day in mon..fri){

  • 8/10/2019 Groovy Datatypes - Ranges

    4/4

    worklog += day.toString() + ' '}assert worklog == 'Mon Tue Wed Thu Fri '[/code]

    To use your class in ranges, two things need to be done:

    Your class provides an implementation for next() and previous() , that is ++ and -- operators.

    Your class implements the java.lang.Comparable interface thus overriding the (spaceship) operator

    Let's go over the script. // 1. shows what happens when you ++ . Just so that we do not get an ArrayIndexOutOfBoundsException , we go back to the start of the list if the index becomes greater than the size. Actually, I amwrong to say this because you can never get that exception when using lists in Groovy. Lists are a topic of future posts. // 2. shows what happens when you -- . The implementation is different from that of ++ because Groovy allows negative indexing. DAYS[-1] would mean the last element of the list. // 3. shows how you would compare two Day objects

    - on the basis of the index value they hold. // 4. is just the string representation of the DAY object. // 5. just puts it all in action.

    That's all about ranges. Next post will introduce lists.