All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.omnifaces.el.functions.Arrays Maven / Gradle / Ivy

There is a newer version: 4.4.1
Show newest version
/*
 * 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;

/**
 * 

* Collection of EL functions for array manipulation: of:createArray(), of:createIntegerArray(), * and of:contains(). * * @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 n times * over an <ui:repeat>, which doesn't support EL in begin and end * 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 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 <f:selectItems> 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 true if the string representation of an item of the given array equals to the string * representation of the given item. This returns false if either the array or the item is null. This * is useful if you want to for example check if #{paramValues.foo} contains a certain value. * @param array The array whose items have to be compared. * @param item The item to be compared. * @return true 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; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy