/
Arrays.java
96 lines (81 loc) · 3.62 KB
/
Arrays.java
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
/*
* Copyright 2012 OmniFaces.
*
* 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.
*/
package org.omnifaces.el.functions;
/**
* <p>
* Collection of EL functions for array manipulation: <code>of:createArray()</code>, <code>of:createIntegerArray()</code>,
* and <code>of:contains()</code>.
*
* @author Bauke Scholtz
*/
public final class Arrays {
// Constants ------------------------------------------------------------------------------------------------------
private static final String ERROR_INVALID_ARRAY_SIZE = "The array size must be at least 0.";
// Constructors ---------------------------------------------------------------------------------------------------
private Arrays() {
// Hide constructor.
}
// Utility --------------------------------------------------------------------------------------------------------
/**
* Creates and returns a dummy object array of the given size. This is useful if you want to iterate <i>n</i> times
* over an <code><ui:repeat></code>, which doesn't support EL in <code>begin</code> and <code>end</code>
* attributes.
* @param size The size of the dummy object array.
* @return A dummy object array of the given size.
* @throws IllegalArgumentException When the size is less than 0.
*/
public static Object[] createArray(int size) {
if (size < 0) {
throw new IllegalArgumentException(ERROR_INVALID_ARRAY_SIZE);
}
return new Object[size];
}
/**
* Creates and returns an integer array which starts at the given integer and ends at the given integer, inclusive.
* This is useful if you want to for example populate a <code><f:selectItems></code> which shows an integer
* range to represent days and years. If the begin is greater than end, then the array will be decremental. If the
* begin equals end, then the array will contain only one item.
* @param begin The begin integer.
* @param end The end integer.
* @return An integer array which starts at the given integer and ends at the given integer, inclusive
*/
public static Integer[] createIntegerArray(int begin, int end) {
int direction = (begin < end) ? 1 : (begin > end) ? -1 : 0;
int size = Math.abs(end - begin) + 1;
Integer[] array = new Integer[size];
for (int i = 0; i < size; i++) {
array[i] = begin + (i * direction);
}
return array;
}
/**
* Returns <code>true</code> if the string representation of an item of the given array equals to the string
* representation of the given item. This returns <code>false</code> if either the array or the item is null. This
* is useful if you want to for example check if <code>#{paramValues.foo}</code> contains a certain value.
* @param array The array whose items have to be compared.
* @param item The item to be compared.
* @return <code>true</code> if the string representation of an item of the given array equals to the string
* representation of the given item.
*/
public static boolean contains(Object[] array, Object item) {
if (array == null || item == null) {
return false;
}
for (Object object : array) {
if (object != null && object.toString().equals(item.toString())) {
return true;
}
}
return false;
}
}