1 /* 2 * Copyright 2007 Kasper B. Graversen 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.supercsv.cellprocessor.constraint; 17 18 import org.supercsv.cellprocessor.CellProcessorAdaptor; 19 import org.supercsv.cellprocessor.ift.CellProcessor; 20 import org.supercsv.cellprocessor.ift.StringCellProcessor; 21 import org.supercsv.exception.SuperCsvCellProcessorException; 22 import org.supercsv.exception.SuperCsvConstraintViolationException; 23 import org.supercsv.util.CsvContext; 24 25 /** 26 * This constraint ensures that the input data has a string length between the supplied min and max values (both 27 * inclusive). Should the input be anything different from a String, it will be converted to a string using the input's 28 * <code>toString()</code> method. 29 * 30 * @author Kasper B. Graversen 31 * @author James Bassett 32 */ 33 public class StrMinMax extends CellProcessorAdaptor implements StringCellProcessor { 34 35 private final long min; 36 private final long max; 37 38 /** 39 * Constructs a new <tt>StrMinMax</tt> processor, which ensures that the input data has a string length between the 40 * supplied min and max values (both inclusive). 41 * 42 * @param min 43 * the minimum String length 44 * @param max 45 * the maximum String length 46 * @throws IllegalArgumentException 47 * if {@code max < min}, or {@code min is < 0} 48 */ 49 public StrMinMax(final long min, final long max) { 50 super(); 51 checkPreconditions(min, max); 52 this.min = min; 53 this.max = max; 54 } 55 56 /** 57 * Constructs a new <tt>StrMinMax</tt> processor, which ensures that the input data has a string length between the 58 * supplied min and max values (both inclusive), then calls the next processor in the chain. 59 * 60 * @param min 61 * the minimum String length 62 * @param max 63 * the maximum String length 64 * @param next 65 * the next processor in the chain 66 * @throws NullPointerException 67 * if next is null 68 * @throws IllegalArgumentException 69 * if {@code max < min}, or {@code min is < 0} 70 */ 71 public StrMinMax(final long min, final long max, final CellProcessor next) { 72 super(next); 73 checkPreconditions(min, max); 74 this.min = min; 75 this.max = max; 76 } 77 78 /** 79 * Checks the preconditions for creating a new StrMinMax processor. 80 * 81 * @param min 82 * the minimum String length 83 * @param max 84 * the maximum String length 85 * @throws IllegalArgumentException 86 * if {@code max < min}, or {@code min is < 0} 87 */ 88 private static void checkPreconditions(final long min, final long max) { 89 if( max < min ) { 90 throw new IllegalArgumentException(String.format("max (%d) should not be < min (%d)", max, min)); 91 } 92 if( min < 0 ) { 93 throw new IllegalArgumentException(String.format("min length (%d) should not be < 0", min)); 94 } 95 } 96 97 /** 98 * {@inheritDoc} 99 * 100 * @throws SuperCsvCellProcessorException 101 * if value is null 102 * @throws SuperCsvConstraintViolationException 103 * if {@code length < min} or {@code length > max} 104 */ 105 public Object execute(final Object value, final CsvContext context) { 106 validateInputNotNull(value, context); 107 108 final String stringValue = value.toString(); 109 final int length = stringValue.length(); 110 if( length < min || length > max ) { 111 throw new SuperCsvConstraintViolationException(String.format( 112 "the length (%d) of value '%s' does not lie between the min (%d) and max (%d) values (inclusive)", 113 length, stringValue, min, max), context, this); 114 } 115 116 return next.execute(stringValue, context); 117 } 118 }