Coverage Report - org.seasar.cubby.converter.impl.AbstractDecimalNumberConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDecimalNumberConverter
60%
14/23
56%
9/16
3.333
 
 1  
 /*
 2  
  * Copyright 2004-2010 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  
 
 17  
 package org.seasar.cubby.converter.impl;
 18  
 
 19  
 import java.math.BigDecimal;
 20  
 
 21  
 import org.seasar.cubby.action.MessageInfo;
 22  
 import org.seasar.cubby.converter.ConversionException;
 23  
 import org.seasar.cubby.converter.ConversionHelper;
 24  
 
 25  
 /**
 26  
  * 小数への変換を行うコンバータの抽象クラスです。
 27  
  * <p>
 28  
  * 変換元のオブジェクトの文字列表現を値とする{@link BigDecimal}からサブクラスが変換した結果を変換先とします。
 29  
  * </p>
 30  
  * 
 31  
  * @author baba
 32  
  */
 33  76
 public abstract class AbstractDecimalNumberConverter extends AbstractConverter {
 34  
 
 35  
         /**
 36  
          * {@inheritDoc}
 37  
          */
 38  
         public Object convertToObject(final Object value,
 39  
                         final Class<?> objectType, final ConversionHelper helper)
 40  
                         throws ConversionException {
 41  36
                 if (value == null) {
 42  0
                         return null;
 43  
                 }
 44  36
                 return convert(value.toString());
 45  
         }
 46  
 
 47  
         /**
 48  
          * 数を表す文字列から数値に変換して返します。
 49  
          * <p>
 50  
          * 型変換に失敗した場合はメッセージのキーを <code>valid.number</code> とした
 51  
          * {@link ConversionException} をスローします。
 52  
          * </p>
 53  
          * 
 54  
          * @param value
 55  
          *            数を表す文字列
 56  
          * @return 変換結果の数値
 57  
          * @throws ConversionException
 58  
          *             型変換に失敗した場合
 59  
          */
 60  
         protected Number convert(final String value) throws ConversionException {
 61  36
                 if (value == null || value.length() == 0) {
 62  0
                         return null;
 63  
                 }
 64  
 
 65  
                 final BigDecimal decimal;
 66  
                 try {
 67  36
                         decimal = new BigDecimal(value);
 68  12
                 } catch (final NumberFormatException e) {
 69  12
                         final MessageInfo messageInfo = new MessageInfo();
 70  12
                         messageInfo.setKey("valid.number");
 71  12
                         throw new ConversionException(messageInfo);
 72  24
                 }
 73  
 
 74  24
                 final BigDecimal min = this.getMinValue();
 75  24
                 final BigDecimal max = this.getMaxValue();
 76  24
                 if ((min != null && min.compareTo(decimal) > 0)
 77  
                                 || (max != null && max.compareTo(decimal) < 0)) {
 78  0
                         final MessageInfo messageInfo = new MessageInfo();
 79  0
                         messageInfo.setKey("valid.range");
 80  0
                         messageInfo.setArguments(min.toPlainString(), max.toPlainString());
 81  0
                         throw new ConversionException(messageInfo);
 82  
                 }
 83  
 
 84  24
                 return convert(decimal);
 85  
         }
 86  
 
 87  
         /**
 88  
          * 数値を変換して返します。
 89  
          * 
 90  
          * @param decimal
 91  
          *            変換元の数値
 92  
          * @return 変換結果の数値
 93  
          */
 94  
         protected abstract Number convert(BigDecimal decimal);
 95  
 
 96  
         /**
 97  
          * 最小値を取得します。
 98  
          * <p>
 99  
          * 最小値をチェックしない場合は <code>null</code> を返します。
 100  
          * </p>
 101  
          * 
 102  
          * @return 最小値
 103  
          */
 104  
         protected abstract BigDecimal getMinValue();
 105  
 
 106  
         /**
 107  
          * 最大値を取得します。
 108  
          * <p>
 109  
          * 最大値をチェックしない場合は <code>null</code> を返します。
 110  
          * </p>
 111  
          * 
 112  
          * @return 最大値
 113  
          */
 114  
         protected abstract BigDecimal getMaxValue();
 115  
 
 116  
         /**
 117  
          * {@inheritDoc}
 118  
          */
 119  
         public String convertToString(final Object value,
 120  
                         final ConversionHelper helper) {
 121  0
                 if (value == null) {
 122  0
                         return null;
 123  
                 }
 124  0
                 return value.toString();
 125  
         }
 126  
 
 127  
 }