Example usage for org.apache.commons.collections FastHashMap put

List of usage examples for org.apache.commons.collections FastHashMap put

Introduction

In this page you can find the example usage for org.apache.commons.collections FastHashMap put.

Prototype

public Object put(Object key, Object value) 

Source Link

Document

Associate the specified value with the specified key in this map.

Usage

From source file:jp.terasoluna.fw.validation.springmodules.SpringValidationErrorsTest.java

/**
 * testAddErrors01() <br>/* ww  w  .ja  v a  2s. c  om*/
 * <br>
 * () <br>
 * C <br>
 * <br>
 * () bean:Object<br>
 * () field:Field<br>
 * field.getKey()="key"<br>
 * field.getMsg("name")="messageKey"<br>
 * field.getArg("name", 0)="arg0"<br>
 * field.getArg("name", 1)="arg1"<br>
 * field.getArg("name", 2)="arg2"<br>
 * field.getArg("name", 3)="arg3"<br>
 * () va:ValidationAction<br>
 * va.getName()="name"<br>
 * <br>
 * () rejectValue():??????<br>
 * fieldCode="key"<br>
 * errorCode="messageKey"<br>
 * args={<br>
 * MessageSourceResolvable{<br>
 * codes[0]={"arg0"}, arguments=null, defaultMessage="arg0"}, <br>
 * MessageSourceResolvable{<br>
 * codes[1]={"arg1"}, arguments=null, defaultMessage="arg1"}, <br>
 * MessageSourceResolvable{<br>
 * codes[2]={"arg2"}, arguments=null, defaultMessage="arg2"}, <br>
 * MessageSourceResolvable{<br>
 * codes[3]={"arg3"}, arguments=null, defaultMessage="arg3"}, <br>
 * }<br>
 * <br>
 * ?not null?? <br>
 * @throws Exception ?????
 */
@Test
public void testAddErrors01() throws Exception {
    // ??
    // bean
    Object bean = new Object();
    // field
    Field field = new Field();
    // field.getKey() : "key"
    field.setKey("key");

    // field.getMsg("name")?errorCode????
    FastHashMap hMsgs = new FastHashMap();
    Msg msg = new Msg();
    msg.setKey("messageKey");
    hMsgs.put("name", msg);
    ReflectionTestUtils.setField(field, "hMsgs", hMsgs);

    // Object[] args????
    @SuppressWarnings("rawtypes")
    Map[] args = new HashMap[4];

    // args[0]
    Arg arg = new Arg();
    arg.setKey("arg0");
    Map<String, Arg> hMap01 = new HashMap<String, Arg>();
    hMap01.put("name", arg);
    args[0] = hMap01;

    // args[1]
    arg = new Arg();
    arg.setKey("arg1");
    Map<String, Arg> hMap02 = new HashMap<String, Arg>();
    hMap02.put("name", arg);
    args[1] = hMap02;

    // args[2]
    arg = new Arg();
    arg.setKey("arg2");
    Map<String, Arg> hMap03 = new HashMap<String, Arg>();
    hMap03.put("name", arg);
    args[2] = hMap03;

    // args[3]
    arg = new Arg();
    arg.setKey("arg3");
    Map<String, Arg> hMap04 = new HashMap<String, Arg>();
    hMap04.put("name", arg);
    args[3] = hMap04;

    ReflectionTestUtils.setField(field, "args", args);

    // va
    ValidatorAction va = new ValidatorAction();

    // va.getName : "name"
    va.setName("name");

    // SpringValidationErrors?
    SpringValidationErrors validation = new SpringValidationErrors();

    // Errors? : ErrorsImpl01 - rejectValue???
    ErrorsImpl01 errors = new ErrorsImpl01();
    ReflectionTestUtils.setField(validation, "errors", errors);

    // 
    validation.addError(bean, field, va);

    // 
    ErrorsImpl01 assertErrors = (ErrorsImpl01) ReflectionTestUtils.getField(validation, "errors");
    // rejectValue?
    assertTrue(assertErrors.isRejectValue);

    // field?
    assertEquals("key", assertErrors.field);

    // errorCode?
    assertEquals("messageKey", assertErrors.errorCode);

    // assertSame(args, assertErrors.errorArgs);
    // errorArgs?
    Object[] objs = assertErrors.errorArgs;
    MessageSourceResolvable msr = null;
    for (int i = 0; i < objs.length; i++) {
        msr = (MessageSourceResolvable) objs[i];

        String[] strs = msr.getCodes();
        // codes[0] : "arg" + i
        assertEquals("arg" + i, strs[0]);
        // arguments : null
        assertNull(msr.getArguments());
        // defaultMessage : "arg" + i
        assertEquals("arg" + i, msr.getDefaultMessage());
    }

    // defaultMessage?
    assertEquals("messageKey", assertErrors.defaultMessage);
}

From source file:eu.qualityontime.commons.QPropertyUtilsBean.java

/**
 * <p>/* w  w  w . j a va  2  s. co  m*/
 * Retrieve the property descriptor for the specified property of the
 * specified bean, or return <code>null</code> if there is no such
 * descriptor. This method resolves indexed and nested property references
 * in the same manner as other methods in this class, except that if the
 * last (or only) name element is indexed, the descriptor for the last
 * resolved property itself is returned.
 * </p>
 *
 * <p>
 * <strong>FIXME</strong> - Does not work with DynaBeans.
 * </p>
 *
 * <p>
 * Note that for Java 8 and above, this method no longer return
 * IndexedPropertyDescriptor for {@link List}-typed properties, only for
 * properties typed as native array. (BEANUTILS-492).
 *
 * @param bean
 *            Bean for which a property descriptor is requested
 * @param name
 *            Possibly indexed and/or nested name of the property for which
 *            a property descriptor is requested
 * @return the property descriptor
 *
 * @throws IllegalAccessException
 *             if the caller does not have access to the property accessor
 *             method
 * @throws IllegalArgumentException
 *             if <code>bean</code> or <code>name</code> is null
 * @throws IllegalArgumentException
 *             if a nested reference to a property returns null
 * @throws InvocationTargetException
 *             if the property accessor method throws an exception
 * @throws NoSuchMethodException
 *             if an accessor method for this propety cannot be found
 */
public PropertyDescriptor getPropertyDescriptor(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (bean == null) {
        throw new IllegalArgumentException("No bean specified");
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified for bean class '" + bean.getClass() + "'");
    }

    // Resolve nested references
    while (resolver.hasNested(name)) {
        final String next = resolver.next(name);
        final Object nestedBean = getProperty(bean, next);
        if (nestedBean == null) {
            throw new NestedNullException(
                    "Null property value for '" + next + "' on bean class '" + bean.getClass() + "'");
        }
        bean = nestedBean;
        name = resolver.remove(name);
    }

    // Remove any subscript from the final name value
    name = resolver.getProperty(name);

    // Look up and return this property from our cache
    // creating and adding it to the cache if not found.
    if (name == null) {
        return null;
    }

    final BeanIntrospectionData data = getIntrospectionData(bean.getClass());
    PropertyDescriptor result = data.getDescriptor(name);
    if (result != null) {
        return result;
    }

    FastHashMap mappedDescriptors = getMappedPropertyDescriptors(bean);
    if (mappedDescriptors == null) {
        mappedDescriptors = new FastHashMap();
        mappedDescriptors.setFast(true);
        mappedDescriptorsCache.put(bean.getClass(), mappedDescriptors);
    }
    result = (PropertyDescriptor) mappedDescriptors.get(name);
    if (result == null) {
        // not found, try to create it
        try {
            result = new MappedPropertyDescriptor(name, bean.getClass());
        } catch (final IntrospectionException ie) {
            /*
             * Swallow IntrospectionException TODO: Why?
             */
        }
        if (result != null) {
            mappedDescriptors.put(name, result);
        }
    }

    return result;

}

From source file:org.kuali.rice.kns.web.struts.form.pojo.PojoPropertyUtilsBean.java

