001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022package org.biojava.bio.search;
023
024import java.util.regex.Pattern;
025import java.util.regex.PatternSyntaxException;
026
027/**
028 * Class for implementing tests with BlastLikeSearchFilter
029 * objects.  Several precanned tests are included.
030 * @author David Huen
031 */
032public interface FilterTest
033{
034    /**
035     * @return returns true if test is successful.
036     */
037    public boolean accept(Object value);
038
039    /**
040     * Tests that the value associated with the specified
041     * key is equal to the value supplied here by whatever
042     * criterion of equality appropriate to those objects.
043     * A value of false is returned by accept() if a
044     * ClassCastException is thrown on comparing the objects.
045     */
046    public static class Equals
047        implements FilterTest
048    {
049        private Object value;
050
051        public Equals(Object value) { this.value = value; }
052
053        public boolean accept(Object value)
054        {
055            try {
056                return this.value.equals(value);
057            }
058            catch (ClassCastException ce) {
059                return false;
060            }
061        }
062    }
063
064    /**
065     * Tests that the value associated with the specified
066     * key is matched in its entirety by the supplied regex.
067     * A value of false is returned by accept() if a
068     * ClassCastException is thrown on comparing the objects.
069     */
070    public static class MatchRegex
071    {
072        private Pattern pattern;
073
074        public MatchRegex(String regex)
075            throws PatternSyntaxException
076        {
077            pattern = Pattern.compile(regex);
078        }
079
080        public boolean accept(Object value)
081        {
082            try {
083                return pattern.matcher((String) value).matches();
084            }
085            catch (ClassCastException ce) {
086                return false;
087            }
088        }
089    }
090
091    /**
092     * Tests that the value associated with the specified
093     * key contains a part matched by the supplied regex.
094     * A value of false is returned by accept() if a
095     * ClassCastException is thrown on comparing the objects.
096     */
097    public static class FindRegex
098    {
099        private Pattern pattern;
100
101        public FindRegex(String regex)
102            throws PatternSyntaxException
103        {
104            pattern = Pattern.compile(regex);
105        }
106
107        public boolean accept(Object value)
108        {
109            try {
110                return pattern.matcher((String) value).find();
111            }
112            catch (ClassCastException ce) {
113                return false;
114            }
115        }
116    }
117
118    /**
119     * Tests that the value associated with the specified
120     * key is less than the specified threshold.  The test
121     * assumes that value is a String representing a real
122     * number.  If not, the test will return null.
123     */
124    public static class LessThan
125        implements FilterTest
126    {
127        private double threshold;
128
129        public LessThan(double threshold) { this. threshold = threshold; }
130
131        public boolean accept(Object value)
132        {
133            return (value instanceof String) && (Double.parseDouble((String) value) < threshold);
134        }
135    }
136
137    /**
138     * Tests that the value associated with the specified
139     * key is greater than the specified threshold.  The test
140     * assumes that value is a String representing a real
141     * number.  If not, the test will return null.
142     */
143    public static class GreaterThan
144        implements FilterTest
145    {
146        private double threshold;
147
148        public GreaterThan(double threshold) { this. threshold = threshold; }
149
150        public boolean accept(Object value)
151        {
152            return (value instanceof String) && (Double.parseDouble((String) value) > threshold);
153        }
154    }
155}
156