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