Example usage for org.springframework.util ObjectUtils toObjectArray

List of usage examples for org.springframework.util ObjectUtils toObjectArray

Introduction

In this page you can find the example usage for org.springframework.util ObjectUtils toObjectArray.

Prototype

public static Object[] toObjectArray(@Nullable Object source) 

Source Link

Document

Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects).

Usage

From source file:Main.java

/**
 * Convert the supplied array into a List. A primitive array gets
 * converted into a List of the appropriate wrapper type.
 * <p>A <code>null</code> source value will be converted to an
 * empty List./* w w  w . jav  a  2 s.  c  o m*/
 * @param source the (potentially primitive) array
 * @return the converted List result
 * @see org.springframework.util.ObjectUtils#toObjectArray(Object)
 */
public static List arrayToList(Object source) {
    return Arrays.asList(ObjectUtils.toObjectArray(source));
}

From source file:Main.java

/**
 * Merge the given array into the given Collection.
 *
 * @param array      the array to merge (may be {@code null})
 * @param collection the target Collection to merge the array into
 *//*from  www.  ja  va  2s .  c o m*/
@SuppressWarnings("unchecked")
public static <E> void mergeArrayIntoCollection(Object array, Collection<E> collection) {
    if (collection == null) {
        throw new IllegalArgumentException("Collection must not be null");
    }
    Object[] arr = ObjectUtils.toObjectArray(array);
    for (Object elem : arr) {
        collection.add((E) elem);
    }
}

From source file:org.jdal.ui.bind.ControlBindingErrorProcessor.java

/** 
 * Add a ControlError instead FieldError to hold component that has failed.
 * @param control/*  www. j a va2  s  . c  om*/
 * @param ex
 * @param bindingResult
 */
public void processPropertyAccessException(Object control, PropertyAccessException ex,
        BindingResult bindingResult) {
    // Create field error with the exceptions's code, e.g. "typeMismatch".
    String field = ex.getPropertyName();
    String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
    Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
    Object rejectedValue = ex.getValue();
    if (rejectedValue != null && rejectedValue.getClass().isArray()) {
        rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue));
    }
    bindingResult.addError(new ControlError(control, bindingResult.getObjectName(), field, rejectedValue, true,
            codes, arguments, ex.getLocalizedMessage()));
}

From source file:org.crazydog.util.spring.CollectionUtils.java

/**
 * Convert the supplied array into a List. A primitive array gets converted
 * into a List of the appropriate wrapper type.
 * <p><b>NOTE:</b> Generally prefer the standard {@link Arrays#asList} method.
 * This {@code arrayToList} method is just meant to deal with an incoming Object
 * value that might be an {@code Object[]} or a primitive array at runtime.
 * <p>A {@code null} source value will be converted to an empty List.
 *
 * @param source the (potentially primitive) array
 * @return the converted List result//from w  w w  . j av  a  2  s . co  m
 * @see ObjectUtils#toObjectArray(Object)
 * @see Arrays#asList(Object[])
 */
@SuppressWarnings("rawtypes")
public static List arrayToList(Object source) {
    return Arrays.asList(ObjectUtils.toObjectArray(source));
}

From source file:se.softhouse.garden.orchid.spring.tags.OrchidArgTag.java

/**
 * Resolve the given arguments Object into an arguments array.
 * /*from www.  j  a  v  a  2 s  .c  o m*/
 * @param arguments
 *            the specified arguments Object
 * @return the resolved arguments as array
 * @throws JspException
 *             if argument conversion failed
 * @see #setArguments
 */
protected Object resolveArgument(String argument) throws JspException {
    Object resolvedArgument = ExpressionEvaluationUtils.evaluate("argument", argument, this.pageContext);
    if (resolvedArgument != null && resolvedArgument.getClass().isArray()) {
        return ObjectUtils.toObjectArray(argument);
    }
    return argument;
}

From source file:de.whs.poodle.repositories.McWorksheetRepository.java

public int createMcWorksheet(CreateMcWorksheetForm form, int studentId, int courseTermId) {
    return jdbc.query(con -> {
        PreparedStatement ps = con.prepareStatement("SELECT * FROM generate_student_mc_worksheet(?,?,?,?,?)");
        ps.setInt(1, courseTermId);/*from w  ww. j  ava2s  . co m*/
        ps.setInt(2, studentId);

        Array tagsArray = con.createArrayOf("int4", ObjectUtils.toObjectArray(form.getTags()));
        ps.setArray(3, tagsArray);

        ps.setInt(4, form.getMaximum());
        ps.setBoolean(5, form.isIgnoreAlreadyAnswered());

        return ps;
    }, new ResultSetExtractor<Integer>() {

        @Override
        public Integer extractData(ResultSet rs) throws SQLException, DataAccessException {
            if (!rs.next()) // no results -> generated worksheet had no questions
                return 0;

            return rs.getInt("id");
        }
    });
}

From source file:org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQueryCreator.java

protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID> criteria, Part part,
        Iterator<Object> iterator) {
    if (part.shouldIgnoreCase().equals(IgnoreCaseType.ALWAYS))
        throw new UnsupportedOperationException("Case insensitivity not supported");

    Class<?> leafNodePropertyType = part.getProperty().getLeafProperty().getType();

    PropertyPath leafNodePropertyPath = part.getProperty().getLeafProperty();
    String leafNodePropertyName = leafNodePropertyPath.toDotPath();
    if (leafNodePropertyName.indexOf(".") != -1) {
        int index = leafNodePropertyName.lastIndexOf(".");
        leafNodePropertyName = leafNodePropertyName.substring(index);
    }//from www . j a va  2  s  . co  m

    switch (part.getType()) {

    case IN:
        Object in = iterator.next();
        Assert.notNull(in, "Creating conditions on null parameters not supported: please specify a value for '"
                + leafNodePropertyName + "'");
        boolean isIterable = ClassUtils.isAssignable(in.getClass(), Iterable.class);
        boolean isArray = ObjectUtils.isArray(in);
        Assert.isTrue(isIterable || isArray, "In criteria can only operate with Iterable or Array parameters");
        Iterable<?> iterable = isIterable ? ((Iterable<?>) in) : Arrays.asList(ObjectUtils.toObjectArray(in));
        return criteria.withPropertyIn(leafNodePropertyName, iterable, leafNodePropertyType);
    case CONTAINING:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.CONTAINS,
                iterator.next(), leafNodePropertyType);
    case STARTING_WITH:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.BEGINS_WITH,
                iterator.next(), leafNodePropertyType);
    case BETWEEN:
        Object first = iterator.next();
        Object second = iterator.next();
        return criteria.withPropertyBetween(leafNodePropertyName, first, second, leafNodePropertyType);
    case AFTER:
    case GREATER_THAN:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GT, iterator.next(),
                leafNodePropertyType);
    case BEFORE:
    case LESS_THAN:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LT, iterator.next(),
                leafNodePropertyType);
    case GREATER_THAN_EQUAL:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GE, iterator.next(),
                leafNodePropertyType);
    case LESS_THAN_EQUAL:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LE, iterator.next(),
                leafNodePropertyType);
    case IS_NULL:
        return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NULL);
    case IS_NOT_NULL:
        return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NOT_NULL);
    case TRUE:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.TRUE,
                leafNodePropertyType);
    case FALSE:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.FALSE,
                leafNodePropertyType);
    case SIMPLE_PROPERTY:
        return criteria.withPropertyEquals(leafNodePropertyName, iterator.next(), leafNodePropertyType);
    case NEGATING_SIMPLE_PROPERTY:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.NE, iterator.next(),
                leafNodePropertyType);
    default:
        throw new IllegalArgumentException("Unsupported keyword " + part.getType());
    }

}

From source file:de.whs.poodle.repositories.McWorksheetRepository.java

public int getCountForMcWorksheet(CreateMcWorksheetForm form, int studentId, int courseTermId) {
    return jdbc.query(con -> {
        PreparedStatement ps = con
                .prepareStatement("SELECT COUNT(*) AS count FROM get_mc_questions_for_worksheet(?,?,?,?,?)");
        ps.setInt(1, courseTermId);/*from   w ww  .  j av  a 2 s.  c om*/
        ps.setInt(2, studentId);

        Array tagsArray = con.createArrayOf("int4", ObjectUtils.toObjectArray(form.getTags()));
        ps.setArray(3, tagsArray);

        ps.setInt(4, form.getMaximum());
        ps.setBoolean(5, form.isIgnoreAlreadyAnswered());
        return ps;
    }, new ResultSetExtractor<Integer>() {

        @Override
        public Integer extractData(ResultSet rs) throws SQLException, DataAccessException {
            rs.next();
            return rs.getInt("count");
        }

    });
}

From source file:org.synyx.hera.si.PluginRegistryAwareMessageHandler.java

/**
 * Returns the actual arguments to be used for the plugin method invocation. Will apply the configured invocation
 * argument expression to the given {@link Message}.
 * //w  ww  . java 2  s. c  o  m
 * @param message
 * @return
 */
private Object[] getInvocationArguments(Message<?> message) {

    if (invocationArgumentsExpression == null) {
        return new Object[] { message };
    }

    StandardEvaluationContext context = new StandardEvaluationContext(message);
    Object result = delimiterExpression.getValue(context);

    return ObjectUtils.isArray(result) ? ObjectUtils.toObjectArray(result) : new Object[] { result };
}

From source file:com.ei.itop.common.tag.MessageTag.java

/**
 * Resolve the given arguments Object into an arguments array.
 * @param arguments the specified arguments Object
 * @return the resolved arguments as array
 * @throws JspException if argument conversion failed
 * @see #setArguments//from w  w  w.j  ava2 s  .  c  o m
 */
protected Object[] resolveArguments(Object arguments) throws JspException {
    if (arguments instanceof String) {
        String[] stringArray = StringUtils.delimitedListToStringArray((String) arguments,
                this.argumentSeparator);
        if (stringArray.length == 1) {
            Object argument = ExpressionEvaluationUtils.evaluate("argument", stringArray[0], pageContext);
            if (argument != null && argument.getClass().isArray()) {
                return ObjectUtils.toObjectArray(argument);
            } else {
                return new Object[] { argument };
            }
        } else {
            Object[] argumentsArray = new Object[stringArray.length];
            for (int i = 0; i < stringArray.length; i++) {
                argumentsArray[i] = ExpressionEvaluationUtils.evaluate("argument[" + i + "]", stringArray[i],
                        pageContext);
            }
            return argumentsArray;
        }
    } else if (arguments instanceof Object[]) {
        return (Object[]) arguments;
    } else if (arguments instanceof Collection) {
        return ((Collection) arguments).toArray();
    } else if (arguments != null) {
        // Assume a single argument object.
        return new Object[] { arguments };
    } else {
        return null;
    }
}