Coverage Report - org.seasar.cubby.converter.impl.SqlTimestampConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
SqlTimestampConverter
47%
7/15
38%
3/8
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 the Seasar Foundation and the Others.
 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,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.cubby.converter.impl;
 17  
 
 18  
 import java.text.DateFormat;
 19  
 import java.text.ParseException;
 20  
 
 21  
 import org.seasar.cubby.converter.ConversionHelper;
 22  
 
 23  
 /**
 24  
  * {@link java.sql.Timestamp}への変換を行うコンバータです。
 25  
  * <p>
 26  
  * 変換元のオブジェクトの文字列表現をフォーマットに従って{@link java.sql.Timestamp}に変換した結果を変換先とします。
 27  
  * </p>
 28  
  * 
 29  
  * @author baba
 30  
  * @since 1.1.0
 31  
  */
 32  76
 public class SqlTimestampConverter extends AbstractConverter {
 33  
 
 34  
         /**
 35  
          * {@inheritDoc}
 36  
          */
 37  
         public Class<?> getObjectType() {
 38  78
                 return java.sql.Timestamp.class;
 39  
         }
 40  
 
 41  
         /**
 42  
          * {@inheritDoc}
 43  
          */
 44  
         public Object convertToObject(final Object value,
 45  
                         final Class<?> objectType, ConversionHelper helper) {
 46  3
                 if (value == null) {
 47  0
                         return null;
 48  
                 }
 49  3
                 final DateFormat dateFormat = helper.getFormatPattern()
 50  
                                 .getTimestampFormat();
 51  3
                 return toTimestamp((String) value, dateFormat);
 52  
         }
 53  
 
 54  
         /**
 55  
          * 文字列を{@link java.sql.Timestamp}に変換して返します。
 56  
          * 
 57  
          * @param date
 58  
          *            変換元のオブジェクトの文字列表現
 59  
          * @param dateFormat
 60  
          *            フォーマット
 61  
          * @return 変換した結果の{@link java.sql.Timestamp}
 62  
          */
 63  
         protected java.sql.Timestamp toTimestamp(final String date,
 64  
                         final DateFormat dateFormat) {
 65  3
                 if (date == null || date.length() == 0) {
 66  0
                         return null;
 67  
                 }
 68  
                 try {
 69  3
                         return new java.sql.Timestamp(dateFormat.parse(date).getTime());
 70  0
                 } catch (final ParseException e) {
 71  0
                         return null;
 72  
                 }
 73  
         }
 74  
 
 75  
         /**
 76  
          * {@inheritDoc}
 77  
          */
 78  
         public String convertToString(final Object value, ConversionHelper helper) {
 79  0
                 if (value == null) {
 80  0
                         return null;
 81  
                 }
 82  0
                 final DateFormat formatter = helper.getFormatPattern()
 83  
                                 .getTimestampFormat();
 84  0
                 return formatter.format((java.sql.Timestamp) value);
 85  
         }
 86  
 
 87  
 }