Open In App

ValueRange of() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The of() method of ValueRange class helps us to Obtains value range based on parameters passed to it.

There are three types of of() methods based on parameters passed to it.

  1. of(long min, long max): This method help us to get a fixed value range where the minimum and maximum values are fixed.
    Syntax:

    public static ValueRange of(long min, long max)
    

    Parameters: This method accepts two parameters:

    • min which is the minimum value
    • max which is the maximum value

    Return value: This method returns the ValueRange for min, max, not null.

    Exception: This method throws IllegalArgumentException if the minimum is greater than the maximum.

    Below program illustrate the ValueRange.of(long min, long max) method:
    Program 1:




    // Java program to demonstrate
    // ValueRange.of(long min, long max) method
      
    import java.time.temporal.ValueRange;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // create ValueRange using
            // of(long min, long max)
            ValueRange vRange = ValueRange.of(0, 66666);
      
            // print results
            System.out.println("ValueRange: "
                               + vRange.toString());
        }
    }

    
    

    Output:

    ValueRange: 0 - 66666
    
  2. of(long min, long maxSmallest, long maxLargest): This method helps us to get a variable value range where the minimum value is fixed and the maximum value may vary.

    Syntax:

    public static ValueRange of(long min,
                                long maxSmallest,
                                long maxLargest)
    

    Parameters: This method accepts three parameters:

    • min which is the minimum value,
    • maxSmallest which is the smallest maximum value
    • maxLargest which is the largest maximum value.

    Return value: This method returns the ValueRange for min, smallest max, largest max, not null.

    Exception: This method throws IllegalArgumentException if the minimum is greater than the smallest maximum, or the smallest maximum is greater than the largest maximum.

    Below program illustrate the ValueRange.of(long min, long maxSmallest, long maxLargest) method:
    Program 2:




    // Java program to demonstrate
    // of(long, long, long) method
      
    import java.time.temporal.ValueRange;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // create ValueRange using
            // of(long min, long maxSmallest, long maxLargest)
            ValueRange vRange = ValueRange.of(0, 230, 500);
      
            // print results
            System.out.println("ValueRange: "
                               + vRange.toString());
        }
    }

    
    

    Output:

    ValueRange: 0 - 230/500
    
  3. of(long minSmallest, long minLargest, long maxSmallest, long maxLargest): This method helps us to get a fully variable value range where both the minimum and maximum value may vary.

    Syntax:

    public static ValueRange of(long minSmallest, long minLargest,
                                long maxSmallest, long maxLargest)
    

    Parameters: This method accepts four parameters:

    • minSmallest which is the smallest minimum value
    • minLargest which is the largest minimum value,
    • maxSmallest which is the smallest maximum value
    • maxLargest which is the largest maximum value.

    Return value: This method returns the ValueRange for a smallest min, largest min, smallest max, largest max, not null.

    Exception: This method throws IllegalArgumentException if the smallest minimum is greater than the smallest maximum, or the smallest maximum is greater than the largest maximum or the largest minimum is greater than the largest maximum.

    Below program illustrate the ValueRange.of(long minSmallest, long minLargest, long maxSmallest, long maxLargest) method:
    Program 2:




    // Java program to demonstrate
    // of(long, long, long, long) method
      
    import java.time.temporal.ValueRange;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // create ValueRange using
            // of(long minSmallest, long minLargest,
            // long maxSmallest, long maxLargest)
            ValueRange vRange
                = ValueRange.of(0, 1, 500, 1000);
      
            // print results
            System.out.println("ValueRange: "
                               + vRange.toString());
        }
    }

    
    

    Output:

    ValueRange: 0/1 - 500/1000
    

References: https://docs.oracle.com/javase/10/docs/api/java/time/temporal/ValueRange.html



Last Updated : 29 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads