001    package net.sourceforge.mp3dings.filter;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: NumericFilter.java 13 2007-07-27 09:52:39Z ringler $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import java.io.Serializable;
023    import java.util.prefs.Preferences;
024    import net.sourceforge.mp3dings.utils.Bean;
025    
026    /** A filter that accepts or rejects a numeric value depending on whether this
027     *  value is inside or outside a given numeric range.
028     * @author Moritz Ringler
029     * @version $Revision: 13 $ ($Date: 2007-07-27 11:52:39 +0200 (Fr, 27 Jul 2007) $)
030     **/
031    public class NumericFilter extends FilterBean implements Serializable{
032            static final long serialVersionUID = -637827176681503635L;
033    
034            /** @see #setInclude(boolean) **/
035            public static final boolean INCLUDE = true;
036            /** @see #setInclude(boolean) **/
037            public static final boolean EXCLUDE = false;
038            private int lowerBound = Integer.MIN_VALUE;
039            private int upperBound = Integer.MAX_VALUE;
040            private boolean include     = INCLUDE;
041    
042            /** Default constructor.
043             * Same as NumericFilter(Integer.MIN_VALUE,
044             * Integer.MAX_VALUE, 1)
045             * @see #NumericFilter(int, int, boolean)
046             */
047            public NumericFilter(){
048            }
049    
050            /** Constructs a new NumericFilter for the given field, with
051             *  the given bounds and the given inclusion behaviour.
052             *  @param lbound Lower bound.
053             *  @param ubound Upper bound.
054             *  @param incl   Sets whether this filter accepts or rejects numbers in
055             *                the open range (lbound, ubound).
056             *  @see #setBounds(int, int)
057             *  @see #setInclude(boolean)
058             */
059            public NumericFilter(int lbound, int ubound, boolean incl){
060                lowerBound = lbound;
061                upperBound = ubound;
062                include = incl;
063            }
064    
065            /* The returned value is a NumericFilter */
066            public static Bean load(Preferences pref, String key){
067                Bean b =  Bean.load(pref, key);
068                if(! (b instanceof NumericFilter) ){
069                    b = null;
070                }
071                return b;
072            }
073    
074            /** Sets the filter bounds. The filter range will be
075             *  the open range (lbound;ubound). Use Double.NEGATIVE_INFINITY
076             *  and Double.POSITIVE_INFINITY where appropriate.
077             *  @param lbound Lower bound.
078             *  @param ubound Upper bound.
079             *  @see #getBounds()
080             */
081            public void setBounds(int lbound, int ubound){
082                setLowerBound(lbound);
083                setUpperBound(ubound);
084            }
085    
086            /** Queries the bounds of this filter.
087             *  @return An array that has the lower bound at index 0
088             *          and the upper bound at index 1;
089             *  @see #setBounds(int, int)
090             **/
091            public int[] getBounds(){
092                return new int[]{lowerBound, upperBound};
093            }
094    
095            /** Checks whether a given number is rejected or accepted
096             *  by this filter.
097             *  @param num the number to check.
098             *  @return true if num passes the filter false otherwise.
099             */
100            private boolean compare(Number num){
101                double dValue = num.doubleValue();
102                boolean isInRange = true;
103                //range is ]lowerBound;upperBound[
104                if (dValue <= lowerBound || dValue >= upperBound){
105                    isInRange = false;
106                }
107                // isInRange | include | result
108                //-----------+---------+-------
109                //   TRUE    |  EXCLUDE| FALSE
110                //   FALSE   |  INCLUDE| FALSE
111                //   TRUE    |  INCLUDE| TRUE
112                //   FALSE   |  EXCLUDE| TRUE
113                // (^ = XOR)
114                return !(isInRange ^ include);
115            }
116    
117            /** Compares val to the filter bounds if val is a number.
118             *  @param val the value to filter
119             *  @return null if val is numeric and NOT inside the filter bounds,
120             *          otherwise the unchanged object val is returned.
121             */
122            public Object filter(Object val){
123                Object o = myFilter(val);
124                //assert val == null || o == null || val.getClass().isInstance(o): "" + val + ";"+o;
125                return o;
126            }
127    
128            private Object myFilter(Object val){
129                return ((val instanceof Number) && ! compare((Number) val))
130                    ? null
131                    : val;
132            }
133    
134            public boolean equals(Object o){
135                if(this == o){
136                    return true;
137                }
138                boolean result = false;
139                if(o instanceof NumericFilter){
140                    NumericFilter other = (NumericFilter) o;
141                    result = (include == other.include);
142                    result = result && (lowerBound == other.lowerBound);
143                    result = result && (upperBound == other.upperBound);
144                    result = result && ((getName() == null)
145                                            ? (other.getName() == null)
146                                            : getName().equals(other.getName()));
147                }
148                return result;
149            }
150    
151            public int hashCode(){
152                int result = 19;
153                result = 43 * result + (include? 0 : 1);
154                result = 43 * result + lowerBound;
155                result = 43 * result + upperBound;
156                result = 43 * result +
157                        ((getName() == null)? 0 : getName().hashCode());
158                return result;
159            }
160    
161            /* Bound properties of this bean */
162            public void setLowerBound(int lowerBound){
163                int old = this.lowerBound;
164                this.lowerBound = lowerBound;
165                firePropertyChange("lowerBound", old, lowerBound);
166            }
167    
168            public int getLowerBound(){
169                return lowerBound;
170            }
171    
172            public void setUpperBound(int upperBound){
173                int old = this.upperBound;
174                this.upperBound = upperBound;
175                firePropertyChange("upperBound", old, upperBound);
176            }
177    
178            public int getUpperBound(){
179                return upperBound;
180            }
181    
182            /** Returns whether this filter currently
183             *  accepts or rejects numbers inside the
184             *  given numeric range.
185             *  @return incl one of INCLUDE and EXCLUDE
186             *  @see #setInclude(boolean)
187             */
188            public boolean getInclude(){
189                return include;
190            }
191    
192            /** Sets whether this filter accepts (INCLUDE)
193             *  or rejects (EXCLUDE) numbers inside the given
194             *  numeric range.
195             *  @param incl one of INCLUDE and EXCLUDE
196             *  @see #getInclude()
197             */
198            public void setInclude(boolean incl){
199                boolean old = include;
200                include = incl;
201                firePropertyChange("include", old, include);
202            }
203    
204    }