Guava 8-Zone

Source: Internet
Author: User
Tags comparable iterable

Example
List scores;iterable Belowmedian =iterables.filter (Scores,range.lessthan (median)); .... Range validgrades = range.closed (1, N); for (int grade:ContiguousSet.create (validgrades, Discretedomain.integers ())) {    ...}
Brief introduction

An interval, sometimes referred to as a range, is a section of convexity in a particular domain (the informal term is continuous or uninterrupted). In form, convexity indicates to A<=b<=c, Range.contains (a) and range.contains (c) means Range.contains (b).

The interval can extend to infinity-for example, the range "X>3″" includes any value greater than 3-and can also be limited to finite, such as "2<=x<5″." Guava in a more compact way, programmers with mathematical backgrounds are familiar with this:

    • (a). b) = {x | A < x < b}
    • [A.. b] = {x | A <= x <= b}
    • [A.. b) = {x | A <= x < b}
    • (a). b] = {x | A < x <= B}
    • (a). +∞) = {x | x > A}
    • [A.. +∞) = {x | x >= A}
    • (-∞. b) = {x | x < b}
    • (-∞. b] = {x | x <= B}
    • (-∞. +∞) = ALL values

The top A and b are called endpoints. To improve consistency, the range in the guava requires that the upper point cannot be less than the bottom point. The upper and lower endpoints may be equal, but the requirement interval is a closed interval or a half open half-closed interval (at least one of the endpoints is contained in the interval):

    • [A.. A]: single element interval
    • [A.. a); (a). A]: empty intervals, but they are valid
    • (a). A): Invalid interval

The guava uses the type range<c> to represent the interval. All interval implementations are immutable types.

Build interval

An interval instance can be obtained by a static method of the Range class:

