001/**
002 * Equal.java
003 * jCOLIBRI2 framework. 
004 * @author Juan A. Recio-Garc�a.
005 * GAIA - Group for Artificial Intelligence Applications
006 * http://gaia.fdi.ucm.es
007 * 28/10/2007
008 */
009package es.ucm.fdi.gaia.jcolibri.method.retrieve.FilterBasedRetrieval.predicates;
010
011import es.ucm.fdi.gaia.jcolibri.exception.NoApplicableFilterPredicateException;
012
013/**
014 * Predicate that compares if the query object is less than the case object.
015 * Only applicable to enums and numbers.
016 * @author Juan A. Recio-Garcia
017 * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge.
018 * @version 1.0
019 * @see FilterBasedRetrievalMethod
020 * @see FilterConfig
021 */
022public class Threshold implements FilterPredicate
023{
024    private double threshold;
025    
026    public Threshold(Number threshold)
027    {
028        this.threshold = threshold.doubleValue();
029    }
030    
031    public boolean compute(Object caseObject, Object queryObject) throws NoApplicableFilterPredicateException
032    {
033        if((caseObject == null)&&(queryObject==null))
034            return true;
035        else if(caseObject == null)
036            return false;
037        else if(queryObject == null)
038            return true;
039        else if (! ((caseObject instanceof Number)||(caseObject instanceof Enum)))
040                throw new NoApplicableFilterPredicateException(this.getClass(), caseObject.getClass());
041        else if (! ((queryObject instanceof Number)||(queryObject instanceof Enum)))
042                throw new NoApplicableFilterPredicateException(this.getClass(), queryObject.getClass());
043        else 
044        {
045            double caseValue;
046            double queryValue;
047            if((caseObject instanceof Number)&&(queryObject instanceof Number))
048            {
049                Number n1  = (Number) caseObject;
050                caseValue  = n1.doubleValue();
051                Number n2  = (Number) queryObject;
052                queryValue = n2.doubleValue();
053            }
054            else
055            {
056                Enum<?> enum1 = (Enum<?>)caseObject;
057                caseValue  = enum1.ordinal();
058                Enum<?> enum2 = (Enum<?>)queryObject;
059                queryValue = enum2.ordinal();
060            }
061            return Math.abs(queryValue-caseValue) < threshold;
062        }
063    }
064
065}