Clover coverage report - Code Coverage for hivemind release 1.0-beta-1
Coverage timestamp: Sat Jul 3 2004 09:41:37 EDT
file stats: LOC: 103   Methods: 3
NCLOC: 55   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
IntTranslator.java 100% 100% 100% 100%
coverage
 1   
 //  Copyright 2004 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.hivemind.schema.rules;
 16   
 
 17   
 import java.util.Map;
 18   
 
 19   
 import org.apache.hivemind.ApplicationRuntimeException;
 20   
 import org.apache.hivemind.HiveMind;
 21   
 import org.apache.hivemind.internal.Module;
 22   
 import org.apache.hivemind.schema.Translator;
 23   
 
 24   
 /**
 25   
  * Translates strings to integer values.
 26   
  *
 27   
  * @author Howard Lewis Ship
 28   
  */
 29   
 public class IntTranslator implements Translator
 30   
 {
 31   
     private int _minValue;
 32   
     private boolean _isMinValue;
 33   
     private int _maxValue;
 34   
     private boolean _isMaxValue;
 35   
     private int _defaultValue = 0;
 36   
 
 37  2
     public IntTranslator()
 38   
     {
 39   
     }
 40   
 
 41   
     /**
 42   
      * Initializers:
 43   
      * <ul>
 44   
      * <li>default: default value for empty or invalid input
 45   
      * <li>min: minimum acceptible value
 46   
      * <li>max: maximum acceptible value
 47   
      * </ul>
 48   
      */
 49  5
     public IntTranslator(String initializer)
 50   
     {
 51  5
         Map m = RuleUtils.convertInitializer(initializer);
 52   
 
 53  5
         String defaultInit = (String) m.get("default");
 54   
 
 55  5
         if (defaultInit != null)
 56  3
             _defaultValue = Integer.parseInt(defaultInit);
 57   
 
 58  5
         String minInit = (String) m.get("min");
 59  5
         if (minInit != null)
 60   
         {
 61  3
             _isMinValue = true;
 62  3
             _minValue = Integer.parseInt(minInit);
 63   
         }
 64   
 
 65  5
         String maxInit = (String) m.get("max");
 66  5
         if (maxInit != null)
 67   
         {
 68  3
             _isMaxValue = true;
 69  3
             _maxValue = Integer.parseInt(maxInit);
 70   
         }
 71   
     }
 72   
 
 73   
     /**
 74   
      * Converts the string to an Integer.  The empty string is returned as zero.
 75   
      * On failure, an error is logged and the method returns zero.
 76   
      */
 77  11
     public Object translate(Module contributingModule, Class propertyType, String inputValue)
 78   
     {
 79  11
         if (HiveMind.isBlank(inputValue))
 80  3
             return new Integer(_defaultValue);
 81   
 
 82  8
         int value;
 83   
 
 84  8
         try
 85   
         {
 86  8
             value = Integer.parseInt(inputValue);
 87   
         }
 88   
         catch (Exception ex)
 89   
         {
 90  2
             throw new ApplicationRuntimeException(RulesMessages.invalidIntValue(inputValue));
 91   
         }
 92   
 
 93  6
         if (_isMinValue && value < _minValue)
 94  2
             throw new ApplicationRuntimeException(RulesMessages.minIntValue(inputValue, _minValue));
 95   
 
 96  4
         if (_isMaxValue && value > _maxValue)
 97  2
             throw new ApplicationRuntimeException(RulesMessages.maxIntValue(inputValue, _maxValue));
 98   
 
 99  2
         return new Integer(value);
 100   
     }
 101   
 
 102   
 }
 103