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

de.tsl2.nano.bean.def.ArrayValue 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 org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Default;
import org.simpleframework.xml.DefaultType;
import org.simpleframework.xml.Element;

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

/**
 * Object[] value.
 * 
 * @author Tom
 * @version $Revision$
 */
@Default(value = DefaultType.FIELD, required = false)
public class ArrayValue implements IValueAccess, IDefaultAttribute {
    /** serialVersionUID */
    private static final long serialVersionUID = -1460468414949211876L;

    transient Object[] instance;
    @Attribute
    Class type;
    @Attribute
    String name;
    @Attribute
    int index;

    @Element(required=false)
    EventController eventController;

    public ArrayValue() {
    }
    
    public ArrayValue(String name, int index) {
        this(name, index, null, null);
    }

    /**
     * constructor
     * 
     * @param object
     * @param type
     */
    public ArrayValue(String name, int index, Class type, Object[] instance) {
        this.name = name;
        this.index = index;
        this.type = (Class) (type != null ? type : instance != null ? instance[index].getClass() : Object.class);
        this.instance = instance;
    }

    @Override
    public Class getDeclaringClass() {
        return Object[].class;
    }

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

    @Override
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public T getValue(Object instance) {
        return (T) (instance instanceof Object[] ? ((Object[]) instance)[index] : instance != null && index == 0
            ? instance : null/*throw new IllegalArgumentException(this + " can't evaluate value for instance " + instance)*/);
    }

    @Override
    public void setValue(Object instance, T value) {
        ((Object[]) instance)[index] = 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(instance);
    }

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

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

    @Override
    public EventController changeHandler() {
        if (eventController == null) {
            eventController = new EventController();
        }
        return eventController;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy