001    /*
002     * Created on Oct 25, 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.FloatingPointNumberAssert;
020    import org.fest.assertions.data.Offset;
021    import org.fest.assertions.internal.Doubles;
022    import org.fest.util.ComparatorBasedComparisonStrategy;
023    import org.fest.util.VisibleForTesting;
024    
025    /**
026     * Assertion methods for doubles.
027     * <p>
028     * To create an instance of this class, invoke <code>{@link Assertions#assertThat(Double)}</code> or
029     * <code>{@link Assertions#assertThat(double)}</code>.
030     * </p>
031     * 
032     * @author Yvonne Wang
033     * @author David DIDIER
034     * @author Alex Ruiz
035     * @author Ansgar Konermann
036     * @author Joel Costigliola
037     */
038    public class DoubleAssert extends AbstractComparableAssert<DoubleAssert, Double> implements
039        FloatingPointNumberAssert<Double> {
040    
041      @VisibleForTesting
042      Doubles doubles = Doubles.instance();
043    
044      protected DoubleAssert(Double actual) {
045        super(actual, DoubleAssert.class);
046      }
047    
048      /** {@inheritDoc} */
049      public DoubleAssert isNaN() {
050        doubles.assertIsNaN(info, actual);
051        return this;
052      }
053    
054      /** {@inheritDoc} */
055      public DoubleAssert isNotNaN() {
056        doubles.assertIsNotNaN(info, actual);
057        return this;
058      }
059    
060      /** {@inheritDoc} */
061      public DoubleAssert isZero() {
062        doubles.assertIsZero(info, actual);
063        return this;
064      }
065    
066      /** {@inheritDoc} */
067      public DoubleAssert isNotZero() {
068        doubles.assertIsNotZero(info, actual);
069        return this;
070      }
071    
072      /** {@inheritDoc} */
073      public DoubleAssert isPositive() {
074        doubles.assertIsPositive(info, actual);
075        return this;
076      }
077    
078      /** {@inheritDoc} */
079      public DoubleAssert isNegative() {
080        doubles.assertIsNegative(info, actual);
081        return this;
082      }
083    
084      /**
085       * Verifies that the actual value is equal to the given one.
086       * @param expected the given value to compare the actual value to.
087       * @return {@code this} assertion object.
088       * @throws AssertionError if the actual value is {@code null}.
089       * @throws AssertionError if the actual value is not equal to the given one.
090       */
091      public DoubleAssert isEqualTo(double expected) {
092        doubles.assertEqual(info, actual, expected);
093        return this;
094      }
095    
096      /** {@inheritDoc} */
097      public DoubleAssert isEqualTo(Double expected, Offset<Double> offset) {
098        doubles.assertEqual(info, actual, expected, offset);
099        return this;
100      }
101    
102      /**
103       * Verifies that the actual value is equal to the given one, within a positive offset.
104       * @param expected the given value to compare the actual value to.
105       * @param offset the given positive offset.
106       * @return {@code this} assertion object.
107       * @throws NullPointerException if the given offset is {@code null}.
108       * @throws AssertionError if the actual value is {@code null}.
109       * @throws AssertionError if the actual value is not equal to the given one.
110       */
111      public DoubleAssert isEqualTo(double expected, Offset<Double> offset) {
112        doubles.assertEqual(info, actual, expected, offset);
113        return this;
114      }
115    
116      /**
117       * Verifies that the actual value is not equal to the given one.
118       * @param other the given value to compare the actual value to.
119       * @return {@code this} assertion object.
120       * @throws AssertionError if the actual value is {@code null}.
121       * @throws AssertionError if the actual value is equal to the given one.
122       */
123      public DoubleAssert isNotEqualTo(double other) {
124        doubles.assertNotEqual(info, actual, other);
125        return this;
126      }
127    
128      /**
129       * Verifies that the actual value is less than the given one.
130       * @param other the given value to compare the actual value to.
131       * @return {@code this} assertion object.
132       * @throws AssertionError if the actual value is {@code null}.
133       * @throws AssertionError if the actual value is equal to or greater than the given one.
134       */
135      public DoubleAssert isLessThan(double other) {
136        doubles.assertLessThan(info, actual, other);
137        return this;
138      }
139    
140      /**
141       * Verifies that the actual value is less than or equal to the given one.
142       * @param other the given value to compare the actual value to.
143       * @return {@code this} assertion object.
144       * @throws AssertionError if the actual value is {@code null}.
145       * @throws AssertionError if the actual value is greater than the given one.
146       */
147      public DoubleAssert isLessThanOrEqualTo(double other) {
148        doubles.assertLessThanOrEqualTo(info, actual, other);
149        return this;
150      }
151    
152      /**
153       * Verifies that the actual value is greater than the given one.
154       * @param other the given value to compare the actual value to.
155       * @return {@code this} assertion object.
156       * @throws AssertionError if the actual value is {@code null}.
157       * @throws AssertionError if the actual value is equal to or less than the given one.
158       */
159      public DoubleAssert isGreaterThan(double other) {
160        doubles.assertGreaterThan(info, actual, other);
161        return this;
162      }
163    
164      /**
165       * Verifies that the actual value is greater than or equal to the given one.
166       * @param other the given value to compare the actual value to.
167       * @return {@code this} assertion object.
168       * @throws AssertionError if the actual value is {@code null}.
169       * @throws AssertionError if the actual value is less than the given one.
170       */
171      public DoubleAssert isGreaterThanOrEqualTo(double other) {
172        doubles.assertGreaterThanOrEqualTo(info, actual, other);
173        return this;
174      }
175    
176      @Override
177      public DoubleAssert usingComparator(Comparator<?> customComparator) {
178        super.usingComparator(customComparator);
179        this.doubles = new Doubles(new ComparatorBasedComparisonStrategy(customComparator));
180        return myself;
181      }
182    
183      @Override
184      public DoubleAssert usingDefaultComparator() {
185        super.usingDefaultComparator();
186        this.doubles = Doubles.instance();
187        return myself;
188      }
189    }