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; 17 18 import org.supercsv.cellprocessor.ift.BoolCellProcessor; 19 import org.supercsv.cellprocessor.ift.DateCellProcessor; 20 import org.supercsv.cellprocessor.ift.DoubleCellProcessor; 21 import org.supercsv.cellprocessor.ift.LongCellProcessor; 22 import org.supercsv.cellprocessor.ift.StringCellProcessor; 23 import org.supercsv.exception.SuperCsvCellProcessorException; 24 import org.supercsv.util.CsvContext; 25 26 /** 27 * Ensure that Strings or String-representations of objects are truncated to a maximum size. If you desire, you can 28 * append a String to denote that the data has been truncated (e.g. "..."). 29 * <p> 30 * As of 2.0.0, this functionality was moved from the {@link Trim} processor to this processor, to allow a clear 31 * distinction between trimming and truncating. 32 * 33 * @author Kasper B. Graversen 34 * @author James Bassett 35 */ 36 public class Truncate extends CellProcessorAdaptor implements BoolCellProcessor, DateCellProcessor, 37 DoubleCellProcessor, LongCellProcessor, StringCellProcessor { 38 39 private static final String EMPTY_STRING = ""; 40 41 private final int maxSize; 42 private final String suffix; 43 44 /** 45 * Constructs a new <tt>Truncate</tt> processor, which truncates a String to ensure it is no longer than the 46 * specified size. 47 * 48 * @param maxSize 49 * the maximum size of the String 50 * @throws IllegalArgumentException 51 * if {@code maxSize <= 0} 52 */ 53 public Truncate(final int maxSize) { 54 this(maxSize, EMPTY_STRING); 55 } 56 57 /** 58 * Constructs a new <tt>Truncate</tt> processor, which truncates a String to ensure it is no longer than the 59 * specified size, then appends the <code>suffix</code> String to indicate that the String has been truncated. 60 * 61 * @param maxSize 62 * the maximum size of the String 63 * @param suffix 64 * the String to append if the input is truncated (e.g. "...") 65 * @throws IllegalArgumentException 66 * if {@code maxSize <= 0} 67 * @throws NullPointerException 68 * if suffix is null 69 */ 70 public Truncate(final int maxSize, final String suffix) { 71 checkPreconditions(maxSize, suffix); 72 this.maxSize = maxSize; 73 this.suffix = suffix; 74 } 75 76 /** 77 * Constructs a new <tt>Truncate</tt> processor, which truncates a String to ensure it is no longer than the 78 * specified size, then appends the <code>suffix</code> String to indicate that the String has been truncated and 79 * calls the next processor in the chain. 80 * 81 * @param maxSize 82 * the maximum size of the String 83 * @param suffix 84 * the String to append if the input is truncated (e.g. "...") 85 * @param next 86 * the next processor in the chain 87 * @throws IllegalArgumentException 88 * if {@code maxSize <= 0} 89 * @throws NullPointerException 90 * if suffix or next is null 91 */ 92 public Truncate(final int maxSize, final String suffix, final StringCellProcessor next) { 93 super(next); 94 checkPreconditions(maxSize, suffix); 95 this.maxSize = maxSize; 96 this.suffix = suffix; 97 } 98 99 /** 100 * Constructs a new <tt>Truncate</tt> processor, which truncates a String to ensure it is no longer than the 101 * specified size, then calls the next processor in the chain. 102 * 103 * @param maxSize 104 * the maximum size of the String 105 * @param next 106 * the next processor in the chain 107 * @throws IllegalArgumentException 108 * if {@code maxSize <= 0} 109 * @throws NullPointerException 110 * if next is null 111 */ 112 public Truncate(final int maxSize, final StringCellProcessor next) { 113 this(maxSize, EMPTY_STRING, next); 114 } 115 116 /** 117 * Checks the preconditions for creating a new Truncate processor. 118 * 119 * @param maxSize 120 * the maximum size of the String 121 * @param suffix 122 * the String to append if the input is truncated (e.g. "...") 123 * @throws IllegalArgumentException 124 * if {@code maxSize <= 0} 125 * @throws NullPointerException 126 * if suffix is null 127 */ 128 private static void checkPreconditions(final int maxSize, final String suffix) { 129 if( maxSize <= 0 ) { 130 throw new IllegalArgumentException(String.format("maxSize should be > 0 but was %d", maxSize)); 131 } 132 if( suffix == null ) { 133 throw new NullPointerException("suffix should not be null"); 134 } 135 } 136 137 /** 138 * {@inheritDoc} 139 * 140 * @throws SuperCsvCellProcessorException 141 * if value is null 142 */ 143 public Object execute(final Object value, final CsvContext context) { 144 validateInputNotNull(value, context); 145 146 final String stringValue = value.toString(); 147 final String result; 148 if( stringValue.length() <= maxSize ) { 149 result = stringValue; 150 } else { 151 result = stringValue.substring(0, maxSize) + suffix; 152 } 153 154 return next.execute(result, context); 155 } 156 }