Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Custom Ranges



We can define a range of custom objects as well. An object must be comparable and it is good to implement next() method so that Groovy knows how to progress from one object to another object.

In order a create a custom object which can be used in a Range, we should implement Comparable interface. As Comparable interface provides compareTo() method which assists in defining the natural ordering of objects. Groovy needs object to have next() method as well so that next object can be determined. Let's create a object step by step which can be used in a range.

Create Version Class implementing Comparable Interface

class Version implements Comparable<Version> {
   int major;
   int minor;
   int patch;

   Version(int major, int minor, int patch) {
      this.major = major;
      this.minor = minor;
      this.patch = patch;
   }

   @Override
   int compareTo(Version other) {
      if (major != other.major) {
         return major <=> other.major; 
      if (minor != other.minor) {
         return minor <=> other.minor;
      }
      return patch <=> other.patch;
   }
}

Here

  • We've defined a class Version which is implementing the Comparable interface.

  • Version has three properties, to define a version as major.minor.patch

  • In compareTo() method implementation, we're first comparing the major and then minor and then patch.

  • compareTo() method returns zero, if objects are same, returns negative integer if object is lesser that other object and a positive integer if object is greater than other object compared.

Implement next() method to assist progresson

Version next() {
   return new Version(major, minor, patch + 1);
}

Here we're implementing next version object as a patch release incremented by 1.

Implement toString() method to print object

String toString() {
   return "${major}.${minor}.${patch}";
}

Here we're printing a version as major.minor.patch

Example - Creating a Range of Custom Object, Version

Example.groovy

class Example {
   static void main(String[] args){

      // Create instances of the Version class
      def v100 = new Version(1, 0, 0);
      def v105 = new Version(1, 0, 5);

      // Create range of version objects
      def versionRange = v100..v105;

      // print the list of version objects
      println versionRange.toList();

      // iterate through version objects
      for (version in versionRange) {
         println "Version: $version"
      }
   }
}

class Version implements Comparable<Version> {
   int major;
   int minor;
   int patch;

   Version(int major, int minor, int patch) {
      this.major = major;
      this.minor = minor;
      this.patch = patch;
   }

   @Override
   int compareTo(Version other) {
      if (major != other.major) {
         return major <=> other.major; 
      }
      if (minor != other.minor) {
         return minor <=> other.minor;
      }
      return patch <=> other.patch;
   }
   
   Version next() {
      return new Version(major, minor, patch + 1);
   }   
   
   String toString() {
      return "${major}.${minor}.${patch}";
   }
}

Output

When we run the above program, we will get the following result −

[1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5]
Version: 1.0.0
Version: 1.0.1
Version: 1.0.2
Version: 1.0.3
Version: 1.0.4
Version: 1.0.5
groovy_ranges.htm
Advertisements