(a). b Open (c, c)
[A.. b Closed (c, c)
[A.. b Closedopen (c, c)
(a). b Openclosed (c, c)
(a). +∞) GreaterThan (C)
[A.. +∞) AtLeast (C)
(-∞. b LessThan (C)
(-∞. b Atmost (C)
(-∞. +∞) All ()
Range.closed ("left", "right"); The string between "left" and "right" in the dictionary order, closed interval Range.lessthan (4.0); A double value that is strictly less than 4.0

In addition, you can explicitly specify a boundary type to construct the interval:

Bounded interval Range (c, Boundtype, C, Boundtype)
No upper bound interval: ((a). +∞) or [A.. +∞)) Downto (C, Boundtype)
No Nether Interval: ((-∞). b) or (-∞. b]) UpTo (C, Boundtype)

The Boundtype here is an enumeration type that contains both closed and open two values.

Range.downto (4, boundtype);//(a). +∞) or [A.. +∞), depending on boundtyperange.range (1, CLOSED, 4, OPEN);//[1..4], equivalent to Range.closedopen (1, 4)
Interval operations

The basic operation of range is its contains (C) method, which, as you would expect, is used to determine whether a value is included in the interval. In addition, the range instance can be used as predicate and in functional programming (Translator Note: See Chapter 4th). Any range instance also supports the Containsall (iterable<? extends C>) method:

Range.closed (1, 3). Contains (2);//return truerange.closed (1, 3). Contains (4);//return Falserange.lessthan (5). Contains (5); Return falserange.closed (1, 4). Containsall (Ints.aslist (1, 2, 3)); return True
Query operations

The Range class provides the following methods to view the endpoints of the interval:

    • Haslowerbound () and Hasupperbound (): Determine whether the interval has a specific boundary, or infinite;
    • Lowerboundtype () and Upperboundtype (): Returns the interval boundary type, closed or open, and if the interval has no corresponding boundary, throws illegalstateexception;
    • Lowerendpoint () and Upperendpoint (): Returns the end value of the interval, or if the interval has no corresponding boundary, throws the IllegalStateException;
    • IsEmpty (): Determines whether it is an empty interval.
Range.closedopen (4, 4). IsEmpty (); Returns truerange.openclosed (4, 4). IsEmpty (); Returns truerange.closed (4, 4). IsEmpty (); Returns Falserange.open (4, 4). IsEmpty (); Range.open throws Illegalargumentexceptionrange.closed (3, ten). Lowerendpoint (); Returns 3range.open (3, ten). Lowerendpoint (); Returns 3range.closed (3, ten). Lowerboundtype (); Returns Closedrange.open (3, ten). Upperboundtype (); Returns OPEN
Relational operations

Contains [enclose]

The most basic relationship between intervals is the inclusion of [encloses]: If the boundary of the inner interval does not exceed the boundary of the outer interval, the outer interval contains the inner interval. The results of the included judgments depend entirely on the comparison of the interval endpoints!

    • [3..6] contains [4..5];
    • (3..6) contains (3..6);
    • [3..6] contains [4..4], although the latter is an empty interval;
    • (3..6] does not contain [3..6];
    • [4..5] does not contain (3..6), although the former contains all the values of the latter, the discrete domain [discrete domains] can solve the problem (see section 8.5);
    • [3..6] does not contain (1..1], although the former contains all the values of the latter.

Inclusion is a partial-order relationship [partial ordering]. Based on the concept of inclusive relationships, range also provides the following methods of operation.

Connected [isconnected]

range.isconnected (range) determines whether the interval is connected. Specifically, the isconnected tests whether there are intervals that are included in both intervals, which is equivalent to the mathematical definition of the "two-interval aggregation is the form of a continuous set" (except for the special case of an empty interval).

The connection is a reflexive [reflexive], symmetrical [symmetric] relationship.

Range.closed (3, 5). IsConnected (Range.open (5, 10)); Returns truerange.closed (0, 9). IsConnected (Range.closed (3, 4)); Returns truerange.closed (0, 5). IsConnected (Range.closed (3, 9)); Returns Truerange.open (3, 5). IsConnected (Range.open (5, 10)); Returns falserange.closed (1, 5). IsConnected (range.closed (6, 10)); Returns false

Intersection [intersection]

range.intersection (Range) returns the intersection of two intervals: both in the first interval and in the largest interval of the other interval. When and only if two intervals are connected, they have a intersection. If two intervals do not intersect, the method throws a IllegalArgumentException.

The intersection is an interchangeable [commutative], associated [associative] operation [Operation].

Range.closed (3, 5). Intersection (Range.open (5, 10)); Returns (5, 5]range.closed (0, 9). Intersection (Range.closed (3, 4)); Returns [3, 4]range.closed (0, 5). Intersection (Range.closed (3, 9)); Returns [3, 5]range.open (3, 5). Intersection (Range.open (5, 10)); Throws Iaerange.closed (1, 5). Intersection (range.closed (6, 10)); Throws IAE

span range [span]

Range.span (Range) returns the "minimum interval that includes two intervals", and if two intervals are connected, that is their set.

span is an interchangeable [commutative], associated [associative], closed [closed] operation [Operation].

Range.closed (3, 5). Span (Range.open (5, 10)); Returns [3, range.closed (0, 9). Span (range.closed (3, 4)); Returns [0, 9]range.closed (0, 5). Span (range.closed (3, 9)); Returns [0, 9]range.open (3, 5). Span (Range.open (5, 10)); Returns (3, range.closed) (1, 5). Span (range.closed (6, 10)); Returns [1, 10]
Discrete domain

Partial (but not all) comparable types are discrete, meaning that the upper and lower bounds of the interval are enumerable.

In guava, the discrete form operation of type C is implemented with discretedomain<c>. A discrete field always represents a complete collection of values of a type, which cannot represent local domains such as "prime", "string length 5", or "timestamp at midnight".

The discrete domain instances provided by Discretedomain include:

Type Discrete domain
Integer Integers ()
Long Longs ()

Once you have obtained the Discretedomain instance, you can use the following range operation method:

    • Contiguousset.create (range, domain): represents elements in range<c> that conform to a discrete domain definition in immutablesortedset<c> form, and adds some extra action - Translator Note: The actual return immutablesortedset subclass contiguousset. (Does not work for infinite intervals unless type C itself is limited, such as int is enumerable)
    • Canonical (domain): the "canonical form" of converting a discrete domain to an interval. If Contiguousset.create (A, domain). Equals (Contiguousset.create (b, domain)) and!a.isempty (), then there is a.canonical (domain). Equals (b.canonical (domain)). (This does not mean that a.equals (b))
Immutablesortedset set = Contigousset.create (Range.open (1, 5), iscretedomain.integers ());//set contains [2, 3, 4] Contiguousset.create (Range.greaterthan (0), discretedomain.integers ());//set contains [1, 2, ..., Integer.max_value]

Note that Contiguousset.create does not really construct the entire collection, but instead returns the interval view in set form.

Your own discrete domain

You can create your own discrete domain, but you must remember several important aspects of the Discretedomain contract.

    • A discrete field always represents a complete collection of values of a type, and it cannot represent a local domain such as a "prime" or a "string of length 5". So, for example, you can't construct a discretedomain to represent a Joda datetime date collection that is accurate to seconds: because that will not contain all the values of the Joda datetime.
    • Discretedomain may be infinite-for example, BigInteger Discretedomain. In this case, you should use the default implementations of MinValue () and MaxValue (), which will throw nosuchelementexception. But guava prohibits the introduction of an infinite interval into the contiguousset.create-- translator Note: There is obviously not an enumerable set.
What if I need a comparator?

We wanted to find a specific balance between the availability of range and API complexity, which led us to not provide an comparator-based interface: we don't need to worry about how the interval is based on different comparator interactions; All API signatures are simple and clear; that's better.

On the other hand, if you need any comparator, you can do one of the following:

    • Use the generic predicate interface instead of the range class. (Range implements the predicate interface, so you can get predicate instances with Predicates.compose (range, function))
    • Use the wrapper class to define the desired sort.

Translator Note: In fact, the range specifies that the element type must be comparable, which already satisfies most requirements. If you need to customize special comparison logic, you can use Predicates.compose (range, function) to combine the function of the comparison.

Guava 8-Zone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.