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

de.tsl2.nano.bean.def.MapValue Maven / Gradle / Ivy

Go to download

TSL2 Framework Descriptor (currency-handling, generic formatter, descriptors for beans, collections, actions and values)

There is a newer version: 2.5.1
Show newest version
/*
 * File: $HeadURL$
 * Id  : $Id$
 * 
 * created by: Tom
 * created on: 27.02.2014
 * 
 * Copyright: (c) Thomas Schneider 2014, all rights reserved
 */
package de.tsl2.nano.bean.def;

import java.util.Map;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;

import de.tsl2.nano.core.cls.IDefaultAttribute;
import de.tsl2.nano.core.cls.IValueAccess;
import de.tsl2.nano.core.messaging.EventController;

/**
 * Object[] value.
 * 
 * @author Tom
 * @version $Revision$
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class MapValue implements IValueAccess, IDefaultAttribute {
    /** serialVersionUID */
    private static final long serialVersionUID = -1460468414949211876L;

    transient Map map;
    @Attribute
    Class type;
    @Element
    Object name;
    transient EventController eventController;

    protected MapValue() {
    }

    public MapValue(Object name) {
        this(name, null, null);
    }

    /**
     * constructor
     * 
     * @param object
     * @param type
     */
    public MapValue(Object name, Class type, Map map) {
        this.name = name;
        this.type = (Class) (type != null ? type : map != null && map.get(name) != null ? map.get(name).getClass() : Object.class);
        this.map = map;
    }

    @Override
    public Class getDeclaringClass() {
        return map.getClass();
    }

    @Override
    public String getName() {
        return name.toString();
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public T getValue(Object instance) {
        return (T) (instance != null ? ((Map) instance).get(name) : map.get(name));
    }

    @Override
    public void setValue(Object instance, T value) {
        if (instance != null) {
            ((Map) instance).put(name, value);
        } else {
            map.put(name, value);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return getId().hashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        return hashCode() == obj.hashCode();
    }

    @Override
    public String toString() {
        return getId();
    }

    @Override
    public T getValue() {
        return getValue(map);
    }

    @Override
    public void setValue(T value) {
        setValue(map, value);
    }

    @Override
    public Class getType() {
        return type;
    }

    public void setMap(Map map) {
        this.map = map;
    }
    
    @Override
    public EventController changeHandler() {
        if (eventController == null) {
            eventController = new EventController();
        }
        return eventController;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy