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.joda; 17 18 import org.joda.time.Period; 19 import org.joda.time.format.ISOPeriodFormat; 20 import org.joda.time.format.PeriodFormat; 21 import org.joda.time.format.PeriodFormatter; 22 import org.joda.time.format.PeriodFormatterBuilder; 23 import org.supercsv.cellprocessor.CellProcessorAdaptor; 24 import org.supercsv.cellprocessor.ift.CellProcessor; 25 import org.supercsv.exception.SuperCsvCellProcessorException; 26 import org.supercsv.util.CsvContext; 27 28 /** 29 * Converts a String to a Joda Period. 30 * 31 * <p> 32 * For constructors using PeriodFormatter, refer to the following Joda classes: 33 * <ul> 34 * <li>{@link PeriodFormat} - formats by pattern and style</li> 35 * <li>{@link ISOPeriodFormat} - ISO8601 formats</li> 36 * <li>{@link PeriodFormatterBuilder} - complex formats created via method calls 37 * </li> 38 * </ul> 39 * <p> 40 * By default, converts from Strings in the ISO8601 duration format. 41 * <p> 42 * For example, "PT6H3M7S" represents 6 hours, 3 minutes, 7 seconds. 43 * <p> 44 * 45 * @since 2.3.0 46 * @author James Bassett 47 */ 48 public class ParsePeriod extends CellProcessorAdaptor { 49 50 private final PeriodFormatter formatter; 51 52 /** 53 * Constructs a new <tt>ParsePeriod</tt> processor, which parses a String as 54 * a Joda Period. 55 */ 56 public ParsePeriod() { 57 this.formatter = null; 58 } 59 60 /** 61 * Constructs a new <tt>ParsePeriod</tt> processor, which parses a String as 62 * a Joda Period, then calls the next processor in the chain. 63 * 64 * @param next 65 * the next processor in the chain 66 * @throws NullPointerException 67 * if next is null 68 */ 69 public ParsePeriod(final CellProcessor next) { 70 super(next); 71 this.formatter = null; 72 } 73 74 /** 75 * Constructs a new <tt>ParsePeriod</tt> processor, which parses a String as 76 * a Joda Period using the supplied formatter. 77 * 78 * @param formatter 79 * the formatter used for parsing 80 * @throws NullPointerException 81 * if formatter is null 82 */ 83 public ParsePeriod(final PeriodFormatter formatter) { 84 checkPreconditions(formatter); 85 this.formatter = formatter; 86 } 87 88 /** 89 * Constructs a new <tt>ParsePeriod</tt> processor, which parses a String as 90 * a Joda Period using the supplied formatter, then calls the next processor 91 * in the chain. 92 * 93 * @param formatter 94 * the formatter used for parsing 95 * @param next 96 * the next processor in the chain 97 * @throws NullPointerException 98 * if formatter or next is null 99 */ 100 public ParsePeriod(final PeriodFormatter formatter, final CellProcessor next) { 101 super(next); 102 checkPreconditions(formatter); 103 this.formatter = formatter; 104 } 105 106 /** 107 * Checks the preconditions for creating a new ParsePeriod processor. 108 * 109 * @param formatter 110 * the formatter 111 * @throws NullPointerException 112 * if formatter is null 113 */ 114 private static void checkPreconditions(final PeriodFormatter formatter) { 115 if (formatter == null) { 116 throw new NullPointerException("formatter should not be null"); 117 } 118 } 119 120 /** 121 * {@inheritDoc} 122 * 123 * @throws SuperCsvCellProcessorException 124 * if value is null or is not a String 125 */ 126 public Object execute(final Object value, final CsvContext context) { 127 validateInputNotNull(value, context); 128 if (!(value instanceof String)) { 129 throw new SuperCsvCellProcessorException(String.class, value, 130 context, this); 131 } 132 133 final String string = (String) value; 134 final Period result; 135 136 try { 137 if (formatter != null) { 138 result = Period.parse(string, formatter); 139 } else { 140 result = Period.parse(string); 141 } 142 } catch (IllegalArgumentException e) { 143 throw new SuperCsvCellProcessorException( 144 "Failed to parse value as a Period", context, this, e); 145 } 146 147 return next.execute(result, context); 148 } 149 150 }