001 /*
002 * Created on Oct 4, 2009
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2009-2011 the original author or authors.
014 */
015 package org.fest.assertions;
016
017 import static org.fest.assertions.ErrorMessages.*;
018 import static org.fest.assertions.Fail.comparisonFailed;
019
020 /**
021 * Template for assertions applicable to <code>{@link Comparable}</code>s.
022 * @param <S> used to simulate "self types." For more information please read "<a
023 * href="http://passion.forco.de/content/emulating-self-types-using-java-generics-simplify-fluent-api-implementation"
024 * target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>."
025 * @param <T> the type of the "actual" value.
026 *
027 * @author Alex Ruiz
028 * @author Ted M. Young
029 */
030 public abstract class ComparableAssert<S, T extends Comparable<T>> extends GenericAssert<S, T> {
031
032 /**
033 * Creates a new </code>{@link ComparableAssert}</code>.
034 * @param selfType the "self type."
035 * @param actual the target to verify.
036 */
037 protected ComparableAssert(Class<S> selfType, T actual) {
038 super(selfType, actual);
039 }
040
041 /**
042 * Verifies that the actual <code>{@link Comparable}</code> is equal to the given one.
043 * @param expected the given {@code Comparable} to compare the actual {@code Comparable} to.
044 * @return this assertion object.
045 * @throws AssertionError if the actual {@code Comparable} is {@code null}.
046 * @throws AssertionError if the actual {@code Comparable} is not equal to the given one.
047 */
048 public final S isEqualByComparingTo(T expected) {
049 isNotNull();
050 if (actual.compareTo(expected) == 0) return myself;
051 failIfCustomMessageIsSet();
052 throw comparisonFailed(rawDescription(), actual, expected);
053 }
054
055 /**
056 * Verifies that the actual <code>{@link Comparable}</code> is <b>not</b> equal to the given one.
057 * @param expected the given {@code Comparable} to use to compare to the actual {@code Comparable}.
058 * @return this assertion object.
059 * @throws AssertionError if the actual {@code Comparable} is {@code null}.
060 * @throws AssertionError if the actual {@code Comparable} is equal to the given one.
061 */
062 public final S isNotEqualByComparingTo(T expected) {
063 isNotNull();
064 if (actual.compareTo(expected) != 0) return myself;
065 failIfCustomMessageIsSet();
066 throw failure(unexpectedEqual(actual, expected));
067 }
068
069 /**
070 * Verifies that the actual <code>{@link Comparable}</code> is less than the given one.
071 * @param other the given value.
072 * @return this assertion object.
073 * @throws AssertionError if the actual {@code Comparable} is {@code null}.
074 * @throws AssertionError if the actual {@code Comparable} is not less than the given one.
075 */
076 public final S isLessThan(T other) {
077 isNotNull();
078 if (actual.compareTo(other) < 0) return myself;
079 failIfCustomMessageIsSet();
080 throw failure(unexpectedGreaterThanOrEqualTo(actual, other));
081 }
082
083 /**
084 * Verifies that the actual <code>{@link Comparable}</code> is greater than the given one.
085 * @param other the given value.
086 * @return this assertion object.
087 * @throws AssertionError if the actual {@code Comparable} is {@code null}.
088 * @throws AssertionError if the actual {@code Comparable} is not greater than the given one.
089 */
090 public final S isGreaterThan(T other) {
091 isNotNull();
092 if (actual.compareTo(other) > 0) return myself;
093 failIfCustomMessageIsSet();
094 throw failure(unexpectedLessThanOrEqualTo(actual, other));
095 }
096
097 /**
098 * Verifies that the actual <code>{@link Comparable}</code> is less than or equal to the given one.
099 * @param other the given value.
100 * @return this assertion object.
101 * @throws AssertionError if the actual {@code Comparable} is {@code null}.
102 * @throws AssertionError if the actual {@code Comparable} is not less than or equal to the given one.
103 */
104 public final S isLessThanOrEqualTo(T other) {
105 isNotNull();
106 if (actual.compareTo(other) <= 0) return myself;
107 failIfCustomMessageIsSet();
108 throw failure(unexpectedGreaterThan(actual, other));
109 }
110
111 /**
112 * Verifies that the actual <code>{@link Comparable}</code> is greater than or equal to the given one.
113 * @param other the given value.
114 * @return this assertion object.
115 * @throws AssertionError if the actual {@code Comparable} is {@code null}.
116 * @throws AssertionError if the actual {@code Comparable} is not greater than or equal to the given one.
117 */
118 public final S isGreaterThanOrEqualTo(T other) {
119 isNotNull();
120 if (actual.compareTo(other) >= 0) return myself;
121 failIfCustomMessageIsSet();
122 throw failure(unexpectedLessThan(actual, other));
123 }
124 }