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

org.supercsv.ext.cellprocessor.constraint.Length Maven / Gradle / Ivy

Go to download

CSVのJavaライブラリであるSuperCSVに、アノテーション機能を追加したライブラリです。

There is a newer version: 2.3
Show newest version
package org.supercsv.ext.cellprocessor.constraint;

import java.util.HashMap;
import java.util.Map;

import org.supercsv.cellprocessor.CellProcessorAdaptor;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.cellprocessor.ift.StringCellProcessor;
import org.supercsv.exception.SuperCsvCellProcessorException;
import org.supercsv.exception.SuperCsvConstraintViolationException;
import org.supercsv.ext.cellprocessor.ift.ValidationCellProcessor;
import org.supercsv.util.CsvContext;


/**
 *
 * @author T.TSUCHIE
 *
 */
public class Length extends CellProcessorAdaptor implements StringCellProcessor, ValidationCellProcessor {
    
    protected final int min;
    
    protected final int max;
    
    public Length(final int min, final int max) {
        super();
        checkPreconditions(min, max);
        this.min = min;
        this.max = max;
    }
    
    public Length(final int min, final int max, final CellProcessor next) {
        super(next);
        checkPreconditions(min, max);
        this.min = min;
        this.max = max;
    }
    
    /**
     * Checks the preconditions for creating a new {@link Length} processor.
     * 
     * @param min
     *            the minimum String length
     * @param max
     *            the maximum String length
     * @throws IllegalArgumentException
     *             {@literal if max < min, or min is < 0}
     */
    private static void checkPreconditions(final int min, final int max) {
        if( max < min ) {
            throw new IllegalArgumentException(String.format("max (%d) should not be < min (%d)", max, min));
        }
        
        if( min < 0 ) {
            throw new IllegalArgumentException(String.format("min length (%d) should not be < 0", min));
        }
    }
    
    /**
     * {@inheritDoc}
     * 
     * @throws SuperCsvCellProcessorException
     *             {@literal if value is null}
     * @throws SuperCsvConstraintViolationException
     *             {@literal if length is < min or length > max}
     */
    @SuppressWarnings("unchecked")
    public Object execute(final Object value, final CsvContext context) {
        validateInputNotNull(value, context);
        
        final String stringValue = value.toString();
        final int length = stringValue.length();
        if( length < min || length > max ) {
            throw new SuperCsvConstraintViolationException(String.format(
                "the length (%d) of value '%s' does not lie between the min (%d) and max (%d) values (inclusive)",
                length, stringValue, min, max), context, this);
        }
        
        return next.execute(stringValue, context);
    }
    
    @Override
    public Map getMessageVariable() {
        Map vars = new HashMap();
        vars.put("min", getMin());
        vars.put("max", getMax());
        return vars;
    }
    
    @Override
    public String formatValue(final Object value) {
        if(value == null) {
            return "";
        }
        return value.toString();
    }
    
    public int getMin() {
        return min;
    }
    
    public int getMax() {
        return max;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy