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

br.com.anteros.bean.validation.util.KeyedAccess Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
/*******************************************************************************
 * Copyright 2012 Anteros Tecnologia
 *  
 * 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 br.com.anteros.bean.validation.util;

import java.lang.annotation.ElementType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Map;

import br.com.anteros.core.utils.TypeUtils;

/**
 * {@link AccessStrategy} to get a keyed value from a {@link Map}. Contains
 * special handling when a string key is used against a container type whose key
 * parameter is not assignable from {@link String}: against a map whose key type
 * is an enum class, it will be interpreted as a named enum constant; other key
 * types will be compared via {@link Object#toString()}.
 */
public class KeyedAccess extends AccessStrategy {
    private static final TypeVariable[] MAP_TYPEVARS = Map.class.getTypeParameters();

    /**
     * Get the Java element type of a particular container type.
     * 
     * @param containerType
     * @return Type or null if containerType is not
     *         some kind of {@link Map}
     */
    public static Type getJavaElementType(Type containerType) {
        if (TypeUtils.isAssignable(containerType, Map.class)) {
            Map, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Map.class);
            return typeArguments.containsKey(MAP_TYPEVARS[1]) ? typeArguments.get(MAP_TYPEVARS[1]) : Object.class;
        }
        return null;
    }

    private Type containerType;
    private Object key;

    /**
     * Create a new KeyedAccess instance.
     * 
     * @param containerType
     * @param key
     */
    public KeyedAccess(Type containerType, Object key) {
        super();
        this.containerType = containerType;
        this.key = key;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object get(Object instance) {
        if (instance instanceof Map) {
            Map map = (Map) instance;
            Map, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Map.class);
            Type keyType = typeArguments.get(MAP_TYPEVARS[0]);
            if (key == null || keyType == null || TypeUtils.isInstance(key, keyType)) {
                return map.get(key);
            }
            if (key instanceof String) {
                String name = (String) key;
                Class rawKeyType = TypeUtils.getRawType(keyType, containerType);
                if (rawKeyType.isEnum()) {
                    @SuppressWarnings({ "unchecked", "rawtypes" })
                    final Object result = map.get(Enum.valueOf((Class) rawKeyType, name));
                    return result;
                }
                for (Map.Entry e : map.entrySet()) {
                    if (name.equals(e.getKey())) {
                        return e.getValue();
                    }
                }
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ElementType getElementType() {
        return ElementType.METHOD;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Type getJavaType() {
        Type result = getJavaElementType(containerType);
        return result == null ? Object.class : result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getPropertyName() {
        return String.format("[%s]", key);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy