1 /** 2 * Copyright 2005 Steve Molloy 3 * 4 * This file is part of OV4J. 5 * 6 * OV4J is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as 7 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 8 * 9 * OV4J is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License along with OV4J; if not, write to the Free Software 13 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 14 * 15 */ 16 package org.ov4j.comp; 17 18 import java.util.logging.Level; 19 import java.util.logging.Logger; 20 21 /** 22 * This class is used to compare numeric values. 23 * 24 * @author smolloy 25 * 26 */ 27 public class NumericComparisonResult<T extends Number> extends ComparisonResult<T> { 28 /** 29 * Logger for this class 30 */ 31 private static final Logger logger = Logger.getLogger(NumericComparisonResult.class.getName()); 32 33 /** 34 * @see org.ov4j.comp.ComparisonResult#compute() 35 */ 36 @Override 37 public void compute() { 38 if (NumericComparisonResult.logger.isLoggable(Level.FINER)) { 39 NumericComparisonResult.logger.entering("NumericComparisonResult", "compute()", "start"); 40 } 41 42 if (getOriginal() != null && getChanged() != null) { 43 if (getChanged().doubleValue() == 0) { 44 setPrecision(getOriginal().doubleValue() == 0 ? 1.0 : 0.0); 45 } else { 46 setPrecision(Math.min(1.0, Math.max(0.0, (1.0 - Math.abs((getChanged().doubleValue() - getOriginal() 47 .doubleValue()) 48 / getChanged().doubleValue()))))); 49 } 50 if (getOriginal().doubleValue() == 0) { 51 setRecall(getChanged().doubleValue() == 0 ? 1.0 : 0.0); 52 } else { 53 setRecall(Math.min(1.0, Math.max(0.0, (1.0 - Math.abs((getChanged().doubleValue() - getOriginal() 54 .doubleValue()) 55 / getOriginal().doubleValue()))))); 56 } 57 } else if (getOriginal() != null) { 58 setPrecision(1.0); 59 setRecall(0.0); 60 } else if (getChanged() != null) { 61 setPrecision(0.0); 62 setRecall(1.0); 63 } else { 64 setPrecision(1.0); 65 setRecall(1.0); 66 } 67 68 if (NumericComparisonResult.logger.isLoggable(Level.FINER)) { 69 NumericComparisonResult.logger.exiting("NumericComparisonResult", "compute()", "end"); 70 } 71 } 72 73 /** 74 * @see org.ov4j.comp.ComparisonResult#fastCompute() 75 */ 76 @Override 77 public void fastCompute() { 78 if (NumericComparisonResult.logger.isLoggable(Level.FINER)) { 79 NumericComparisonResult.logger.entering("NumericComparisonResult", "fastCompute()", "start"); 80 } 81 82 if (getOriginal() != null && getChanged() != null && getOriginal().equals(getChanged())) { 83 setPrecision(1.0); 84 } 85 86 if (NumericComparisonResult.logger.isLoggable(Level.FINER)) { 87 NumericComparisonResult.logger.exiting("NumericComparisonResult", "fastCompute()", "end"); 88 } 89 } 90 }