net.sf.staccatocommons.lang
Class Compare

java.lang.Object
  extended by net.sf.staccatocommons.lang.Compare

public class Compare
extends Object

Class methods that implement comparisons for Comparables, like between(max, min) and in(collection)

Author:
flbulgarelli

Constructor Summary
Compare()
           
 
Method Summary
static boolean between(int element, int min, int max)
          Tests that given three ints element, min and max, is true that: min <= element <= max
static boolean between(long element, long min, long max)
          Tests that given three longs element, min and max, is true that: min <= element <= max
static
<T extends Comparable<T>>
net.sf.staccatocommons.defs.predicate.Predicate<T>
between(T min, T max)
          Answers a predicate that evalutes if its argument is between min and max
static
<T extends Comparable<T>>
boolean
between(T element, T min, T max)
          Tests that given three Comparables element, min and max, is true that: min <= element <= max
static
<T> boolean
between(T element, T min, T max, Comparator<T> comparator)
          Tests that given three Comparables element, min and max, and a Comparator, using that comparator is true that: min <= element <= max
static
<T extends Comparable<T>>
net.sf.staccatocommons.defs.predicate.Predicate<T>
greaterThan(T value)
          Returns a predicate that evaluates if its argument is greater than the given value.
static
<T extends Comparable<T>>
net.sf.staccatocommons.defs.predicate.Predicate<T>
greaterThanOrEqualTo(T value)
          Returns a predicate that evaluates if its argument is greater than or equal to the given value.
static
<T> net.sf.staccatocommons.defs.predicate.Predicate<T>
in_(T... values)
          Returns a predicate that tests if its argument is equal to any of the given values
static boolean in(int element, int[] values)
          Tests if a given array contains an element
static boolean in(long element, long[] values)
          Tests if a given array contains an element
static
<T> boolean
in(T element, T... values)
          Tests if a given array contains an element
static
<T extends Comparable<T>>
net.sf.staccatocommons.defs.predicate.Predicate<T>
lessThan(T value)
          Returns a predicate that evaluates if its argument is less than the given value.
static
<T extends Comparable<T>>
net.sf.staccatocommons.defs.predicate.Predicate<T>
lessThanOrEqualTo(T value)
          Returns a predicate that evaluates if its argument is lower than or equal to the given value.
static
<A> net.sf.staccatocommons.defs.function.Function2<A,A,A>
max(Comparator<? super A> comparator)
          Answers a new AbstractFunction2 that returns the max of its arguments using the given comparator.
static
<T extends Comparable<T>>
T
max(T c1, T c2)
           
static
<T> T
max(T c1, T c2, Comparator<? super T> comparator)
          Answers the min element between c1 and c2, using the given Comparator
static
<A> net.sf.staccatocommons.defs.function.Function2<A,A,A>
min(Comparator<? super A> comparator)
          Answers a new AbstractFunction2 that returns the min of its arguments using the given comparator.
static
<T extends Comparable<T>>
T
min(T c1, T c2)
           
static
<T> T
min(T c1, T c2, Comparator<? super T> comparator)
          Answers the max element between c1 and c2, using the given Comparator
static
<A extends Comparable<A>>
Comparator<A>
natural()
          Answers a natural comparator, that is, a comparator that compares elements using its natural order, as defined by Comparable.
static
<A,B extends Comparable<B>>
Comparator<A>
on(net.sf.staccatocommons.defs.Applicable<? super A,B> function)
          Answers a new Comparator that performs the comparison between the results of applying the given function to its arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Compare

public Compare()
Method Detail

between

public static <T extends Comparable<T>> boolean between(@NonNull
                                                        T element,
                                                        @NonNull
                                                        T min,
                                                        @NonNull
                                                        T max)
Tests that given three Comparables element, min and max, is true that:
 min <= element <= max
 

Type Parameters:
T - the type of comparable element
Parameters:
element - non null
min - non null
max - non null
Returns:
if element is between min inclusive, and max, inclusive

between

public static <T> boolean between(@NonNull
                                  T element,
                                  @NonNull
                                  T min,
                                  @NonNull
                                  T max,
                                  @NonNull
                                  Comparator<T> comparator)
Tests that given three Comparables element, min and max, and a Comparator, using that comparator is true that:
 min <= element <= max
 

Type Parameters:
T - the type of comparable element
Parameters:
element - non null
min - non null
max - non null
comparator - non null
Returns:
if element is between min inclusive, and max, inclusive, using the given Comparator as ordering criteria

between

public static boolean between(long element,
                              long min,
                              long max)
Tests that given three longs element, min and max, is true that:
 min <= element <= max
 

Parameters:
element - non null
min - non null
max - non null
Returns:
if element is between min inclusive, and max, inclusive

between

public static boolean between(int element,
                              int min,
                              int max)
Tests that given three ints element, min and max, is true that:
 min <= element <= max
 

Parameters:
element - non null
min - non null
max - non null
Returns:
if element is between min inclusive, and max, inclusive

between

public static <T extends Comparable<T>> net.sf.staccatocommons.defs.predicate.Predicate<T> between(@NonNull
                                                                                                   T min,
                                                                                                   @NonNull
                                                                                                   T max)
Answers a predicate that evalutes if its argument is between min and max

Type Parameters:
T -
Parameters:
min -
max -
Returns:
a new Predicate

