Groovy Explore custom range one by one simple custom range classes

Source: Internet
Author: User
Tags comparable implement range tostring

This will be a series of text that expands our approach to the use of custom range classes. Perhaps we unknowingly use the range class in the groovy language, not even aware of it, or we often marvel at the convenience of using the range class in the coding process of the groovy language, but just marvel that we're not paying for it. Or we want to use the custom range class in the actual encoding, but we can't find the right place to use it. And so on, all of this, hopefully after reading our series, we'll be able to use the range class passively and turn it into the active use of our custom range class.

An important concept introduced by the groovy language is range. With range, we can write code that looks like this:

(1..10).each{
print it
}
println ''

The results of the operation are:

12345678910

We can find range in the code of Groovy language everywhere. It is very convenient to use, here we do not say more.

The groovy language introduces the concept of range to us, and there is an innovation in that we can customize range. Can customize the range class, we are certainly welcome, because when we initialize a collection class object, we use a list or an array, we need to instantiate all of their elements and put them in the container, and when we use range, we need only instantiate two elements, You can get to the collection class object. Of course it was tremendously convenient for us.

To say no, let's take a look at an example of a custom range class first.

This example says that there are five departments in a department and we need to traverse all sections.

To customize a range class, the first thing we do is let the class implement the "comparable" interface, and then implement the "Next", "previous", and "CompareTo" methods in that class. Of course, these methods are used by the range class object to traverse, not implemented.

Now let's look at our custom department code:

class Section implements Comparable{
def SECTION_TYPE = ['Yike','Erke','Sanke','Sike','Wuke']
private index = 0
public Section(type)
{
this.index = SECTION_TYPE.indexOf(type)
}
Section next()
{
new Section(SECTION_TYPE[(index+1)%SECTION_TYPE.size()])
}
Section previous()
{
new Section(index-1)
}
int compareTo(Object other)
{
index<=>other.index
}
public String toString()
{
SECTION_TYPE[index]
}

}

The entire class is also very simple, "section_type" is used to define the code for the section so that the Range class object can automatically instantiate a department.

The "index" variable is used to determine the position at the time of traversal.

The "Next" method is used to iterate backwards, and the "previous" method is used to iterate forward, and the "CompareTo" method is used to compare the position of two elements. These are the methods that the range class must implement.

In all custom range classes, their implementation logic is similar.

and "ToString" method is the section of the method of their own, of course, we can define a number of sections in the Department class to use the other methods, in this case, for the sake of simplicity, no longer define other methods.

Next, we can test this custom range class:

def yike = new Section('Yike')
def wuke = new Section('Wuke')
(yike..wuke).each{
println it.toString()
}

The results of the run are:

Yike
Erke
Sanke
Sike
Wuke

Let's go back to the test code, in the code, we only initialized two department objects, as follows:

def yike = new Section('Yike')
def wuke = new Section('Wuke')

You can iterate over five objects. If we use the list object, then the code will look like this:

def list = [new Section('Yike'),
new Section('Erke'),
new Section('Sanke'),
new Section('Sike'),
new Section('Wuke')]
list.each{
println it.toString()
}

This simple example, of course, initially shows the superiority of the custom range class, but the custom range class is never so simple, and it has a much broader arena.

This will be the next part of this series of text that is going to be said.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.