Guava - Range Class
L'intervallo rappresenta un intervallo o una sequenza. Viene utilizzato per ottenere un insieme di numeri / stringhe che si trovano in un intervallo particolare.
Dichiarazione di classe
Di seguito è riportata la dichiarazione per com.google.common.collect.Range<C> classe -
@GwtCompatible
public final class Range<C extends Comparable>
extends Object
implements Predicate<C>, Serializable
Metodi
Suor n | Metodo e descrizione |
---|---|
1 | static <C extends Comparable<?>> Range<C> all() Restituisce un intervallo che contiene ogni valore di tipo C. |
2 | boolean apply(C input)Deprecated. Fornito solo per soddisfare l'interfaccia Predicate; usa invece contiene (C). |
3 | static <C extends Comparable<?>> Range<C> atLeast(C endpoint) Restituisce un intervallo che contiene tutti i valori maggiori o uguali all'endpoint. |
4 | static <C extends Comparable<?>> Range<C> atMost(C endpoint) Restituisce un intervallo che contiene tutti i valori minori o uguali all'endpoint. |
5 | Range<C> canonical(DiscreteDomain<C> domain) Restituisce la forma canonica di questo intervallo nel dominio specificato. |
6 | static <C extends Comparable<?>> Range<C> closed(C lower, C upper) Restituisce un intervallo che contiene tutti i valori maggiori o uguali a inferiore e minore o uguale a superiore. |
7 | static <C extends Comparable<?>> Range<C> closedOpen(C lower, C upper) Restituisce un intervallo che contiene tutti i valori maggiori o uguali a inferiore e rigorosamente inferiori a superiore. |
8 | boolean contains(C value) Restituisce vero se il valore rientra nei limiti di questo intervallo. |
9 | boolean containsAll(Iterable<? extends C> values) Restituisce vero se ogni elemento in values è contenuto in questo intervallo. |
10 | static <C extends Comparable<?>> Range<C> downTo(C endpoint, BoundType boundType) Restituisce un intervallo dall'endpoint specificato, che può essere inclusivo (chiuso) o esclusivo (aperto), senza limite superiore. |
11 | static <C extends Comparable<?>> Range<C> encloseAll(Iterable<C> values) Restituisce l'intervallo minimo che contiene tutti i valori dati. |
12 | boolean encloses(Range<C> other) Restituisce vero se i limiti di altro non si estendono al di fuori dei limiti di questo intervallo. |
13 | boolean equals(Object object) Restituisce vero se l'oggetto è un intervallo con gli stessi endpoint e tipi associati di questo intervallo. |
14 | static <C extends Comparable<?>> Range<C> greaterThan(C endpoint) Restituisce un intervallo che contiene tutti i valori strettamente maggiori dell'endpoint. |
15 | int hashCode() Restituisce un codice hash per questo intervallo. |
16 | boolean hasLowerBound() Restituisce vero se questo intervallo ha un endpoint inferiore. |
17 | boolean hasUpperBound() Restituisce vero se questo intervallo ha un endpoint superiore. |
18 | Range<C> intersection(Range<C> connectedRange) Restituisce l'intervallo massimo racchiuso da questo intervallo e da connectedRange, se tale intervallo esiste. |
19 | boolean isConnected(Range<C> other) Restituisce vero se esiste un intervallo (possibilmente vuoto) racchiuso sia da questo intervallo che da altri. |
20 | boolean isEmpty() Restituisce vero se questo intervallo è nella forma [v..v) o (v..v]. |
21 | static <C extends Comparable<?>> Range<C> lessThan(C endpoint) Restituisce un intervallo che contiene tutti i valori strettamente inferiori a endpoint. |
22 | BoundType lowerBoundType() Restituisce il tipo del limite inferiore di questo intervallo: BoundType.CLOSED se l'intervallo include il suo punto finale inferiore, BoundType.OPEN in caso contrario. |
23 | C lowerEndpoint() Restituisce il punto finale inferiore di questo intervallo. |
24 | static <C extends Comparable<?>> Range<C> open(C lower, C upper) Restituisce un intervallo che contiene tutti i valori strettamente maggiori di lower e rigorosamente minori di upper. |
25 | static <C extends Comparable<?>> Range<C> openClosed(C lower, C upper) Restituisce un intervallo che contiene tutti i valori rigorosamente maggiori di inferiore e minore o uguale a superiore. |
26 | static <C extends Comparable<?>> Range<C> range(C lower, BoundType lowerType, C upper, BoundType upperType) Restituisce un intervallo che contiene qualsiasi valore da inferiore a superiore, dove ogni endpoint può essere inclusivo (chiuso) o esclusivo (aperto). |
27 | static <C extends Comparable<?>> Range<C> singleton(C value) Restituisce un intervallo che contiene solo il valore specificato. |
28 | Range<C> span(Range<C> other) Restituisce l'intervallo minimo che racchiude sia questo intervallo che l'altro. |
29 | String toString() Restituisce una rappresentazione di stringa di questo intervallo, come "[3..5)" (altri esempi sono elencati nella documentazione della classe). |
30 | BoundType upperBoundType() Restituisce il tipo del limite superiore di questo intervallo: BoundType.CLOSED se l'intervallo include il punto finale superiore, BoundType.OPEN se non lo fa. |
31 | C upperEndpoint() Restituisce il punto finale superiore di questo intervallo. |
32 | static <C extends Comparable<?>> Range<C> upTo(C endpoint, BoundType boundType) Restituisce un intervallo senza limite inferiore fino all'endpoint specificato, che può essere inclusivo (chiuso) o esclusivo (aperto). |
Metodi ereditati
Questa classe eredita i metodi dalla classe seguente:
- java.lang.Object
Esempio di classe di intervallo
Crea il seguente programma java usando qualsiasi editor di tua scelta, diciamo C:/> Guava.
GuavaTester.java
import com.google.common.collect.ContiguousSet;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;
import com.google.common.primitives.Ints;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testRange();
}
private void testRange() {
//create a range [a,b] = { x | a <= x <= b}
Range<Integer> range1 = Range.closed(0, 9);
System.out.print("[0,9] : ");
printRange(range1);
System.out.println("5 is present: " + range1.contains(5));
System.out.println("(1,2,3) is present: " + range1.containsAll(Ints.asList(1, 2, 3)));
System.out.println("Lower Bound: " + range1.lowerEndpoint());
System.out.println("Upper Bound: " + range1.upperEndpoint());
//create a range (a,b) = { x | a < x < b}
Range<Integer> range2 = Range.open(0, 9);
System.out.print("(0,9) : ");
printRange(range2);
//create a range (a,b] = { x | a < x <= b}
Range<Integer> range3 = Range.openClosed(0, 9);
System.out.print("(0,9] : ");
printRange(range3);
//create a range [a,b) = { x | a <= x < b}
Range<Integer> range4 = Range.closedOpen(0, 9);
System.out.print("[0,9) : ");
printRange(range4);
//create an open ended range (9, infinity
Range<Integer> range5 = Range.greaterThan(9);
System.out.println("(9,infinity) : ");
System.out.println("Lower Bound: " + range5.lowerEndpoint());
System.out.println("Upper Bound present: " + range5.hasUpperBound());
Range<Integer> range6 = Range.closed(3, 5);
printRange(range6);
//check a subrange [3,5] in [0,9]
System.out.println("[0,9] encloses [3,5]:" + range1.encloses(range6));
Range<Integer> range7 = Range.closed(9, 20);
printRange(range7);
//check ranges to be connected
System.out.println("[0,9] is connected [9,20]:" + range1.isConnected(range7));
Range<Integer> range8 = Range.closed(5, 15);
//intersection
printRange(range1.intersection(range8));
//span
printRange(range1.span(range8));
}
private void printRange(Range<Integer> range) {
System.out.print("[ ");
for(int grade : ContiguousSet.create(range, DiscreteDomain.integers())) {
System.out.print(grade +" ");
}
System.out.println("]");
}
}
Verifica il risultato
Compila la classe usando javac compilatore come segue -
C:\Guava>javac GuavaTester.java
Ora esegui il GuavaTester per vedere il risultato.
C:\Guava>java GuavaTester
Guarda il risultato.
[0,9] : [ 0 1 2 3 4 5 6 7 8 9 ]
5 is present: true
(1,2,3) is present: true
Lower Bound: 0
Upper Bound: 9
(0,9) : [ 1 2 3 4 5 6 7 8 ]
(0,9] : [ 1 2 3 4 5 6 7 8 9 ]
[0,9) : [ 0 1 2 3 4 5 6 7 8 ]
(9,infinity) :
Lower Bound: 9
Upper Bound present: false
[ 3 4 5 ]
[0,9] encloses [3,5]:true
[ 9 10 11 12 13 14 15 16 17 18 19 20 ]
[0,9] is connected [9,20]:true
[ 5 6 7 8 9 ]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ]