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

de.tsl2.nano.bean.def.ValueExpressionFormat 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: Thomas Schneider
 * created on: Jun 5, 2013
 * 
 * Copyright: (c) Thomas Schneider 2013, all rights reserved
 */
package de.tsl2.nano.bean.def;

import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;

import de.tsl2.nano.core.util.Util;

/**
 * Format for entities/beans - not implementing it's own toString(). The format packs the given type into a
 * {@link BeanDefinition}, using the first attribute as output. See {@link ValueExpression} for further informations.
 * 
 * @author Thomas Schneider
 * @version $Revision$
 */
public class ValueExpressionFormat extends Format {
    /** serialVersionUID */
    private static final long serialVersionUID = -3040338597603039966L;
    /** don't use this variable directly. use {@link #ve()} instead. */
    ValueExpression ve;

    /**
     * constructor to be serializable
     */
    protected ValueExpressionFormat() {
        super();
    }

    /**
     * constructor
     * 
     * @param type type to format
     */
    public ValueExpressionFormat(Class type) {
        super();
        ve = BeanDefinition.getBeanDefinition(type).getValueExpression();    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    @Override
    public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
        pos.setEndIndex(pos.getBeginIndex() + 1);
        return toAppendTo.append(ve().to((T) obj));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object parseObject(String source, ParsePosition pos) {
        pos.setIndex(pos.getIndex() + 1);
        return ve().from(source);
    }
    
    public String getPattern() {
        return ve().getExpression();
    }
    
    public void applyPattern(String pattern) {
        ve().setExpression(pattern);
    }
    
    ValueExpression ve() {
        return ve;
    }
    
    @Override
    public String toString() {
        return Util.toString(getClass(), ve());
    }

    /**
     * @return Returns the ve.
     */
    public ValueExpression getValueExpression() {
        return ve();
    }

    /**
     * @param ve The ve to set.
     */
    public void setValueExpression(ValueExpression ve) {
        this.ve = ve;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy