protected AbstractOptionalAssert(Optional<VALUE> actual, Class<?> selfType) { super(actual, selfType); this.optionalValueComparisonStrategy = StandardComparisonStrategy.instance(); }
/** * {@inheritDoc} */ @Override public void iterablesRemoveFirst(Iterable<?> iterable, Object value) { if (iterable == null) { return; } Iterator<?> iterator = iterable.iterator(); while (iterator.hasNext()) { if (areEqual(iterator.next(), value)) { iterator.remove(); return; } } }
@Override public boolean isGreaterThan(Object actual, Object other) { checkArgumentIsComparable(actual); return Comparable.class.cast(actual).compareTo(other) > 0; }
public Arrays() { this(StandardComparisonStrategy.instance()); }
/** * {@inheritDoc} */ @Override public void iterableRemoves(Iterable<?> iterable, Object value) { if (iterable == null) { return; } Iterator<?> iterator = iterable.iterator(); while (iterator.hasNext()) { if (areEqual(iterator.next(), value)) { iterator.remove(); } } } /**
@Override public boolean isLessThan(Object actual, Object other) { checkArgumentIsComparable(actual); return Comparable.class.cast(actual).compareTo(other) < 0; }
/** * Creates a new <code>{@link ShouldContainSubsequence}</code>. * * @param actual the actual value in the failed assertion. * @param subsequence the subsequence of values expected to be in {@code actual}. * @return the created {@code ErrorMessageFactory}. */ public static ErrorMessageFactory shouldContainSubsequence(Object actual, Object subsequence) { return new ShouldContainSubsequence(actual, subsequence, StandardComparisonStrategy.instance()); }
/** * Returns true if given {@link Iterable} contains given value based on {@link Objects#areEqual(Object, Object)}, * false otherwise.<br> * If given {@link Iterable} is null, return false. * * @param iterable the {@link Iterable} to search value in * @param value the object to look for in given {@link Iterable} * @return true if given {@link Iterable} contains given value based on {@link Objects#areEqual(Object, Object)}, * false otherwise. */ @Override public boolean iterableContains(Iterable<?> iterable, Object value) { if (iterable == null) { return false; } return Streams.stream(iterable).anyMatch(object -> areEqual(object, value)); }
@Override public boolean isLessThan(Object actual, Object other) { checkArgumentIsComparable(actual); return Comparable.class.cast(actual).compareTo(other) < 0; }
/** * Creates a new <code>{@link ShouldBeInThePast}</code>. * @param actual the actual value in the failed assertion. * @return the created {@code ErrorMessageFactory}. */ public static ErrorMessageFactory shouldBeInThePast(Date actual) { return new ShouldBeInThePast(actual, StandardComparisonStrategy.instance()); }
@Override protected Set<Object> newSetUsingComparisonStrategy() { // define a comparator so that we can use areEqual to compare objects in Set collections // the "less than" comparison does not make much sense here but need to be defined. return new TreeSet<>((o1, o2) -> { if (areEqual(o1, o2)) return 0; return Objects.hashCodeFor(o1) < Objects.hashCodeFor(o2) ? -1 : 1; }); }
@Override public boolean isGreaterThan(Object actual, Object other) { checkArgumentIsComparable(actual); return Comparable.class.cast(actual).compareTo(other) > 0; }
/** * Creates a new <code>{@link ShouldBeLess}</code>. * @param <T> guarantees that the values used in this factory have the same type. * @param actual the actual value in the failed assertion. * @param other the value used in the failed assertion to compare the actual value to. * @return the created {@code ErrorMessageFactory}. */ public static <T extends Comparable<? super T>> ErrorMessageFactory shouldBeLess(T actual, T other) { return new ShouldBeLess(actual, other, StandardComparisonStrategy.instance()); }
@Override protected Set<Object> newSetUsingComparisonStrategy() { // define a comparator so that we can use areEqual to compare objects in Set collections // the "less than" comparison does not make much sense here but need to be defined. return new TreeSet<>((o1, o2) -> { if (areEqual(o1, o2)) return 0; return Objects.hashCodeFor(o1) < Objects.hashCodeFor(o2) ? -1 : 1; }); }
/** * Creates a new <code>{@link ShouldBeAfterYear}</code>. * @param actual the actual value in the failed assertion. * @param year the year to compare the actual date's year to. * @return the created {@code ErrorMessageFactory}. */ public static ErrorMessageFactory shouldBeAfterYear(Date actual, int year) { return new ShouldBeAfterYear(actual, year, StandardComparisonStrategy.instance()); }
/** * Creates a new <code>{@link ShouldContainAtIndex}</code>. * @param actual the actual value in the failed assertion. * @param expected value expected to be in {@code actual}. * @param index the index of the expected value. * @param found the value in {@code actual} stored under {@code index}. * @return the created {@code ErrorMessageFactory}. */ public static ErrorMessageFactory shouldContainAtIndex(Object actual, Object expected, Index index, Object found) { return new ShouldContainAtIndex(actual, expected, index, found, StandardComparisonStrategy.instance()); }
/** * Creates a new <code>{@link ShouldBeEqual}</code>. * * @param actual the actual value in the failed assertion. * @param expected the expected value in the failed assertion. * @param representation the {@link Representation} used to format values. * @return the created {@code AssertionErrorFactory}. */ public static AssertionErrorFactory shouldBeEqual(Object actual, Object expected, Representation representation) { return new ShouldBeEqual(actual, expected, StandardComparisonStrategy.instance(), representation); }
/** * Creates a new <code>{@link ShouldNotHaveDuplicates}</code>. * @param actual the actual value in the failed assertion. * @param duplicates the duplicate values found in {@code actual}. * @return an instance of {@code ErrorMessageFactory}. */ public static ErrorMessageFactory shouldNotHaveDuplicates(Object actual, Object duplicates) { return new ShouldNotHaveDuplicates(actual, duplicates, StandardComparisonStrategy.instance()); }
/** * Creates a new <code>{@link ShouldBeBefore}</code>. * @param actual the actual value in the failed assertion. * @param other the value used in the failed assertion to compare the actual value to. * @return the created {@code ErrorMessageFactory}. */ public static ErrorMessageFactory shouldBeBefore(Object actual, Object other) { return new ShouldBeBefore(actual, other, StandardComparisonStrategy.instance()); }
/** * Creates a new <code>{@link ShouldNotContainSequence}</code>. * @param actual the actual value in the failed assertion. * @param sequence the sequence of values expected to be in {@code actual}. * @param index the index where the sequence was found. * @return the created {@code ErrorMessageFactory}. */ public static ErrorMessageFactory shouldNotContainSequence(Object actual, Object sequence, int index) { return new ShouldNotContainSequence(actual, sequence, index, StandardComparisonStrategy.instance()); }