ValueRange of() method in Java with Examples

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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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());
        }
    }

    chevron_right

    
    

    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:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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());
        }
    }

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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());
        }
    }

    chevron_right

    
    

    Output:

    ValueRange: 0/1 - 500/1000
    

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

java-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.