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