Guava: Range 开闭区间

package com.wdxxl.guava.model.range;

import java.util.Set;

import com.google.common.collect.ContiguousSet;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;
/**
 * http://docs.guava-libraries.googlecode.com/git-history/v12.0/javadoc/com/google/common/collect/Range.html
 * 
 * Notation Definition  Factory method
 * (a..b)  {x | a < x < b}     open
 * [a..b]  {x | a <= x <= b}   closed
 * (a..b]  {x | a < x <= b}    openClosed
 * [a..b)  {x | a <= x < b}    closedOpen
 * (a..+∞) {x | x > a}         greaterThan
 * [a..+∞) {x | x >= a}        atLeast
 * (-∞..b) {x | x < b}         lessThan
 * (-∞..b] {x | x <= b}        atMost
 * (-∞..+∞)    {x}             all
 */
public class RangeTest {

    public static void main(String[] args) {
        Range range = Range.closed(20, 30);
        print("closed", ContiguousSet.create(range, DiscreteDomain.integers()));

        range = Range.open(20, 30);
        print("open", ContiguousSet.create(range, DiscreteDomain.integers()));

        range = Range.openClosed(20, 30);
        print("openClosed", ContiguousSet.create(range, DiscreteDomain.integers()));

        range = Range.closedOpen(20, 30);
        print("closedOpen", ContiguousSet.create(range, DiscreteDomain.integers()));

        range = Range.greaterThan(20);
        System.out.println("greaterThan: "
                + ContiguousSet.create(range, DiscreteDomain.integers()).toString());

        range = Range.atLeast(20);
        System.out.println(
                "atLeast: " + ContiguousSet.create(range, DiscreteDomain.integers()).toString());

        range = Range.lessThan(30);
        System.out.println(
                "lessThan: " + ContiguousSet.create(range, DiscreteDomain.integers()).toString());

        range = Range.atMost(30);
        System.out.println(
                "atMost: " + ContiguousSet.create(range, DiscreteDomain.integers()).toString());

        range = Range.all();
        System.out.println(
                "all: " + ContiguousSet.create(range, DiscreteDomain.integers()).toString());
    }

    public static void print(String type, Set ranges) {
        boolean isFirst = true;
        System.out.print(type + ": ");
        for (Integer i : ranges) {
            System.out.print(isFirst ? i : "," + i);
            isFirst = false;
        }
        System.out.println();
    }
    /**
     * closed: 20,21,22,23,24,25,26,27,28,29,30
     * open: 21,22,23,24,25,26,27,28,29
     * openClosed: 21,22,23,24,25,26,27,28,29,30
     * closedOpen: 20,21,22,23,24,25,26,27,28,29
     * greaterThan: [21‥2147483647]
     * atLeast: [20‥2147483647]
     * lessThan: [-2147483648‥29]
     * atMost: [-2147483648‥30]
     * all: [-2147483648‥2147483647]
     */
}

你可能感兴趣的:(Guava: Range 开闭区间)