in

public static boolean in(int element,
                         @NonNull
                         int[] values)
Tests if a given array contains an element

Parameters:
element -
values -
Returns:
if the array contains the given element

in

public static boolean in(long element,
                         @NonNull
                         long[] values)
Tests if a given array contains an element

Parameters:
element -
values -
Returns:
if the array contains the given element

in

public static <T> boolean in(@NonNull
                             T element,
                             @NonNull
                             T... values)
Tests if a given array contains an element

Type Parameters:
T -
Parameters:
element -
values -
Returns:
if the array contains the given element, using equals comparison

in_

public static <T> net.sf.staccatocommons.defs.predicate.Predicate<T> in_(@NonNull
                                                                         T... values)
Returns a predicate that tests if its argument is equal to any of the given values

Type Parameters:
T -
Parameters:
values -
Returns:
a new Predicate

min

@NonNull
public static <T extends Comparable<T>> T min(@NonNull
                                                      T c1,
                                                      @NonNull
                                                      T c2)
Type Parameters:
T -
Parameters:
c1 -
c2 -
Returns:
c1 if it is lower than or equal to c2, c2 otherwise.

max

@NonNull
public static <T extends Comparable<T>> T max(@NonNull
                                                      T c1,
                                                      @NonNull
                                                      T c2)
Type Parameters:
T -
Parameters:
c1 -
c2 -
Returns:
c1 if it is greater than or equal to c2, c2 otherwise.

min

@NonNull
public static <A> net.sf.staccatocommons.defs.function.Function2<A,A,A> min(@NonNull
                                                                                    Comparator<? super A> comparator)
Answers a new AbstractFunction2 that returns the min of its arguments using the given comparator.

Type Parameters:
A -
Parameters:
comparator -
Returns:
a new AbstractFunction2

max

@NonNull
public static <A> net.sf.staccatocommons.defs.function.Function2<A,A,A> max(@NonNull
                                                                                    Comparator<? super A> comparator)
Answers a new AbstractFunction2 that returns the max of its arguments using the given comparator.

Type Parameters:
A -
Parameters:
comparator -
Returns:
a new AbstractFunction2

max

@NonNull
public static <T> T max(@NonNull
                                T c1,
                                @NonNull
                                T c2,
                                @NonNull
                                Comparator<? super T> comparator)
Answers the min element between c1 and c2, using the given Comparator

Type Parameters:
T -
Parameters:
c1 -
c2 -
comparator -
Returns:
comparator.compare(c1, c2) >= 0 ? c1 : c2

min

@NonNull
public static <T> T min(@NonNull
                                T c1,
                                @NonNull
                                T c2,
                                Comparator<? super T> comparator)
Answers the max element between c1 and c2, using the given Comparator

Type Parameters:
T -
Parameters:
c1 -
c2 -
comparator -
Returns:
comparator.compare(c1, c2) <= 0 ? c1 : c2

on

@NonNull
public static <A,B extends Comparable<B>> Comparator<A> on(@NonNull
                                                                   net.sf.staccatocommons.defs.Applicable<? super A,B> function)
Answers a new Comparator that performs the comparison between the results of applying the given function to its arguments.

Type Parameters:
A -
B -
Parameters:
function -
Returns:
a new Comparator

lessThan

public static <T extends Comparable<T>> net.sf.staccatocommons.defs.predicate.Predicate<T> lessThan(@NonNull
                                                                                                    T value)
Returns a predicate that evaluates if its argument is less than the given value. More formally, this method returns a new predicate that evaluates comparable argument with the statement argument.compareTo(value) < 0

Type Parameters:
T -
Parameters:
value -
Returns:
a new predicate

lessThanOrEqualTo

public static <T extends Comparable<T>> net.sf.staccatocommons.defs.predicate.Predicate<T> lessThanOrEqualTo(@NonNull
                                                                                                             T value)
Returns a predicate that evaluates if its argument is lower than or equal to the given value. More formally, this method returns a new predicate that evaluates comparable argument with the statement argument.compareTo(value) <= 0

Type Parameters:
T -
Parameters:
value -
Returns:
a new predicate

greaterThan

public static <T extends Comparable<T>> net.sf.staccatocommons.defs.predicate.Predicate<T> greaterThan(@NonNull
                                                                                                       T value)
Returns a predicate that evaluates if its argument is greater than the given value. More formally, this method returns a new predicate that evaluates comparable argument with the statement argument.compareTo(value) > 0

Type Parameters:
T -
Parameters:
value -
Returns:
a new predicate

greaterThanOrEqualTo

public static <T extends Comparable<T>> net.sf.staccatocommons.defs.predicate.Predicate<T> greaterThanOrEqualTo(@NonNull
                                                                                                                T value)
Returns a predicate that evaluates if its argument is greater than or equal to the given value. More formally, this method returns a new predicate that evaluates comparable argument with the statement argument.compareTo(value) >= 0

Type Parameters:
T -
Parameters:
value -
Returns:
a new predicate

natural

@Constant
public static <A extends Comparable<A>> Comparator<A> natural()
Answers a natural comparator, that is, a comparator that compares elements using its natural order, as defined by Comparable.

Type Parameters:
A -
Returns:
a natural Comparator


Copyright © 2010-2012 Staccatocommons. All Rights Reserved.