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: 86   Methods: 3
NCLOC: 43   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
EnumerationTranslator.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.lang.reflect.Field;
 18   
 import java.util.Map;
 19   
 
 20   
 import org.apache.hivemind.ApplicationRuntimeException;
 21   
 import org.apache.hivemind.HiveMind;
 22   
 import org.apache.hivemind.internal.Module;
 23   
 import org.apache.hivemind.schema.Translator;
 24   
 
 25   
 /**
 26   
  * Used to translate a set of strings to one of a number of constant values.
 27   
  * Each input string is matched against the name of a public static field
 28   
  * of a class.  The name of the class, and the mappings, are provided
 29   
  * in the initializer.
 30   
  *
 31   
  * @author Howard Lewis Ship
 32   
  */
 33   
 public class EnumerationTranslator implements Translator
 34   
 {
 35   
     private Map _mappings;
 36   
     private String _className;
 37   
     private Class _class;
 38   
 
 39   
     /**
 40   
      * Initialized the translator; the intitializer is the name of the class, a comma,
 41   
      * and a series of key=value mappings from the input values to the names
 42   
      * of the public static fields of the class.
 43   
      */
 44  5
     public EnumerationTranslator(String initializer)
 45   
     {
 46  5
         int commax = initializer.indexOf(',');
 47   
 
 48  5
         _className = initializer.substring(0, commax);
 49   
 
 50  5
         _mappings = RuleUtils.convertInitializer(initializer.substring(commax + 1));
 51   
     }
 52   
 
 53  5
     private synchronized Class getClass(Module contributingModule)
 54   
     {
 55  5
         if (_class == null)
 56  4
             _class = contributingModule.getClassResolver().findClass(_className);
 57   
 
 58  4
         return _class;
 59   
     }
 60   
 
 61  6
     public Object translate(Module contributingModule, Class propertyType, String inputValue)
 62   
     {
 63  6
         if (HiveMind.isBlank(inputValue))
 64  1
             return null;
 65   
 
 66  5
         Class c = getClass(contributingModule);
 67   
 
 68  4
         String fieldName = (String) _mappings.get(inputValue);
 69   
 
 70  4
         if (fieldName == null)
 71  1
             throw new ApplicationRuntimeException(RulesMessages.enumNotRecognized(inputValue));
 72   
 
 73  3
         try
 74   
         {
 75  3
             Field f = c.getField(fieldName);
 76   
 
 77  2
             return f.get(null);
 78   
         }
 79   
         catch (Exception ex)
 80   
         {
 81  1
             throw new ApplicationRuntimeException(RulesMessages.enumError(c, fieldName, ex), ex);
 82   
         }
 83   
     }
 84   
 
 85   
 }
 86