001    package net.sourceforge.mp3dings.filter;
002    
003    /* Mp3dings - manage mp3 meta-information
004    * Copyright (C) 2003 Moritz Ringler
005    * $Id: SubstringFilter.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.util.prefs.Preferences;
023    import net.sourceforge.mp3dings.utils.Bean;
024    
025    /** A filter that extracts a substring from a string.
026     * @author Moritz Ringler
027     * @version $Revision: 13 $ ($Date: 2007-07-27 11:52:39 +0200 (Fr, 27 Jul 2007) $)
028     **/
029    public class SubstringFilter extends AbstractStringFilter{
030        static final long serialVersionUID = 963946458589054825L;
031    
032        private int lowerIndex = 0;
033        private int upperIndex = -1;
034    
035        /** Constructs a filter with its indices initialized so that it
036        * returns all objects unchanged. **/
037        public SubstringFilter(){
038        }
039    
040        /** Constructs a new filter with the given indices.
041        * @param loi the lower index
042        * @param upi the upper index
043        * @see #setUpperIndex(int)
044        * @see #setLowerIndex(int)
045        **/
046        public SubstringFilter(int loi, int upi){
047            lowerIndex = loi;
048            upperIndex = upi;
049        }
050    
051    
052    
053        /** returns a SubstringFilter or null **/
054        public static Bean load(Preferences pref, String key){
055            Bean b =  Bean.load(pref, key);
056            if(! (b instanceof SubstringFilter) ){
057                b = null;
058            }
059            return b;
060        }
061    
062    
063        /** Sets the lower index used for substring filtering. The actual start
064        * index of each substring is determined as follows: If the lower index loi is
065        * positive the start index will be loi. If loi &lt 0 characters
066        * will be counted from the end of the String
067        * with its last character having index -1. A start index < 0 is
068        * equivalent to 0.
069        **/
070        public void setLowerIndex(int loi){
071            int old = lowerIndex;
072            lowerIndex = loi;
073            firePropertyChange("lowerIndex", old, loi);
074        }
075    
076        /** Gets the lower index of this filter.
077        * @see #setLowerIndex(int)
078        **/
079        public int getLowerIndex(){
080            return lowerIndex;
081        }
082    
083        /** Sets the upper index used for substring filtering. The actual stop
084        * index of each substring is determined as follows: If the upper index upi is
085        * positive the start index will be upi. If upi &lt 0 characters
086        * will be counted from the end of the String
087        * with its last character having index -1. A stop index greater than the
088        * length of the String s is equivalent to s.length().
089        **/
090        public void setUpperIndex(int upi){
091            int old = upperIndex;
092            upperIndex = upi;
093            firePropertyChange("upperIndex", old, upi);
094        }
095    
096        /** Gets the upper index of this filter.
097        * @see #setUpperIndex(int)
098        **/
099        public int getUpperIndex(){
100            return upperIndex;
101        }
102    
103        /** Sets both lower and upper indices of this filter.
104         * @param loi the lower index
105         * @param upi the upper index
106         * @see #setUpperIndex(int)
107         * @see #setLowerIndex(int)
108         **/
109        public void setIndices(int loi, int upi){
110            setLowerIndex(loi);
111            setUpperIndex(upi);
112        }
113    
114    
115        public String filterString(String s){
116            int len = s.length();
117            /* check if indices are negative (counting from end of String)
118             * and turn them into positive indices if necessary */
119            int loi = (lowerIndex >= 0)? lowerIndex: len + lowerIndex;
120            int upi = (upperIndex >= 0)? upperIndex: len + upperIndex;
121            /* increment upi to account for the fact that
122            *  String.substring(loi,upi) returns the String ending at
123            *  upi -1
124            */
125            upi = upi + 1;
126            /* check if indices are out of bounds */
127            if (upi > len){
128                upi = len;
129            }
130            if (loi < 0){
131                loi = 0;
132            }
133            /* check if the string passes the filter at all */
134            /* return the proper substring */
135            return (loi < upi)? s.substring(loi,upi) : null;
136        }
137    
138        public boolean equals(Object o){
139            if(this == o){
140                return true;
141            }
142            boolean result = false;
143            if(o instanceof SubstringFilter){
144                SubstringFilter other = (SubstringFilter) o;
145                result = result && (lowerIndex == other.lowerIndex);
146                result = result && (upperIndex == other.upperIndex);
147                result = result &&
148                        ((getName() == null)
149                            ? (other.getName() == null)
150                            : getName().equals(other.getName()));
151            }
152            return result;
153        }
154    
155        public int hashCode(){
156            int result = 19;
157            result = 43 * result + lowerIndex;
158            result = 43 * result + upperIndex;
159            result = 43 * result +
160                    ((getName() == null)? 0 : getName().hashCode());
161            return result;
162        }
163    }