Coverage Report - org.seasar.cubby.validator.validators.NumberValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
NumberValidator
88%
14/16
33%
2/6
3.333
 
 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.validator.validators;
 17  
 
 18  
 import java.math.BigDecimal;
 19  
 
 20  
 import org.seasar.cubby.validator.MessageHelper;
 21  
 import org.seasar.cubby.validator.ScalarFieldValidator;
 22  
 import org.seasar.cubby.validator.ValidationContext;
 23  
 import org.seasar.framework.util.StringUtil;
 24  
 
 25  
 /**
 26  
  * 数値かどうかを検証します。
 27  
  * <p>
 28  
  * 数値かどうかの検証は {@link BigDecimal#BigDecimal(String)} で行っています。
 29  
  * <p>
 30  
  * デフォルトエラーメッセージキー:valid.number
 31  
  * </p>
 32  
  * 
 33  
  * @author agata
 34  
  * @author baba
 35  
  * @see BigDecimal#BigDecimal(String)
 36  
  */
 37  
 public class NumberValidator implements ScalarFieldValidator {
 38  
 
 39  
         /**
 40  
          * メッセージヘルパ。
 41  
          */
 42  
         private final MessageHelper messageHelper;
 43  
 
 44  
         /**
 45  
          * コンストラクタ
 46  
          */
 47  
         public NumberValidator() {
 48  4
                 this("valid.number");
 49  4
         }
 50  
 
 51  
         /**
 52  
          * エラーメッセージキーを指定するコンストラクタ
 53  
          * 
 54  
          * @param messageKey
 55  
          *            エラーメッセージキー
 56  
          */
 57  4
         public NumberValidator(final String messageKey) {
 58  4
                 this.messageHelper = new MessageHelper(messageKey);
 59  4
         }
 60  
 
 61  
         public void validate(final ValidationContext context, final Object value) {
 62  7
                 if (value instanceof String) {
 63  7
                         final String str = (String) value;
 64  7
                         if (StringUtil.isEmpty(str)) {
 65  0
                                 return;
 66  
                         }
 67  
                         try {
 68  7
                                 new BigDecimal(str);
 69  4
                                 return;
 70  3
                         } catch (final NumberFormatException e) {
 71  
                         }
 72  3
                 } else if (value == null) {
 73  0
                         return;
 74  
                 }
 75  3
                 context.addMessageInfo(this.messageHelper.createMessageInfo());
 76  3
         }
 77  
 
 78  
 }