forked from assertj/assertj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrays.java
More file actions
141 lines (127 loc) · 4.64 KB
/
Arrays.java
File metadata and controls
141 lines (127 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2014 the original author or authors.
*/
package org.assertj.core.util;
import org.assertj.core.presentation.Representation;
import org.assertj.core.presentation.StandardRepresentation;
import static java.util.Collections.emptyList;
import static org.assertj.core.util.Preconditions.checkNotNull;
import java.util.*;
/**
* Utility methods related to arrays.
*
* @author Alex Ruiz
* @author Joel Costigliola
*/
public class Arrays {
private static final ArrayFormatter FORMATTER = new ArrayFormatter();
private static final StandardRepresentation STANDARD_REPRESENTATION = new StandardRepresentation();
/**
* Indicates whether the given object is not {@code null} and is an array.
*
* @param o the given object.
* @return {@code true} if the given object is not {@code null} and is an array, otherwise {@code false}.
*/
public static boolean isArray(Object o) {
return o != null && o.getClass().isArray();
}
/**
* Indicates whether the given array is {@code null} or empty.
*
* @param <T> the type of elements of the array.
* @param array the array to check.
* @return {@code true} if the given array is {@code null} or empty, otherwise {@code false}.
*/
public static <T> boolean isNullOrEmpty(T[] array) {
return array == null || !hasElements(array);
}
/**
* Returns an array containing the given arguments.
*
* @param <T> the type of the array to return.
* @param values the values to store in the array.
* @return an array containing the given arguments.
*/
@SafeVarargs
public static <T> T[] array(T... values) {
return values;
}
/**
* Returns the {@code String} representation of the given array, or {@code null} if the given object is either
* {@code null} or not an array. This method supports arrays having other arrays as elements.
*
* @param representation
* @param array the object that is expected to be an array.
* @return the {@code String} representation of the given array.
*/
public static String format(Representation representation, Object array) {
return FORMATTER.format(representation, array);
}
/**
* Returns the {@code String} {@link org.assertj.core.presentation.StandardRepresentation standard representation} of
* the given array, or {@code null} if the given object is either {@code null} or not an array.
* This method supports arrays having other arrays as elements.
*
* @param array the object that is expected to be an array.
* @return the {@code String} standard representation of the given array.
*/
public static Object format(final Object array) {
return format(STANDARD_REPRESENTATION, array);
}
/**
* Returns all the non-{@code null} elements in the given array.
*
* @param <T> the type of elements of the array.
* @param array the given array.
* @return all the non-{@code null} elements in the given array. An empty list is returned if the given array is
* {@code null}.
* @since 1.1.3
*/
public static <T> List<T> nonNullElementsIn(T[] array) {
if (array == null) {
return emptyList();
}
List<T> nonNullElements = new ArrayList<>();
for (T o : array) {
if (o != null) {
nonNullElements.add(o);
}
}
return nonNullElements;
}
/**
* Returns {@code true} if the given array has only {@code null} elements, {@code false} otherwise. If given array is
* empty, this method returns {@code true}.
*
* @param <T> the type of elements of the array.
* @param array the given array. <b>It must not be null</b>.
* @return {@code true} if the given array has only {@code null} elements or is empty, {@code false} otherwise.
* @throws NullPointerException if the given array is {@code null}.
* @since 1.1.3
*/
public static <T> boolean hasOnlyNullElements(T[] array) {
checkNotNull(array);
if (!hasElements(array)) {
return false;
}
for (T o : array) {
if (o != null) {
return false;
}
}
return true;
}
private static <T> boolean hasElements(T[] array) {
return array.length > 0;
}
private Arrays() {}
}