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

de.tsl2.nano.action.IConstraint Maven / Gradle / Ivy

Go to download

TSL2 Framework Commons (Collections, Actions/Excecution, Readers, Xml, Print, Mail, FuzzyFinder, Proxies, Network-Structure)

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

import java.text.Format;
import java.util.Collection;

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

/**
 * 
 * @author Tom
 * @version $Revision$ 
 */
public interface IConstraint {
    /** format-constraint for the attributes value */
    Format getFormat();

    /** value type */
    Class getType();
    
    /** checks, if the given value is valid - should not throw an exception */
    IStatus checkStatus(String name, T value);

    /** checks, if the given value is valid - throws an exception */
    void check(String name, T value);

    /** default value for this attribute */
    T getDefault();

    
    /** returns the minimum value or null */
    Comparable getMinimum();

    /** returns the maximum value or null */
    Comparable getMaximum();

    /** returns the allowed values or null */
    Collection getAllowedValues();

    /** maximum length - useful on strings */
    int getLength();

    /** scale - useful for numbers of type BigDecimal */
    int getScale();

    /** precision - useful for numbers of type BigDecimal */
    int getPrecision();

    /** should return true, if attribute-value may be null */
    boolean isNullable();

    /** define some basic attribute definitions */
    > C setBasicDef(int length, boolean nullable, Format format, T defaultValue);

    /** define number definitions - if the attribute is a number */
    > C  setNumberDef(int scale, int precision);

    /** defines a min/max range constraint. use {@link ValueCompare} to compare on changing values */
    > C  setRange(Comparable min, Comparable max);

    /**
     * defines all allowed values - if you call {@link #setRange(Comparable, Comparable)}, you shouldn't call this
     * method
     */
    > C  setRange(Collection allowedValues);

    /** define constraining text format. use RegularExpressionFormat to define a regexp pattern */
    > C  setFormat(Format format);

    /** define type */
    > C  setType(Class type);

    /** define maximum length */
    > C  setLength(int length);

    /** define scale */
    > C  setScale(int scale);

    /** define precision */
    > C  setPrecision(int precision);

    /** define nullable */
    > C  setNullable(boolean nullable);

    > C  setDefault(T defaultValue);
    
    /** usable for int definitions that are not bitfields */
    static final int UNDEFINED = -1;

    /** usable for all int definitions like bitfiels type and style */
    static final int UNSET = 0;
    
    static Object fromString(Class type, String def) {
        if (type != null && !Util.isEmpty(def)) {
            try {
                return BeanClass.createInstance(type, def);
            } catch (Exception e) { //don't stop the flow - the default/min/max will not be set!
                e.printStackTrace();//TODO: log
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy