GeoTools

OSGeo

Previous topic

GeoTools

Next topic

Text

This Page

RangeΒΆ

GeoTools inlcudes a Range class to represent a numeric, or value, range. This is handy when expressing scales a dataset is useful for.

    // simple range of one value
    Range<Double> zero = new Range<>(Double.class, 0.0);
    
    System.out.println(zero); // [0.0, 0.0]
    
    // range include two values
    Range<Double> from1to5 = new Range<>(Double.class, 1.0, 5.0);
    System.out.println(from1to5); // [1.0, 5.0]
    
    // range from one value up to a limit
    Range<Double> from1upto10 = new Range<>(Double.class, 1.0, true, 10.0, false);
    System.out.println(from1upto10); // [1.0, 10.0)
    
    // range between two values
    Range<Double> between6and8 = new Range<>(Double.class, 6.0, false, 8.0, false);
    System.out.println(between6and8); // (6.0, 8.0)
    
    // range details
    from1to5.getMinValue();
    from1to5.isMinIncluded();
    from1to5.getMaxValue();
    from1to5.isMaxIncluded();
    
    // range operations
    from1upto10.subtract(between6and8); // returns two ranges
    from1upto10.subtract(from1to5); // returns one range
    
    from1to5.union(between6and8); // returns [1.0,8.0)
    
    // range check
    from1to5.contains(3.0); // true
    from1upto10.contains(between6and8); // true
    

This actually works with any Comparable such as Strings or enumerations.

    Range<Date> today = new Range<>(Date.class, new Date());

We have a special NumericRange that uses the fact that numbers can work together to allow comparisons between NumericRange<Double> and NumericRange<Integer>.

    
    NumberRange<Double> positive = NumberRange.create(0.0, Double.MAX_VALUE);
    NumberRange<Double> negative = NumberRange.create(0.0, false, Double.MIN_VALUE, true);
    
    NumberRange<Integer> decimal = NumberRange.create(1, 10);
    positive.contains(decimal);
    
    positive.contains((Number) 3.0); // test double
    positive.contains((Number) 3); // test integer