Example usage for org.apache.commons.collections CollectionUtils get

List of usage examples for org.apache.commons.collections CollectionUtils get

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils get.

Prototype

public static Object get(Object object, int index) 

Source Link

Document

Returns the index-th value in object, throwing IndexOutOfBoundsException if there is no such element or IllegalArgumentException if object is not an instance of one of the supported types.

Usage

From source file:org.kuali.rice.kew.impl.document.search.FormFields.java

/**
 * Sets a Field value appropriately, depending on whether it is a "multi-value" field type
 *//* w w w.  j  a v a 2  s. co m*/
void setFieldValue(Field field, String[] values) {
    if (!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) {
        field.setPropertyValue(CollectionUtils.get(values, 0));
    } else {
        //multi value, set to values
        field.setPropertyValues(values);
    }
}

From source file:org.nuxeo.ecm.webapp.filemanager.FileManageActionsBean.java

@SuppressWarnings({ "rawtypes" })
public void performAction(ActionEvent event) {
    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext eContext = context.getExternalContext();
    String index = eContext.getRequestParameterMap().get("index");

    try {//from   w  w w  . j a va  2s  .  c om
        DocumentModel current = navigationContext.getCurrentDocument();
        if (!current.hasSchema(FILES_SCHEMA)) {
            return;
        }
        ArrayList files = (ArrayList) current.getPropertyValue(FILES_PROPERTY);
        Object file = CollectionUtils.get(files, Integer.valueOf(index).intValue());
        files.remove(file);
        current.setPropertyValue(FILES_PROPERTY, files);
        documentActions.updateDocument(current, Boolean.TRUE);
    } catch (IndexOutOfBoundsException | NuxeoException e) {
        log.error(e, e);
        throw e;
    }
}

From source file:org.projectforge.common.BeanHelper.java

/**
 * Invokes getter method of the given bean and returns the idx element of array or collection. Use-age: "user[3]".
 * @param bean//  w  w  w  .j  a v  a  2 s  .com
 * @param property Must be from format "xxx[#]"
 * @return
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
public static Object getIndexedProperty(final Object bean, final String property) {
    final int pos = property.indexOf('[');
    if (pos <= 0) {
        throw new UnsupportedOperationException(
                "'" + property + "' is not an indexed property, such as 'xxx[#]'.");
    }
    final String prop = property.substring(0, pos);
    final String indexString = property.substring(pos + 1, property.length() - 1);
    final Integer index = NumberHelper.parseInteger(indexString);
    if (index == null) {
        throw new UnsupportedOperationException(
                "'" + property + "' contains no number as index string: '" + indexString + "'.");
    }
    final Object value = getProperty(bean, prop);
    if (value == null) {
        return null;
    }
    if (value instanceof Collection<?> == true) {
        CollectionUtils.get(value, index);
    } else if (value.getClass().isArray() == true) {
        return Array.get(value, index);
    }
    throw new UnsupportedOperationException(
            "Collection or array from type '" + value.getClass() + "' not yet supported: '" + property + "'.");
}

From source file:org.sonar.batch.DecoratorsSelectorTest.java

@Test
public void decoratorsShouldBeExecutedBeforeFormulas() {
    Project project = new Project("key");
    Decorator metric1Decorator = new Metric1Decorator();
    BatchExtensionDictionnary dictionnary = newDictionnary(metric1Decorator, withFormula1);

    Collection<Decorator> decorators = new DecoratorsSelector(dictionnary).select(project);

    Decorator firstDecorator = (Decorator) CollectionUtils.get(decorators, 0);
    Decorator secondDecorator = (Decorator) CollectionUtils.get(decorators, 1);

    assertThat(firstDecorator, is(Metric1Decorator.class));
    assertThat(secondDecorator, is(FormulaDecorator.class));

    FormulaDecorator formulaDecorator = (FormulaDecorator) secondDecorator;
    assertThat(formulaDecorator.dependsUponDecorators().size(), is(1));
    assertThat(CollectionUtils.get(formulaDecorator.dependsUponDecorators(), 0), is((Object) firstDecorator));
}

From source file:org.structr.core.parser.ArrayExpression.java

@Override
public Object transform(final ActionContext ctx, final GraphObject entity, final Object value)
        throws FrameworkException {

    if (value == null) {
        return null;
    }//from   w ww . j a va  2  s  .  c  o  m

    final Integer index = (Integer) evaluate(ctx, entity);
    if (index != null) {

        if (value instanceof Collection || value.getClass().isArray()) {

            try {

                // silently ignore array index errors
                return CollectionUtils.get(value, index);

            } catch (ArrayIndexOutOfBoundsException ignore) {
            }

        } else {

            throw new FrameworkException(422,
                    "Invalid expression: expected collection, found " + value.getClass().getSimpleName() + ".");
        }

    } else {

        throw new FrameworkException(422, "Invalid expression: invalid array index: null.");
    }

    return null;
}