001 /*
002 * Created on Nov 18, 2010
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 @2010-2011 the original author or authors.
014 */
015 package org.fest.assertions.api;
016
017 import java.util.Comparator;
018
019 import org.fest.assertions.core.ComparableAssert;
020 import org.fest.assertions.internal.Comparables;
021 import org.fest.util.ComparatorBasedComparisonStrategy;
022 import org.fest.util.VisibleForTesting;
023
024 /**
025 * Base class for all implementations of <code>{@link ComparableAssert}</code>.
026 * @param <S> the "self" type of this assertion class. Please read "<a href="http://bit.ly/anMa4g"
027 * target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>"
028 * for more details.
029 * @param <A> the type of the "actual" value.
030 *
031 * @author Alex Ruiz
032 * @author Mikhail Mazursky
033 */
034 public abstract class AbstractComparableAssert<S extends AbstractComparableAssert<S, A>, A extends Comparable<? super A>>
035 extends AbstractAssert<S, A> implements ComparableAssert<S, A> {
036
037 @VisibleForTesting
038 Comparables comparables = Comparables.instance();
039
040 protected AbstractComparableAssert(A actual, Class<?> selfType) {
041 super(actual, selfType);
042 }
043
044 /** {@inheritDoc} */
045 public final S isLessThan(A other) {
046 comparables.assertLessThan(info, actual, other);
047 return myself;
048 }
049
050 /** {@inheritDoc} */
051 public final S isLessThanOrEqualTo(A other) {
052 comparables.assertLessThanOrEqualTo(info, actual, other);
053 return myself;
054 }
055
056 /** {@inheritDoc} */
057 public final S isGreaterThan(A other) {
058 comparables.assertGreaterThan(info, actual, other);
059 return myself;
060 }
061
062 /** {@inheritDoc} */
063 public final S isGreaterThanOrEqualTo(A other) {
064 comparables.assertGreaterThanOrEqualTo(info, actual, other);
065 return myself;
066 }
067
068 @Override
069 public S usingComparator(Comparator<?> customComparator) {
070 super.usingComparator(customComparator);
071 this.comparables = new Comparables(new ComparatorBasedComparisonStrategy(customComparator));
072 return myself;
073 }
074
075 @Override
076 public S usingDefaultComparator() {
077 super.usingDefaultComparator();
078 this.comparables = Comparables.instance();
079 return myself;
080 }
081 }