001    /*
002     * Created on Feb 16, 2008
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 @2008-2011 the original author or authors.
014     */
015    package org.fest.assertions;
016    
017    import static java.lang.Double.*;
018    import static java.lang.Math.abs;
019    import static org.fest.assertions.ErrorMessages.*;
020    import static org.fest.assertions.Formatting.format;
021    
022    /**
023     * Assertions for {@code Double}s and {@code double}s.
024     * <p>
025     * To create a new instance of this class invoke <code>{@link Assertions#assertThat(Double)}</code> or
026     * <code>{@link Assertions#assertThat(double)}</code>.
027     * </p>
028     *
029     * @author Yvonne Wang
030     * @author David DIDIER
031     * @author Alex Ruiz
032     * @author Ansgar Konermann
033     */
034    public class DoubleAssert extends GenericAssert<DoubleAssert, Double> implements NumberAssert {
035    
036      private static final double ZERO = 0.0;
037    
038      /**
039       * Creates a new <code>{@link DoubleAssert}</code>.
040       * @param actual the actual value to verify.
041       */
042      protected DoubleAssert(double actual) {
043        super(DoubleAssert.class, actual);
044      }
045    
046      /**
047       * Creates a new <code>{@link DoubleAssert}</code>.
048       * @param actual the actual value to verify.
049       */
050      protected DoubleAssert(Double actual) {
051        super(DoubleAssert.class, actual);
052      }
053    
054      /**
055       * Verifies that the actual {@code Double} is equal to the given one.
056       * @param expected the value to compare the actual one to.
057       * @return this assertion object.
058       * @throws AssertionError if the actual {@code Double} is not equal to the given one.
059       */
060      public DoubleAssert isEqualTo(double expected) {
061        return isEqualTo(valueOf(expected));
062      }
063    
064      /**
065       * Verifies that the actual {@code Double} is equal to the given one, within a positive delta.
066       * @param expected the value to compare the actual one to.
067       * @param delta the given delta.
068       * @return this assertion object.
069       * @throws AssertionError if the actual {@code Double} is not equal to the given one.
070       * @deprecated use method <code>{@link #isEqualTo(double, org.fest.assertions.Delta)}</code> instead. This method will
071       * be removed in version 2.0.
072       */
073      @Deprecated
074      public DoubleAssert isEqualTo(double expected, Delta delta) {
075        return isEqualTo(expected, delta.value);
076      }
077    
078      /**
079       * Verifies that the actual {@code Double} is equal to the given one, within a positive delta.
080       * @param expected the value to compare the actual one to.
081       * @param delta the given delta.
082       * @return this assertion object.
083       * @throws AssertionError if the actual {@code Double} is not equal to the given one.
084       * @since 1.1
085       */
086      public DoubleAssert isEqualTo(double expected, org.fest.assertions.Delta delta) {
087        return isEqualTo(expected, delta.doubleValue());
088      }
089    
090      private DoubleAssert isEqualTo(double expected, double deltaValue) {
091        return isEqualTo(valueOf(expected), deltaValue);
092      }
093    
094      /**
095       * Verifies that the actual {@code Double} is equal to the given one, within a positive delta.
096       * @param expected the value to compare the actual one to.
097       * @param delta the given delta.
098       * @return this assertion object.
099       * @throws AssertionError if the actual {@code Double} is not equal to the given one.
100       * @since 1.3
101       */
102      public DoubleAssert isEqualTo(Double expected, org.fest.assertions.Delta delta) {
103        return isEqualTo(expected, delta.doubleValue());
104      }
105    
106      private DoubleAssert isEqualTo(Double expected, double deltaValue) {
107        if (actual == null || expected == null) return isEqualTo(expected);
108        if (actual.compareTo(expected) == 0) return this;
109        if (abs(expected - actual) <= deltaValue) return this;
110        failIfCustomMessageIsSet();
111        throw failure(unexpectedNotEqual(actual, expected) + format(" using delta:<%s>", deltaValue));
112      }
113    
114      /**
115       * Verifies that the actual {@code Double} is not equal to the given one.
116       * @param other the given value.
117       * @return this assertion object.
118       * @throws AssertionError if the actual {@code Double} is equal to the given one.
119       */
120      public DoubleAssert isNotEqualTo(double other) {
121        if (compareTo(other) != 0) return this;
122        failIfCustomMessageIsSet();
123        throw failure(unexpectedEqual(actual, other));
124      }
125    
126      /**
127       * Verifies that the actual {@code Double} is greater than the given one.
128       * @param other the given value.
129       * @return this assertion object.
130       * @throws AssertionError if the actual {@code Double} is not greater than the given one.
131       */
132      public DoubleAssert isGreaterThan(double other) {
133        if (compareTo(other) > 0) return this;
134        failIfCustomMessageIsSet();
135        throw failure(unexpectedLessThanOrEqualTo(actual, other));
136      }
137    
138      /**
139       * Verifies that the actual {@code Double} is less than the given one.
140       * @param other the given value.
141       * @return this assertion object.
142       * @throws AssertionError if the actual {@code Double} is not less than the given one.
143       */
144      public DoubleAssert isLessThan(double other) {
145        if (compareTo(other) < 0) return this;
146        failIfCustomMessageIsSet();
147        throw failure(unexpectedGreaterThanOrEqualTo(actual, other));
148      }
149    
150      /**
151       * Verifies that the actual {@code Double} is greater or equal to the given one.
152       * @param other the given value.
153       * @return this assertion object.
154       * @throws AssertionError if the actual {@code Double} is not greater than or equal to the given one.
155       */
156      public DoubleAssert isGreaterThanOrEqualTo(double other) {
157        if (compareTo(other) >= 0) return this;
158        failIfCustomMessageIsSet();
159        throw failure(unexpectedLessThan(actual, other));
160      }
161    
162      /**
163       * Verifies that the actual {@code Double} is less or equal to the given one.
164       * @param other the given value.
165       * @return this assertion object.
166       * @throws AssertionError if the actual {@code Double} is not less than or equal to the given one.
167       */
168      public DoubleAssert isLessThanOrEqualTo(double other) {
169        if (compareTo(other) <= 0) return this;
170        failIfCustomMessageIsSet();
171        throw failure(unexpectedGreaterThan(actual, other));
172      }
173    
174      private int compareTo(double other) {
175        return compare(actual, other);
176      }
177    
178      /**
179       * Verifies that the actual {@code Double} is equal to zero.
180       * @return this assertion object.
181       * @throws AssertionError if the actual {@code Double} is not equal to zero.
182       */
183      public DoubleAssert isZero() {
184        return isEqualTo(ZERO);
185      }
186    
187      /**
188       * Verifies that the actual {@code Double} is positive.
189       * @return this assertion object.
190       * @throws AssertionError if the actual {@code Double} is not positive.
191       */
192      public DoubleAssert isPositive() {
193        return isGreaterThan(ZERO);
194      }
195    
196      /**
197       * Verifies that the actual {@code Double} is negative.
198       * @return this assertion object.
199       * @throws AssertionError if the actual {@code Double} is not negative.
200       */
201      public DoubleAssert isNegative() {
202        return isLessThan(ZERO);
203      }
204    
205      /**
206       * Verifies that the actual {@code Double} is equal to <code>{@link Double#NaN}</code>.
207       * @return this assertion object.
208       * @throws AssertionError if the actual {@code Double} is not equal to <code>NAN</code>.
209       */
210      public DoubleAssert isNaN() {
211        return isEqualTo(Double.NaN);
212      }
213    
214    
215      /**
216       * Creates a new holder for a delta value to be used in <code>{@link DoubleAssert#isEqualTo(double,
217       * org.fest.assertions.DoubleAssert.Delta)}</code>.
218       * @param d the delta value.
219       * @return a new delta value holder.
220       * @deprecated use method <code>{@link org.fest.assertions.Delta#delta(double)}</code> instead. This method will be
221       * removed in version 2.0.
222       */
223      @Deprecated
224      public static Delta delta(double d) {
225        return new Delta(d);
226      }
227    
228      /**
229       * Holds a delta value to be used in <code>{@link DoubleAssert#isEqualTo(double,
230       * org.fest.assertions.DoubleAssert.Delta)}</code>.
231       * @deprecated use top-level class <code>{@link org.fest.assertions.Delta}</code> instead. This class will be removed
232       * in version 2.0.
233       */
234      @Deprecated
235      public static class Delta {
236        final double value;
237    
238        private Delta(double value) {
239          this.value = value;
240        }
241      }
242    }