001 package jcolibri.method.retrieve.NNretrieval.similarity.local; 002 003 import jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction; 004 005 006 007 /** 008 * This function returns the similarity of two number inside an interval. 009 * sim(x,y)=1-(|x-y|/interval) 010 * 011 * Now it works with Number values. 012 */ 013 public class Interval implements LocalSimilarityFunction { 014 015 /** Interval */ 016 double _interval; 017 018 /** 019 * Constructor. 020 */ 021 public Interval(double interval) { 022 _interval = interval; 023 } 024 025 /** 026 * Applies the similarity function. 027 * 028 * @param o1 029 * Number 030 * @param o2 031 * Number 032 * @return result of apply the similarity function. 033 */ 034 public double compute(Object o1, Object o2) throws jcolibri.exception.NoApplicableSimilarityFunctionException{ 035 if ((o1 == null) || (o2 == null)) 036 return 0; 037 if (!(o1 instanceof java.lang.Number)) 038 throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), o1.getClass()); 039 if (!(o2 instanceof java.lang.Number)) 040 throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), o2.getClass()); 041 042 043 Number i1 = (Number) o1; 044 Number i2 = (Number) o2; 045 046 double v1 = i1.doubleValue(); 047 double v2 = i2.doubleValue(); 048 return 1 - ((double) Math.abs(v1 - v2) / _interval); 049 } 050 051 /** Applicable to Integer */ 052 public boolean isApplicable(Object o1, Object o2) 053 { 054 if((o1==null)&&(o2==null)) 055 return true; 056 else if(o1==null) 057 return o2 instanceof Number; 058 else if(o2==null) 059 return o1 instanceof Number; 060 else 061 return (o1 instanceof Number)&&(o2 instanceof Number); 062 } 063 064 }