001    /*
002     * Created on Dec 20, 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.*;
020    import org.fest.assertions.data.Index;
021    import org.fest.assertions.internal.DoubleArrays;
022    import org.fest.util.*;
023    
024    /**
025     * Assertion methods for arrays of {@code double}s.
026     * <p>
027     * To create an instance of this class, invoke <code>{@link Assertions#assertThat(double[])}</code>.
028     * </p>
029     * 
030     * @author Yvonne Wang
031     * @author Alex Ruiz
032     * @author Joel Costigliola
033     */
034    public class DoubleArrayAssert extends AbstractAssert<DoubleArrayAssert, double[]> implements
035        EnumerableAssert<DoubleArrayAssert>, ArraySortedAssert<DoubleArrayAssert, Double> {
036    
037      @VisibleForTesting
038      DoubleArrays arrays = DoubleArrays.instance();
039    
040      protected DoubleArrayAssert(double[] actual) {
041        super(actual, DoubleArrayAssert.class);
042      }
043    
044      /** {@inheritDoc} */
045      public void isNullOrEmpty() {
046        arrays.assertNullOrEmpty(info, actual);
047      }
048    
049      /** {@inheritDoc} */
050      public void isEmpty() {
051        arrays.assertEmpty(info, actual);
052      }
053    
054      /** {@inheritDoc} */
055      public DoubleArrayAssert isNotEmpty() {
056        arrays.assertNotEmpty(info, actual);
057        return this;
058      }
059    
060      /** {@inheritDoc} */
061      public DoubleArrayAssert hasSize(int expected) {
062        arrays.assertHasSize(info, actual, expected);
063        return this;
064      }
065    
066      /**
067       * Verifies that the actual array contains the given values, in any order.
068       * @param values the given values.
069       * @return {@code this} assertion object.
070       * @throws NullPointerException if the given argument is {@code null}.
071       * @throws IllegalArgumentException if the given argument is an empty array.
072       * @throws AssertionError if the actual array is {@code null}.
073       * @throws AssertionError if the actual array does not contain the given values.
074       */
075      public DoubleArrayAssert contains(double... values) {
076        arrays.assertContains(info, actual, values);
077        return this;
078      }
079    
080      /**
081       * Verifies that the actual array contains only the given values and nothing else, in any order.
082       * @param values the given values.
083       * @return {@code this} assertion object.
084       * @throws NullPointerException if the given argument is {@code null}.
085       * @throws IllegalArgumentException if the given argument is an empty array.
086       * @throws AssertionError if the actual array is {@code null}.
087       * @throws AssertionError if the actual array does not contain the given values, i.e. the actual array contains some
088       *           or none of the given values, or the actual array contains more values than the given ones.
089       */
090      public DoubleArrayAssert containsOnly(double... values) {
091        arrays.assertContainsOnly(info, actual, values);
092        return this;
093      }
094    
095      /**
096       * Verifies that the actual array contains the given sequence, without any other values between them.
097       * @param sequence the sequence of values to look for.
098       * @return this assertion object.
099       * @throws AssertionError if the actual array is {@code null}.
100       * @throws AssertionError if the given array is {@code null}.
101       * @throws AssertionError if the actual array does not contain the given sequence.
102       */
103      public DoubleArrayAssert containsSequence(double... sequence) {
104        arrays.assertContainsSequence(info, actual, sequence);
105        return this;
106      }
107    
108      /**
109       * Verifies that the actual array contains the given value at the given index.
110       * @param value the value to look for.
111       * @param index the index where the value should be stored in the actual array.
112       * @return this assertion object.
113       * @throws AssertionError if the actual array is {@code null} or empty.
114       * @throws NullPointerException if the given {@code Index} is {@code null}.
115       * @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or greater than the size of
116       *           the actual array.
117       * @throws AssertionError if the actual array does not contain the given value at the given index.
118       */
119      public DoubleArrayAssert contains(double value, Index index) {
120        arrays.assertContains(info, actual, value, index);
121        return this;
122      }
123    
124      /**
125       * Verifies that the actual array does not contain the given values.
126       * @param values the given values.
127       * @return {@code this} assertion object.
128       * @throws NullPointerException if the given argument is {@code null}.
129       * @throws IllegalArgumentException if the given argument is an empty array.
130       * @throws AssertionError if the actual array is {@code null}.
131       * @throws AssertionError if the actual array contains any of the given values.
132       */
133      public DoubleArrayAssert doesNotContain(double... values) {
134        arrays.assertDoesNotContain(info, actual, values);
135        return this;
136      }
137    
138      /**
139       * Verifies that the actual array does not contain the given value at the given index.
140       * @param value the value to look for.
141       * @param index the index where the value should be stored in the actual array.
142       * @return this assertion object.
143       * @throws AssertionError if the actual array is {@code null}.
144       * @throws NullPointerException if the given {@code Index} is {@code null}.
145       * @throws AssertionError if the actual array contains the given value at the given index.
146       */
147      public DoubleArrayAssert doesNotContain(double value, Index index) {
148        arrays.assertDoesNotContain(info, actual, value, index);
149        return this;
150      }
151    
152      /**
153       * Verifies that the actual array does not contain duplicates.
154       * @return {@code this} assertion object.
155       * @throws AssertionError if the actual array is {@code null}.
156       * @throws AssertionError if the actual array contains duplicates.
157       */
158      public DoubleArrayAssert doesNotHaveDuplicates() {
159        arrays.assertDoesNotHaveDuplicates(info, actual);
160        return this;
161      }
162    
163      /**
164       * Verifies that the actual array starts with the given sequence of values, without any other values between them.
165       * Similar to <code>{@link #containsSequence(double...)}</code>, but it also verifies that the first element in the
166       * sequence is also first element of the actual array.
167       * @param sequence the sequence of values to look for.
168       * @return this assertion object.
169       * @throws NullPointerException if the given argument is {@code null}.
170       * @throws IllegalArgumentException if the given argument is an empty array.
171       * @throws AssertionError if the actual array is {@code null}.
172       * @throws AssertionError if the actual array does not start with the given sequence.
173       */
174      public DoubleArrayAssert startsWith(double... sequence) {
175        arrays.assertStartsWith(info, actual, sequence);
176        return this;
177      }
178    
179      /**
180       * Verifies that the actual array ends with the given sequence of values, without any other values between them.
181       * Similar to <code>{@link #containsSequence(double...)}</code>, but it also verifies that the last element in the
182       * sequence is also last element of the actual array.
183       * @param sequence the sequence of values to look for.
184       * @return this assertion object.
185       * @throws NullPointerException if the given argument is {@code null}.
186       * @throws IllegalArgumentException if the given argument is an empty array.
187       * @throws AssertionError if the actual array is {@code null}.
188       * @throws AssertionError if the actual array does not end with the given sequence.
189       */
190      public DoubleArrayAssert endsWith(double... sequence) {
191        arrays.assertEndsWith(info, actual, sequence);
192        return this;
193      }
194    
195      /** {@inheritDoc} */
196      public DoubleArrayAssert isSorted() {
197        arrays.assertIsSorted(info, actual);
198        return this;
199      }
200    
201      /** {@inheritDoc} */
202      public DoubleArrayAssert isSortedAccordingTo(Comparator<? extends Double> comparator) {
203        arrays.assertIsSortedAccordingToComparator(info, actual, comparator);
204        return this;
205      }
206      
207      @Override
208      public DoubleArrayAssert usingComparator(Comparator<?> customComparator) {
209        super.usingComparator(customComparator);
210        this.arrays = new DoubleArrays(new ComparatorBasedComparisonStrategy(customComparator));
211        return myself;
212      }
213      
214      @Override
215      public DoubleArrayAssert usingDefaultComparator() {
216        super.usingDefaultComparator();
217        this.arrays = DoubleArrays.instance();
218        return myself;
219      }
220    }