/** * Returns the greater of its two arguments. * * @param a1 A value to compare with another. * @param a2 A value to compare with another. * @return The greater of the two values. */ public A max(final A a1, final A a2) { return def.append(a1, a2); }
/** * An order instance for the {@link Either} type. * * @param oa Order across the left side of {@link Either}. * @param ob Order across the right side of {@link Either}. * @return An order instance for the {@link Either} type. */ public static <A, B> Ord<Either<A, B>> eitherOrd(final Ord<A> oa, final Ord<B> ob) { Definition<A> oaDef = oa.def; Definition<B> obDef = ob.def; return ordDef((e1, e2) -> e1.isLeft() ? e2.isLeft() ? oaDef.compare(e1.left().value()).f(e2.left().value()) : Ordering.LT : e2.isLeft() ? Ordering.GT : obDef.compare(e1.right().value()).f(e2.right().value())); }
public final Ord<A> reverse() { return ordDef(def.dual()); }
/** * An order instance for the {@link Stream} type. * * @param oa Order across the elements of the stream. * @return An order instance for the {@link Stream} type. */ public static <A> Ord<Stream<A>> streamOrd(final Ord<A> oa) { return ordDef((s1, s2) -> { if (s1.isEmpty()) return s2.isEmpty() ? Ordering.EQ : Ordering.LT; else if (s2.isEmpty()) return s1.isEmpty() ? Ordering.EQ : Ordering.GT; else { final Ordering c = oa.compare(s1.head(), s2.head()); // FIXME: not stack safe return c == Ordering.EQ ? streamOrd(oa).def.compare(s1.tail()._1()).f(s2.tail()._1()) : c; } }); }
public static <A, B> Ord<P2<A, B>> p2Ord2(Ord<B> ob) { return on(P2.<A, B>__2(), ob).ord(); }
@Override public F<B, Ordering> compare(B b) { return compose(def.compare(f.f(b)), f); }
@Override public Ordering compare(A a1, A a2) { return Definition.this.compare(a2, a1); }
/** * An order instance for the {@link Option} type. * * @param oa Order across the element of the option. * @return An order instance for the {@link Option} type. */ public static <A> Ord<Option<A>> optionOrd(final Ord<A> oa) { Definition<A> oaDef = oa.def; return ordDef((o1, o2) -> o1.isNone() ? o2.isNone() ? Ordering.EQ : Ordering.LT : o2.isNone() ? Ordering.GT : oaDef.compare(o1.some()).f(o2.some())); }
public final Monoid<A> minMonoid(A zero) { return Monoid.monoidDef(def.dual(), zero); }
/** * Returns a function that returns true if its argument is greater than than the argument to this method. * * @param a A value to compare against. * @return A function that returns true if its argument is greater than the argument to this method. */ public F<A, Boolean> isGreaterThan(final A a) { return compose(o -> o == Ordering.LT, def.compare(a)); }
/** * Returns a function that returns true if its argument is less than the argument to this method. * * @param a A value to compare against. * @return A function that returns true if its argument is less than the argument to this method. */ public F<A, Boolean> isLessThan(final A a) { return compose(o -> o == Ordering.GT, def.compare(a)); }
/** * Returns <code>true</code> if the first given argument is less than the second given argument, * <code>false</code> otherwise. * * @param a1 An instance to compare for ordering to another. * @param a2 An instance to compare for ordering to another. * @return <code>true</code> if the first given argument is less than the second given argument, * <code>false</code> otherwise. */ public boolean isLessThan(final A a1, final A a2) { return def.compare(a1, a2) == Ordering.LT; }
@Override public F<A, Ordering> compare(A a) { return compose(Ordering::reverse, Definition.this.compare(a)); }
public static <A, B> Ord<P2<A, B>> p2Ord1(Ord<A> oa) { return on(P2.<A, B>__1(), oa).ord(); }
public final Semigroup<A> minSemigroup() { return semigroupDef(def.dual()); }
@Override public Ordering compare(B b1, B b2) { return def.compare(f.f(b1), f.f(b2)); } };
/** * Returns an ordering for the given arguments. * * @param a1 An instance to compare for ordering to another. * @param a2 An instance to compare for ordering to another. * @return An ordering for the given arguments. */ public Ordering compare(final A a1, final A a2) { return def.compare(a1, a2); }
/** * Returns <code>true</code> if the first given argument is less than or equal to the second given argument, * <code>false</code> otherwise. * * @param a1 An instance to compare for ordering to another. * @param a2 An instance to compare for ordering to another. * @return <code>true</code> if the first given argument is less than or equal to the second given argument, * <code>false</code> otherwise. */ public boolean isLessThanOrEqualTo(final A a1, final A a2) { return def.compare(a1, a2) != Ordering.GT; }
/** * Returns <code>true</code> if the given arguments are equal, <code>false</code> otherwise. * * @param a1 An instance to compare for equality to another. * @param a2 An instance to compare for equality to another. * @return <code>true</code> if the given arguments are equal, <code>false</code> otherwise. */ public boolean eq(final A a1, final A a2) { return def.compare(a1, a2) == Ordering.EQ; }