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 * @author Mikhail Mazursky
038 * @author Nicolas François
039 */
040 public class DoubleAssert extends AbstractComparableAssert<DoubleAssert, Double> implements
041 FloatingPointNumberAssert<Double> {
042
043 @VisibleForTesting
044 Doubles doubles = Doubles.instance();
045
046 protected DoubleAssert(Double actual) {
047 super(actual, DoubleAssert.class);
048 }
049
050 /** {@inheritDoc} */
051 public DoubleAssert isNaN() {
052 doubles.assertIsNaN(info, actual);
053 return this;
054 }
055
056 /** {@inheritDoc} */
057 public DoubleAssert isNotNaN() {
058 doubles.assertIsNotNaN(info, actual);
059 return this;
060 }
061
062 /** {@inheritDoc} */
063 public DoubleAssert isZero() {
064 doubles.assertIsZero(info, actual);
065 return this;
066 }
067
068 /** {@inheritDoc} */
069 public DoubleAssert isNotZero() {
070 doubles.assertIsNotZero(info, actual);
071 return this;
072 }
073
074 /** {@inheritDoc} */
075 public DoubleAssert isPositive() {
076 doubles.assertIsPositive(info, actual);
077 return this;
078 }
079
080 /** {@inheritDoc} */
081 public DoubleAssert isNegative() {
082 doubles.assertIsNegative(info, actual);
083 return this;
084 }
085
086 /** {@inheritDoc} */
087 public DoubleAssert isNotNegative(){
088 doubles.assertIsNotNegative(info, actual);
089 return this;
090 }
091
092 /** {@inheritDoc} */
093 public DoubleAssert isNotPositive(){
094 doubles.assertIsNotPositive(info, actual);
095 return this;
096 }
097
098 /**
099 * Verifies that the actual value is equal to the given one.
100 * @param expected the given value to compare the actual value to.
101 * @return {@code this} assertion object.
102 * @throws AssertionError if the actual value is {@code null}.
103 * @throws AssertionError if the actual value is not equal to the given one.
104 */
105 public DoubleAssert isEqualTo(double expected) {
106 doubles.assertEqual(info, actual, expected);
107 return this;
108 }
109
110 /** {@inheritDoc} */
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 equal to the given one, within a positive offset.
118 * @param expected the given value to compare the actual value to.
119 * @param offset the given positive offset.
120 * @return {@code this} assertion object.
121 * @throws NullPointerException if the given offset is {@code null}.
122 * @throws AssertionError if the actual value is {@code null}.
123 * @throws AssertionError if the actual value is not equal to the given one.
124 */
125 public DoubleAssert isEqualTo(double expected, Offset<Double> offset) {
126 doubles.assertEqual(info, actual, expected, offset);
127 return this;
128 }
129
130 /**
131 * Verifies that the actual value is not equal to the given one.
132 * @param other the given value to compare the actual value to.
133 * @return {@code this} assertion object.
134 * @throws AssertionError if the actual value is {@code null}.
135 * @throws AssertionError if the actual value is equal to the given one.
136 */
137 public DoubleAssert isNotEqualTo(double other) {
138 doubles.assertNotEqual(info, actual, other);
139 return this;
140 }
141
142 /**
143 * Verifies that the actual value is less than the given one.
144 * @param other the given value to compare the actual value to.
145 * @return {@code this} assertion object.
146 * @throws AssertionError if the actual value is {@code null}.
147 * @throws AssertionError if the actual value is equal to or greater than the given one.
148 */
149 public DoubleAssert isLessThan(double other) {
150 doubles.assertLessThan(info, actual, other);
151 return this;
152 }
153
154 /**
155 * Verifies that the actual value is less than or equal to the given one.
156 * @param other the given value to compare the actual value to.
157 * @return {@code this} assertion object.
158 * @throws AssertionError if the actual value is {@code null}.
159 * @throws AssertionError if the actual value is greater than the given one.
160 */
161 public DoubleAssert isLessThanOrEqualTo(double other) {
162 doubles.assertLessThanOrEqualTo(info, actual, other);
163 return this;
164 }
165
166 /**
167 * Verifies that the actual value is greater than the given one.
168 * @param other the given value to compare the actual value to.
169 * @return {@code this} assertion object.
170 * @throws AssertionError if the actual value is {@code null}.
171 * @throws AssertionError if the actual value is equal to or less than the given one.
172 */
173 public DoubleAssert isGreaterThan(double other) {
174 doubles.assertGreaterThan(info, actual, other);
175 return this;
176 }
177
178 /**
179 * Verifies that the actual value is greater than or equal to the given one.
180 * @param other the given value to compare the actual value to.
181 * @return {@code this} assertion object.
182 * @throws AssertionError if the actual value is {@code null}.
183 * @throws AssertionError if the actual value is less than the given one.
184 */
185 public DoubleAssert isGreaterThanOrEqualTo(double other) {
186 doubles.assertGreaterThanOrEqualTo(info, actual, other);
187 return this;
188 }
189
190 @Override
191 public DoubleAssert usingComparator(Comparator<? super Double> customComparator) {
192 super.usingComparator(customComparator);
193 this.doubles = new Doubles(new ComparatorBasedComparisonStrategy(customComparator));
194 return myself;
195 }
196
197 @Override
198 public DoubleAssert usingDefaultComparator() {
199 super.usingDefaultComparator();
200 this.doubles = Doubles.instance();
201 return myself;
202 }
203 }