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