/**
 * <p>//  w w  w  . ja v  a2s.c o m
 * Retrieve the property descriptor for the specified property of the specified bean, or return <code>null</code> if there is
 * no such descriptor. This method resolves indexed and nested property references in the same manner as other methods in this
 * class, except that if the last (or only) name element is indexed, the descriptor for the last resolved property itself is
 * returned.
 * </p>
 *
 * <p>
 * <strong>FIXME </strong>- Does not work with DynaBeans.
 * </p>
 *
 * @param bean Bean for which a property descriptor is requested
 * @param name Possibly indexed and/or nested name of the property for which a property descriptor is requested
 *
 * @exception IllegalAccessException if the caller does not have access to the property accessor method
 * @exception IllegalArgumentException if <code>bean</code> or <code>name</code> is null
 * @exception IllegalArgumentException if a nested reference to a property returns null
 * @exception InvocationTargetException if the property accessor method throws an exception
 * @exception NoSuchMethodException if an accessor method for this propety cannot be found
 */
public PropertyDescriptor getPropertyDescriptor(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (bean == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("No bean specified, name = " + name);
        return null;
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified");
    }
    try {
        // Resolve nested references
        Object propBean = null;
        while (true) {
            int delim = findNextNestedIndex(name);
            //int delim = name.indexOf(PropertyUtils.NESTED_DELIM);
            if (delim < 0) {
                break;
            }
            String next = name.substring(0, delim);
            int indexOfINDEXED_DELIM = next.indexOf(PropertyUtils.INDEXED_DELIM);
            int indexOfMAPPED_DELIM = next.indexOf(PropertyUtils.MAPPED_DELIM);
            if (indexOfMAPPED_DELIM >= 0
                    && (indexOfINDEXED_DELIM < 0 || indexOfMAPPED_DELIM < indexOfINDEXED_DELIM)) {
                propBean = getMappedProperty(bean, next);
            } else {
                if (indexOfINDEXED_DELIM >= 0) {
                    propBean = getIndexedProperty(bean, next);
                } else {
                    propBean = getSimpleProperty(bean, next);
                }
            }
            if (ObjectUtils.isNull(propBean)) {
                Class propertyType = getPropertyType(bean, next);
                if (propertyType != null) {
                    Object newInstance = ObjectUtils.createNewObjectFromClass(propertyType);
                    setSimpleProperty(bean, next, newInstance);
                    propBean = getSimpleProperty(bean, next);
                }
            }
            bean = propBean;
            name = name.substring(delim + 1);
        }

        // Remove any subscript from the final name value
        int left = name.indexOf(PropertyUtils.INDEXED_DELIM);
        if (left >= 0) {
            name = name.substring(0, left);
        }
        left = name.indexOf(PropertyUtils.MAPPED_DELIM);
        if (left >= 0) {
            name = name.substring(0, left);
        }

        // Look up and return this property from our cache
        // creating and adding it to the cache if not found.
        if ((bean == null) || (name == null)) {
            return (null);
        }

        PropertyDescriptor descriptors[] = getPropertyDescriptors(bean);
        if (descriptors != null) {

            for (int i = 0; i < descriptors.length; i++) {
                if (name.equals(descriptors[i].getName()))
                    return (descriptors[i]);
            }
        }

        PropertyDescriptor result = null;
        FastHashMap mappedDescriptors = getMappedPropertyDescriptors(bean);
        if (mappedDescriptors == null) {
            mappedDescriptors = new FastHashMap();
            mappedDescriptors.setFast(true);
        }
        result = (PropertyDescriptor) mappedDescriptors.get(name);
        if (result == null) {
            // not found, try to create it
            try {
                result = new MappedPropertyDescriptor(name, bean.getClass());
            } catch (IntrospectionException ie) {
            }
            if (result != null) {
                mappedDescriptors.put(name, result);
            }
        }

        return result;
    } catch (RuntimeException ex) {
        LOG.error("Unable to get property descriptor for " + bean.getClass().getName() + " . " + name + "\n"
                + ex.getClass().getName() + ": " + ex.getMessage());
        throw ex;
    }
}