Friday, August 7, 2020

Adding Range Type in Java

Oracle Java Exam Prep, Oracle Java Tutorial and Material, Java Certification, Oracle Java Guides

1. Introduction


Modern and more recent programming languages have the concept of a range type. Programming languages such as Python, Ada, Ruby, and Rust, each have the concept of a range-a bounded beginning and end of a numerical sequence or series of integers.

The Java programming language has a reputation for being a verbose, wordy programming language [Pras 2012] by nature. But yet, there is no range type in the Java language. This is a deficiency in the Java language, but using existing Java language features the range type is easily added into the programming language.

1.1 Python Programming Language

A range example in the Python programming language [PYna 2020] is:

#!/usr/bin/env python
#
range0to9 = range(0, 10)

for n in range0to9:
  print(n)

for n in range(0,10):
  print(n)

1.2 Ada Programming Language

A range example in the Ada programming language [Wiki 2017] is:

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
  type Index0to9 is range 0..9;
  package Index_IO is new Integer_IO (Index0to9);

begin

  for I in 0..9 loop
    Put(Integer'Image (I));
    New_Line;
  end loop;

  for I in Index0to9 loop
    Index_IO.Put(I);
    New_Line;
  end loop;

end Main;

1.3 Rust Programming Language

One of the newer, much discussed programming languages, Rust has a range type. Consider the adapted Rust source code [Mozi 2019] of:

fn main() {
  for number in 1..10 {
    println!("{}", number);
  }
}

1.4 Ruby Programming Language

An example in the Ruby [Ruby 2020] programming language using a range is:

#
#!/usr/bin/ruby
#
range0to9 = 0..9

for i in range0to9 do
  puts i
end

puts "\n"

for i in 0..9 do
  puts i
end

puts "\n"

val = 7

if (0..9) === val
  puts "In the range"
else
  puts "Out of range"
end

puts "\n"

if range0to9.include?(val)
  puts "In the range"
else
  puts "Out of range"
end

1.5 Advantages of the Range Type

All four example do the same thing, and illustrate the syntax for a range type in each programming language. The advantage of a range in a programming language is a more compact, and concise for expressiveness in comparison to alternatives that are an archaic syntax form. Without a range type, a programming language can be more verbose in writing functionality, but not have the clarity of expression from the range type feature.

2. Range Type for Java


Adding a range type for Java is possible in two ways:

1. Extend the Java language with a non-standard syntax and keywords.
2. Use the Java language with a class and features to add the feature.

2.1 Extending the Java Language

One approach to add a range type is to add it directly into the programming language. The problem is that this way adds non-standard syntax, and keywords. This would require a new compiler or new transcompiler in order to use the range type. Also as stated, this approach is non-standard, so as Java continues to develop and evolve, the new range type could become incompatible, or obsolete.

This approach, while feasible, is not an effective method to add a range type. Basically it is “embrace and extend” but the extension might become incompatible, and requires a proprietary compiler.

2.2 Use the Java Language

A more effective and less proprietary approach is to use the Java programming language to add a feature to the Java language. This adds a range type, but also does not require a new compiler to use, and risk future incompatible syntax and keywords.

Using the Java programming language, there are two approaches in terms of syntax:

1. Use static methods accessed through the class.
2. Use static methods along with the static import feature of Java.

The static import feature was added to Java 5 in 2004 [Orac 2004] so using that feature requires Java version 5 or later.

2.2.1 Implementation Approach

For the feature the Iterator interface of Java can be used for creation a virtual data structure—a Range type. However this Range type is not an actual data structure with a list of integers, but a virtual data structure. An example of the syntax used with a for-each loop similar to the Python and Ada examples is:

for(Integer n : Range.of(0,10)){
  System.out.print(n);
  System.out.println();
}//end for

As the syntax demonstrates, the Range class with the “of()” that performs like an iterator in Java, and implements a Java iterator.

2.2.2 Using an Iterator

An iterator is a classic design pattern from the “Gang of Four” [Gamm 1994] design patterns book. Java [ZetC 2020] introduced a language standard iterator in Java 1.2 in December 1998 used with the Java Collections. As of Java 5, objects implementing the Iterable interface, which returns an Iterator instances from its only method, can be traversed using the Java for-each loop syntax.

The Iterator brings the Iterator design pattern, which is a common behavioral pattern used to access the elements of a collection object in sequential manner without any need to know its underlying representation.

The Java iterator is an interface with four methods, but only two methods: “hasNext()” and “next()” are used for the Range type. For the Range type the method “iterator()” is of significance as it creates the iterator over the range of values from a lower and upper boundary.

3. Implementation of Range


The implementation of a Range type in Java requires two classes:

1. Range – the class for creating a range and getting an iterator through the range type.
2. RangeIterator – the class that implements the Iterator interface and the methods required.

The Range class is the virtual data structure, and the RangeIterator class is to traverse the Range data structure.

3.1 Range Class

The Range class is the “virtual” data structure of a list of integers from the start until the close of a range. The Range class creates a range and gets the iterator for a given range. Static factory methods create an iterator that is used to traverse through the “virtual” data structure list of integers.

The Range class is organized around three areas:

1. Class attributes and constructors to initialize those attributes.
2. Class iterator method and factory methods to create a range type.
3. Class accessor methods to get class attributes.

3.1.1. Range Class Attributes and Constructors

The Range class attributes and constructors are:

public final class Range implements Iterable<Integer> {

  private final int begin;
  private final int close;
  private final int index;

  public Range(final int begin, final int close) {
    this(begin, close, 1);
  }//end constructor

  public Range(final int begin, final int close, final int index) {
    this.begin = begin;
    this.close = close;
    this.index = index;
  }//end constructor

  //...

}//end class Range

3.1.2 Range Class Iterator Method and Factory Methods

The Range class iterator method and factor methods are:

public final class Range implements Iterable<Integer> {

  //...

  public static Range of(final int begin, final int close) {
    return new Range(begin, close);
  }//end of

  public static Range of(final int begin, final int close, final int index) {
    return new Range(begin, close, index);
  }//end of

  public static Range to(final int begin, final int close) {
    return new Range(begin, close+1);
  }//end to

  public static Range to(final int begin, final int close, final int index) {
     return new Range(begin, close+1, index);
  }//end to

  public boolean in(final int value) {
    //...
  }//end of

  public Iterator<Integer> iterator() {
    return new RangeIterator<>(this);
  }//end iterator

}//end class Range


3.1.3 Range Class Accessor Methods

The Range class accessor methods for the beginning and ending of the range, and the index are:

public final class Range implements Iterable<Integer> { 
 
  //...
 
  public int getBegin() { 
    return this.begin;
  }//end getBegin
 
  public int getClose() { 
    return this.close;
  }//end getClose
 
  public int getIndex() { 
    return this.index;
  }//end getIndex
 
}//end class Range

3.2 Range Methods

The three core methods of a range type in the Range class are:

1. of – is to create the range from an inclusive lower bound to an exclusive upper bound.
2. to – is to create the range from an inclusive lower bound to an inclusive upper bound.
3. in – is to verify if inside the range from the lower bound to the upper bound inclusively.

3.2.1 Range of() method

The Range.of() method creates a range that begins at the lower bound, and closes at the upper bound, but while the iterator value is lesser than…thus the upper bound is exclusive. The Range type source code, and the more basic for-loop equivalent is:

for(Integer idx : Range.of(0,10)){ 
  //...
}//end for
 
for(int idx=0; idx<10; idx++){ 
  //...
}//end for

3.2.2 Range to() Method

The Range.to() method creates a range that begins at the lower bound, and closes at the upper bound, but while the iterator value is equal than…thus the upper bound is inclusive. The Range type source code, and the more basic for-loop equivalent is:

for(Integer idx : Range.to(0,10)){ 
  //...
}//end for
 
for(int idx=0; idx <= 10; idx++){ 
  //...
}//end for

3.2.3 Range in() Method

The Range “in()” method verifies a value is within the range boundaries for a given range type.

3.2.3.1 Implementation of Range “in()” Method

The “in()” method is implemented in two steps:

1. Check the value is within the lower and upper bounds of the range.

2. If within the range boundaries, check the modulus is zero from the index—so that the value is a multiple within the range.

The source code to implement the “in()” method is more explicit:

public final class Range implements Iterable<Integer> { 
 
  //...
 
  public boolean in(final int val) {
 
    if(val < this.begin || val > this.close) return false; //val within range return (((val - this.begin) % this.index) == 0); //val reachable by index
  }//end in
 
  //...
 
}//end class Range

3.2.3.2 Using the Range “in()” Method

The Range type source code, and the more basic if-then equivalent is for the range “of()” method:

if(Range.of(0,10).in(x)){ 
  //...
}//end if
 
if(x <=0 &amp;&amp; x < 10){ 
  //...
}//end if

The Range type source code, and the more basic if-then equivalent is for the range “to()” method:

if(Range.to(0,10).in(x)){ 
  //...
}//end if
 
if(x <=0 &amp;&amp; x <= 10){ 
  //...
}//end if

3.3 Range Iterator Class

The RangeIterator is the class that implements the iterator and returns integers at each step in the process of iterating through the “virtual” data structure of a Range. The RangeIterator class in Java is defined as:

public final class RangeIterator<T> implements Iterator<T> {
 
  private int idx = -1; //increment index 
  private int pos = 0;  //position in range
 
  private int begin = -1; //begin value of range
  private int close = -1;//close value of range
 
  public RangeIterator(final Range range){
    this.begin = range.getBegin();
    this.close = range.getClose();
    this.idx.  = range.getIndex();
  }//end constructor
 
  public boolean hasNext(){
    return (this.begin + this.pos) < this.close;
  }//end hasNext
 
  public T next(){ 
    if(!this.hasNext()) {
      throw new NoSuchElementException("No further values in range."); 
    }//end if
 
    @SuppressWarnings("unchecked")
    T val = (T) Integer.valueOf(this.begin + this.pos); 
    this.pos = this.pos + this.idx;
    return val;
  }//end next
 
  public void remove(){
    throw new UnsupportedOperationException("Remove is not supported.");
  }//end remove
 
}//end class RangeIterator

The RangeIterator is simple, implementing the necessary functions to iterate over the range. The function “remove()” is not implemented, as there is no actual datum to remove.

3.4 Range using Static Import

In Java, the static import concept is introduced in 1.5 version. Using static import, we can access the static members of a class directly without class name or any object. This can allow more compact and concise access using a static method of the Range class for the range, rather than the class name and static method.

3.4.1 Static Import

Static import allows static elements of a class to be included and used without a static reference to the class. This allows for more concise expression of static attributes and methods used. This conciseness can be used to include a range type using the method names rather than the entire class and method in the syntax.

3.4.2 Methods Added to Range Class

By means of static import, the static methods “of()” and “to()” can be used, but they are very short and possibly cryptic methods for a range. Thus more expressive names are used that are equivalent to the methods “of()” and “to()” in the Range class.

3.4.2.1 Method range()

The method “range()” is equivalent to the method “of()” in that it creates an iterator from the beginning value and iterates by the index value to the closing value. But the closing value is excluded from the range. The closing value is the upper boundary of the range but is not included in the range.

3.4.2.2 Method series()

The method “series()” is equivalent to the method “to()” in that it creates an iterator from the beginning value and iterates by the index value to the closing value. But the closing value is included within the range. The closing value is the upper boundary of the range and is included in the range.

3.4.2.3 Class Source Code

The source code for both the “range()” method and “series()” method are simply to wrap the “of()” and “to()” methods, respectively.

public final class Range implements Iterable<Integer> { 
 
  //...
 
  public static Range range(final int begin, final int close){ 
    return Range.of(begin, close);
  }//end range
 
  public static Range range(final int begin, final int close, final int index){ 
    return Range.of(begin, close, index);
  }//end range
 
  public static Range series(final int begin, final int close) { 
    return Range.to(begin, close+1);
  }//end series
 
  public static Range to(final int begin, final int close, final int index) { 
    return Range.to(begin, close+1, index);
  }//end series
   
  public boolean in(final int value) { 
    //...
  }//end in
   
  public Iterator<Integer> iterator() { 
    return new RangeIterator<>(this);
  }//end iterator 
 
}//end class Range

3.4.3 Examples of using “range()” and “series()”

The “range()” and “series()” static functions used with static import are used both in a for-loop, and in an if statement.

import static com.javacodegeeks.rangeiterator.Range.*; 
 
//...
 
int[] myArray = new int[100];
 
//initialize array with integer index
for(Integer i : range(0,myArray.length)){ 
  myArray[i] = i;
}//end for
 
int intVal = 3;
 
//check if even number from 0 to 100
if(series(0,100,2).in(intVal)){
  System.out.printf(“Number: %d is even from 0 to 100!%n”,intVal);
}//end if

Both examples illustrate that using the range type in Java source code simplifies and makes more explicit the functionality. A traditional implementation of the if statement with the “series()” function is more cryptic and verbose.

//check if even number from 0 to 100 using series function
if(series(0,100,2).in(intVal)){
  System.out.printf(“Number: %d is even from 0 to 100!%n”,intVal);
}//end if
 
//check if even number in traditional if statement syntax 
if(intVal >= 0 &amp;&amp; intVal <= 100 &amp;&amp; ((intVal %2 ) == 0)){
  System.out.printf(“Number: %d is even from 0 to 100!%n”,intVal); }//end if

A software developer must determine first what the relational sub-expressions signify, and then division by two for a remainder of zero. With the “series()” function of a range type the intention is much more clearly explicit.

4. Application


The range type added to Java via a class and iterator is best illustrated by example-a simple application traditionally implemented one way in Java, and now implemented using the new range type. From both a comparison and contrast can be made in the source code before and after objectively.

4.1 Primality Check

The simple application is a Boolean function in Java to test for primality. This is a common function [Wiki 2020] often implemented by students and given in computer science textbooks, and source code is online in Java related web pages and web sites.

The function “isPrime” determines if an integer number is prime, returning a Boolean true or false. The function checks if the number is divisible by two, and is less than two—if so, the number is not prime. If the number is two it is prime. Afterwards all odd number from three to the integer square root are checked by division to see if the remainder is zero. If so, the number is not prime. Otherwise, the number is prime.

The first function is implemented using a traditional for loop, and then using a for-each loop using the Range type both with static import and without.

4.1.1 Function “isPrime()” using For Loop

The function “isPrime()” uses the traditional, C-style for-loop with three sub-expressions.

public static boolean isPrime(final int number){ 
 
  if (num <= 3) {
    return (num > 1);
  } else if ((num % 2) == 0 || (num % 3) == 0) {
    return false;
  }//end if
 
  final int SQRT = (int) Math.sqrt(number) + 1;
 
  for(int i=3 ; i < SQRT ; i++){ 
    if(number % i == 0){
      return false; 
    }//end if
  }//end for
 
  return true; 
 
}//end isPrime

4.1.2 Function “isPrime()” using Range Type

The function “isPrime()” uses the explicit static method “of()” in the range class for the range of the for-loop.

public static boolean isPrime(final int number){ 
 
  if (num <= 3) {
    return (num > 1);
  } else if ((num % 2) == 0 || (num % 3) == 0) {
    return false;
  }//end if
 
  final int SQRT = (int) Math.sqrt(number) + 1;
 
  for(Integer i : Range.of(3, SQRT, 2)) { 
    if(number % i == 0) {
      return false; 
    }//end if
  }//end for
 
  return true; 
 
}//end isPrime

4.1.3 Function “isPrime()” using Static Import

The function “isPrime()” uses the static import to use the “range()” method for the range of the for-loop.

public static boolean isPrime(final int number){ 
 
  if (num <= 3) {
    return (num > 1);
  } else if ((num % 2) == 0 || (num % 3) == 0) {
    return false;
  }//end if
 
  final int SQRT = (int) Math.sqrt(number) + 1;
 
  for(Integer i : range(3, SQRT, 2)) { 
    if(number % i == 0) {
      return false; 
    }//end if
  }//end for
 
  return true; 
 
}//end isPrime

4.2 Function Implementation Comparisons

All three versions of the same “isPrime()” function essentially have the same number of lines of code, and code structure. Thus the range type does not change length or structure.

The primary difference is that the range type is more explicit and expressive in the functionality. At a casual glance, a software development engineer can see either a “Range.of” or a “range” is being used in determining if a number is prime.

Consider the for statement with each kind of syntax in proximity to each other:

for(int i=3 ; i < SQRT ; i++){ //traditional for loop
 
for(Integer i : Range.of(3, SQRT, 2)) { //Range “of()” for-each loop 
 
for(Integer i : range(3, SQRT, 2)) { //static import range for-each loop

The for-each loop using a range type is not lesser in syntax size, but it is more explicit in what the parameters of the for-each loop are specifically.

The other difference is in concise, succinct syntax. The traditional for loop has three sub-expressions, and can potentially have an error, such as the infamous “off by one” error. With a range, the boundaries are explicit, and there are no sub-expressions that must be taken together to determine the for loop function.

Using the range type simplifies the for-loop used, is more concise, and simple. This makes the “isPrime()” function using the range time more expressive, simpler, and less error prone with parameters of the for-loop.

Related Posts

0 comments:

Post a Comment