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

de.tsl2.nano.bean.def.ValueMatcher 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: Jul 2, 2012
 * 
 * Copyright: (c) Thomas Schneider 2012, all rights reserved
 */
package de.tsl2.nano.bean.def;

import java.io.Serializable;

import de.tsl2.nano.bean.IValueAccess;
import de.tsl2.nano.bean.ValueHolder;
import de.tsl2.nano.core.messaging.EventController;
import de.tsl2.nano.util.operation.IConverter;

/**
 * is able to translate a string to a boolean - through a given regular expression. may be useful to translate a
 * character like 'y' and 'n' to true and false.
 * 
 * @author Thomas Schneider
 * @version $Revision$
 */
public class ValueMatcher implements IValueAccess, Serializable {
    /** serialVersionUID */
    private static final long serialVersionUID = -4686551991738977281L;

    ValueBinder vb;
    
    protected static final String DEFAULT_TRUE = "[1xXjJyYsSoOtT]";
    protected static final String DEFAULT_FALSE = "[0  nNfF]";

    public ValueMatcher(IValueAccess value) {
        this(value, DEFAULT_TRUE, DEFAULT_TRUE.substring(0, 1), DEFAULT_FALSE.substring(0, 1));
    }

    public ValueMatcher(IValueAccess value, final String defaultTrueValue, final String defaultFalseValue) {
        this(value, DEFAULT_TRUE, defaultTrueValue, defaultFalseValue);
    }

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

    /**
     * constructor
     * @param value
     * @param trueExpression
     */
    public ValueMatcher(IValueAccess value, final String trueExpression, final String defaultTrueValue, final String defaultFalseValue) {
        super();
        IConverter c = new IConverter() {
            @Override
            public String from(Boolean toValue) {
                return toValue ? defaultTrueValue : defaultFalseValue;
            }

            @Override
            public Boolean to(String fromValue) {
                return fromValue.matches(trueExpression);
            }
        };
        vb = new ValueBinder(value, new ValueHolder(c.to(value.getValue())), c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Boolean getValue() {
        return vb.getSecondValue();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setValue(Boolean newValue) {
        vb.setSecondValue(newValue);
    }

    
    /**
     * {@inheritDoc}
     */
    @Override
    public Class getType() {
        return Boolean.class;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public EventController changeHandler() {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy