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.internal;
016
017 import java.util.Comparator;
018
019 import org.fest.assertions.core.ArraySortedAssert;
020 import org.fest.assertions.core.AssertionInfo;
021 import org.fest.assertions.data.Index;
022 import org.fest.util.ComparisonStrategy;
023 import org.fest.util.StandardComparisonStrategy;
024 import org.fest.util.VisibleForTesting;
025
026 /**
027 * Reusable assertions for arrays of {@code long}s.
028 *
029 * @author Alex Ruiz
030 * @author Joel Costigliola
031 */
032 public class LongArrays {
033
034 private static final LongArrays INSTANCE = new LongArrays();
035
036 /**
037 * Returns the singleton instance of this class.
038 * @return the singleton instance of this class.
039 */
040 public static LongArrays instance() {
041 return INSTANCE;
042 }
043
044 private Arrays arrays = Arrays.instance();
045
046 @VisibleForTesting Failures failures = Failures.instance();
047
048 @VisibleForTesting LongArrays() {
049 this(StandardComparisonStrategy.instance());
050 }
051
052 public LongArrays(ComparisonStrategy comparisonStrategy) {
053 this.arrays = new Arrays(comparisonStrategy);
054 }
055
056 @VisibleForTesting
057 public Comparator<?> getComparator() {
058 return arrays.getComparator();
059 }
060
061 /**
062 * Asserts that the given array is {@code null} or empty.
063 * @param info contains information about the assertion.
064 * @param actual the given array.
065 * @throws AssertionError if the given array is not {@code null} *and* contains one or more elements.
066 */
067 public void assertNullOrEmpty(AssertionInfo info, long[] actual) {
068 arrays.assertNullOrEmpty(info, failures, actual);
069 }
070
071 /**
072 * Asserts that the given array is empty.
073 * @param info contains information about the assertion.
074 * @param actual the given array.
075 * @throws AssertionError if the given array is {@code null}.
076 * @throws AssertionError if the given array is not empty.
077 */
078 public void assertEmpty(AssertionInfo info, long[] actual) {
079 arrays.assertEmpty(info, failures, actual);
080 }
081
082 /**
083 * Asserts that the given array is not empty.
084 * @param info contains information about the assertion.
085 * @param actual the given array.
086 * @throws AssertionError if the given array is {@code null}.
087 * @throws AssertionError if the given array is empty.
088 */
089 public void assertNotEmpty(AssertionInfo info, long[] actual) {
090 arrays.assertNotEmpty(info, failures, actual);
091 }
092
093 /**
094 * Asserts that the number of elements in the given array is equal to the expected one.
095 * @param info contains information about the assertion.
096 * @param actual the given array.
097 * @param expectedSize the expected size of {@code actual}.
098 * @throws AssertionError if the given array is {@code null}.
099 * @throws AssertionError if the number of elements in the given array is different than the expected one.
100 */
101 public void assertHasSize(AssertionInfo info, long[] actual, int expectedSize) {
102 arrays.assertHasSize(info, failures, actual, expectedSize);
103 }
104
105 /**
106 * Asserts that the given array contains the given values, in any order.
107 * @param info contains information about the assertion.
108 * @param actual the given array.
109 * @param values the values that are expected to be in the given array.
110 * @throws NullPointerException if the array of values is {@code null}.
111 * @throws IllegalArgumentException if the array of values is empty.
112 * @throws AssertionError if the given array is {@code null}.
113 * @throws AssertionError if the given array does not contain the given values.
114 */
115 public void assertContains(AssertionInfo info, long[] actual, long[] values) {
116 arrays.assertContains(info, failures, actual, values);
117 }
118
119 /**
120 * Verifies that the given array contains the given value at the given index.
121 * @param info contains information about the assertion.
122 * @param actual the given array.
123 * @param value the value to look for.
124 * @param index the index where the value should be stored in the given array.
125 * @throws AssertionError if the given array is {@code null} or empty.
126 * @throws NullPointerException if the given {@code Index} is {@code null}.
127 * @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or greater than the size of
128 * the given array.
129 * @throws AssertionError if the given array does not contain the given value at the given index.
130 */
131 public void assertContains(AssertionInfo info, long[] actual, long value, Index index) {
132 arrays.assertContains(info, failures, actual, value, index);
133 }
134
135 /**
136 * Verifies that the given array does not contain the given value at the given index.
137 * @param info contains information about the assertion.
138 * @param actual the given array.
139 * @param value the value to look for.
140 * @param index the index where the value should be stored in the given array.
141 * @throws AssertionError if the given array is {@code null}.
142 * @throws NullPointerException if the given {@code Index} is {@code null}.
143 * @throws AssertionError if the given array contains the given value at the given index.
144 */
145 public void assertDoesNotContain(AssertionInfo info, long[] actual, long value, Index index) {
146 arrays.assertDoesNotContain(info, failures, actual, value, index);
147 }
148
149 /**
150 * Asserts that the given array contains only the given values and nothing else, in any order.
151 * @param info contains information about the assertion.
152 * @param actual the given array.
153 * @param values the values that are expected to be in the given array.
154 * @throws NullPointerException if the array of values is {@code null}.
155 * @throws IllegalArgumentException if the array of values is empty.
156 * @throws AssertionError if the given array is {@code null}.
157 * @throws AssertionError if the given array does not contain the given values or if the given
158 * array contains values that are not in the given array.
159 */
160 public void assertContainsOnly(AssertionInfo info, long[] actual, long[] values) {
161 arrays.assertContainsOnly(info, failures, actual, values);
162 }
163
164 /**
165 * Verifies that the given array contains the given sequence of values, without any other values between them.
166 * @param info contains information about the assertion.
167 * @param actual the given array.
168 * @param sequence the sequence of values to look for.
169 * @throws AssertionError if the given array is {@code null}.
170 * @throws NullPointerException if the given sequence is {@code null}.
171 * @throws IllegalArgumentException if the given sequence is empty.
172 * @throws AssertionError if the given array does not contain the given sequence of values.
173 */
174 public void assertContainsSequence(AssertionInfo info, long[] actual, long[] sequence) {
175 arrays.assertContainsSequence(info, failures, actual, sequence);
176 }
177
178 /**
179 * Asserts that the given array does not contain the given values.
180 * @param info contains information about the assertion.
181 * @param actual the given array.
182 * @param values the values that are expected not to be in the given array.
183 * @throws NullPointerException if the array of values is {@code null}.
184 * @throws IllegalArgumentException if the array of values is empty.
185 * @throws AssertionError if the given array is {@code null}.
186 * @throws AssertionError if the given array contains any of given values.
187 */
188 public void assertDoesNotContain(AssertionInfo info, long[] actual, long[] values) {
189 arrays.assertDoesNotContain(info, failures, actual, values);
190 }
191
192 /**
193 * Asserts that the given array does not have duplicate values.
194 * @param info contains information about the assertion.
195 * @param actual the given array.
196 * @throws NullPointerException if the array of values is {@code null}.
197 * @throws IllegalArgumentException if the array of values is empty.
198 * @throws AssertionError if the given array is {@code null}.
199 * @throws AssertionError if the given array contains duplicate values.
200 */
201 public void assertDoesNotHaveDuplicates(AssertionInfo info, long[] actual) {
202 arrays.assertDoesNotHaveDuplicates(info, failures, actual);
203 }
204
205 /**
206 * Verifies that the given array starts with the given sequence of values, without any other values between them.
207 * Similar to <code>{@link #assertContainsSequence(AssertionInfo, long[], long[])}</code>, but it also verifies
208 * that the first element in the sequence is also the first element of the given array.
209 * @param info contains information about the assertion.
210 * @param actual the given array.
211 * @param sequence the sequence of values to look for.
212 * @throws NullPointerException if the given argument is {@code null}.
213 * @throws IllegalArgumentException if the given argument is an empty array.
214 * @throws AssertionError if the given array is {@code null}.
215 * @throws AssertionError if the given array does not start with the given sequence of values.
216 */
217 public void assertStartsWith(AssertionInfo info, long[] actual, long[] sequence) {
218 arrays.assertStartsWith(info, failures, actual, sequence);
219 }
220
221 /**
222 * Verifies that the given array ends with the given sequence of values, without any other values between them.
223 * Similar to <code>{@link #assertContainsSequence(AssertionInfo, long[], long[])}</code>, but it also verifies
224 * that the last element in the sequence is also the last element of the given array.
225 * @param info contains information about the assertion.
226 * @param actual the given array.
227 * @param sequence the sequence of values to look for.
228 * @throws NullPointerException if the given argument is {@code null}.
229 * @throws IllegalArgumentException if the given argument is an empty array.
230 * @throws AssertionError if the given array is {@code null}.
231 * @throws AssertionError if the given array does not end with the given sequence of values.
232 */
233 public void assertEndsWith(AssertionInfo info, long[] actual, long[] sequence) {
234 arrays.assertEndsWith(info, failures, actual, sequence);
235 }
236
237 /**
238 * Concrete implementation of {@link ArraySortedAssert#isSorted()}.
239 *
240 * @param info contains information about the assertion.
241 * @param actual the given array.
242 */
243 public void assertIsSorted(AssertionInfo info, long[] actual) {
244 arrays.assertIsSorted(info, failures, actual);
245 }
246
247 /**
248 * Concrete implementation of {@link ArraySortedAssert#isSortedAccordingTo(Comparator)}.
249 *
250 * @param info contains information about the assertion.
251 * @param actual the given array.
252 * @param comparator the {@link Comparator} used to compare array elements
253 */
254 public void assertIsSortedAccordingToComparator(AssertionInfo info, long[] actual,
255 Comparator<? extends Long> comparator) {
256 Arrays.assertIsSortedAccordingToComparator(info, failures, actual, comparator);
257 }
258 }