001    package jcolibri.method.retrieve.NNretrieval.similarity.local;
002    
003    import jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction;
004    
005    
006    /**
007     * This function returns 1 if the difference between two numbers is less than a
008     * threshold, 0 in the other case.
009     */
010    public class Threshold implements LocalSimilarityFunction {
011    
012    
013            /** Threshold. */
014            double _threshold;
015    
016            /**
017             * Constructor.
018             */
019            public Threshold(double threshold) {
020                    _threshold = threshold;
021            }
022    
023            /**
024             * Applies the similarity function.
025             */
026            public int compare(int x, int y) {
027                    if (Math.abs(x - y) > _threshold)
028                            return 0;
029                    else
030                            return 1;
031            }
032    
033    
034    
035            /**
036             * Applies the similarity function.
037             * 
038             * @param o1
039             *            Integer
040             * @param o2
041             *            Integer
042             * @return result of apply the similarity function.
043             */
044            public double compute(Object o1, Object o2) throws jcolibri.exception.NoApplicableSimilarityFunctionException {
045                    if ((o1 == null) || (o2 == null))
046                            return 0;
047                    if (!(o1 instanceof java.lang.Integer))
048                            throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), o1.getClass());
049                    if (!(o2 instanceof java.lang.Integer))
050                            throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), o2.getClass());
051    
052    
053                    Integer i1 = (Integer) o1;
054                    Integer i2 = (Integer) o2;
055                    return (double) compare(i1.intValue(), i2.intValue());
056            }
057            
058            /** Applicable to Integer */
059            public boolean isApplicable(Object o1, Object o2)
060            {
061                    if((o1==null)&&(o2==null))
062                            return true;
063                    else if(o1==null)
064                            return o2 instanceof Integer;
065                    else if(o2==null)
066                            return o1 instanceof Integer;
067                    else
068                            return (o1 instanceof Integer)&&(o2 instanceof Integer);
069            }
070    }