Coverage report

  %line %branch
ca.spaz.cron.database.NutrientTable
0% 
0% 

 1  
 /*
 2  
  * Created on Apr 16, 2005 by davidson
 3  
  */
 4  
 package ca.spaz.cron.database;
 5  
 
 6  
 import java.lang.reflect.Field;
 7  
 
 8  
 import org.apache.log4j.Logger;
 9  
 
 10  
 import ca.spaz.cron.datasource.ILocalFoodDatasource;
 11  
 
 12  
 /**
 13  
  * A base class for a group of related nutrient values Provides common support
 14  
  * for loading and updating the database.
 15  
  * 
 16  
  * The nutrient table is expected to have field names that match the names and
 17  
  * types of the corresponding database table. This greatly simplifies
 18  
  * maintaining the database and generating UI forms through reflection.
 19  
  * 
 20  
  * @author davidson
 21  
  */
 22  0
 public abstract class NutrientTable {
 23  
     /**
 24  
      * Logger for this class
 25  
      */
 26  0
     private static final Logger logger = Logger.getLogger(NutrientTable.class);
 27  
     private Food food;
 28  
 
 29  0
     public NutrientTable() {
 30  0
     }
 31  
 
 32  0
     public NutrientTable(Food f) {
 33  0
         this.food = f;
 34  0
         f.getDataSource().getNutrientsFor(f, this);
 35  0
     }
 36  0
 
 37  
     /**
 38  
      * Get the name of the table corresponding to this class. By default it uses
 39  
      * the class name. Override this method if it is different.
 40  
      * 
 41  0
      * @return the name of the database table for this object
 42  0
      */
 43  
     public String getTableName() {
 44  0
         return doGetTableName();
 45  0
     }
 46  0
 
 47  
     /**
 48  0
      * Subclasses must implement this in order to supply their table name.
 49  
      * 
 50  0
      * @return the table name to perform lookups on.
 51  0
      */
 52  
     protected abstract String doGetTableName();
 53  
 
 54  0
     /**
 55  
      * Add the nutrients in the given table to our total
 56  0
      * 
 57  
      * @param toAdd
 58  0
      *            the nutrients ratios to add
 59  0
      * @param weight
 60  0
      *            multiplier for the amount in the added nutrients
 61  
      */
 62  0
     public void addFood(NutrientTable toAdd, double weight) {
 63  0
         assert (toAdd.getClass() == this.getClass());
 64  0
         Field[] fields = this.getClass().getFields();
 65  0
         for (int i = 0; i < fields.length; i++) {
 66  
             try {
 67  0
                 double d1 = fields[i].getDouble(this);
 68  0
                 double d2 = fields[i].getDouble(toAdd);
 69  0
                 fields[i].setDouble(this, d1 + d2 * weight);
 70  0
             } catch (Exception e) {
 71  0
                 logger.error("addFood(NutrientTable, double)", e);
 72  0
             }
 73  
         }
 74  0
     }
 75  
 
 76  
     /**
 77  
      * Get the amount of a nutrient by the tag name
 78  
      * 
 79  
      * @param tag
 80  
      *            the tag name of the nutrient
 81  
      * @return the amount of the nutrient, or 0 if none found
 82  
      */
 83  
     public double getAmountByTag(String tag) {
 84  0
         double val = 0;
 85  
         try {
 86  0
             Field f = this.getClass().getField(tag);
 87  0
             val = f.getDouble(this);
 88  0
         } catch (Exception e) {
 89  0
             logger.error("getAmountByTag(String)", e);
 90  0
         }
 91  0
         return val;
 92  
     }
 93  
 
 94  
     /**
 95  
      * Set the amount of a nutrient by the tag name
 96  
      * 
 97  
      * @param tag the tag name of the nutrient
 98  
      * @param val the amount of the nutrient, or 0 if none found
 99  
      */
 100  
     public void setAmountByTag(String tag, double val) {
 101  
         try {
 102  0
             Field f = this.getClass().getField(tag);
 103  0
             f.setDouble(this, val);
 104  0
         } catch (Exception e) {
 105  0
             logger.error("setAmountByTag(String, double)", e);
 106  0
         }
 107  0
     }
 108  
 
 109  
     /**
 110  
      * Update the existing food information
 111  
      */
 112  
     public void updateDatabase() {
 113  0
         ILocalFoodDatasource lds = (ILocalFoodDatasource) food.getDataSource();
 114  0
         lds.saveFood(food);
 115  0
     }
 116  
 
 117  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.