001    /*
002    NGramJ - n-gram based text classification
003    Copyright (C) 2001 Frank S. Nestel (frank at spieleck.de)
004    
005    This program is free software; you can redistribute it and/or modify
006    it under the terms of the GNU Lesser General Public License as published 
007    by the Free Software Foundation; either version 2.1 of the License, or
008    (at your option) any later version.
009    
010    This program is distributed in the hope that it will be useful,
011    but WITHOUT ANY WARRANTY; without even the implied warranty of
012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013    GNU General Public License for more details.
014    
015    You should have received a copy of the GNU Lesser General Public License
016    along with this program (lesser.txt); if not, write to the Free Software
017    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018    */
019    
020    package de.spieleck.app.ngramj.phoner;
021    
022    import java.util.Hashtable;
023    
024    /**
025     * Efficient enumeration of PhoneNumberWords to a certain Number
026     */
027    public class PhonerEnum
028        extends PhoneKeys
029    {
030        protected int i, length;
031        protected byte[][] sets;
032        protected byte[] res;
033        protected int[] j;
034    
035        public PhonerEnum(String pnumber)
036            throws IllegalArgumentException
037        {
038            length = pnumber.length();
039            sets = new byte[length][];
040            for (i = 0; i < length; i++)
041            {
042                String ch = String.valueOf(pnumber.charAt(i));
043                sets[i] = (byte[])replacers.get(ch);
044                // Non existing characters are taken verbatim
045                if ( sets[i] == null )
046                    sets[i] = new byte[] { ch.getBytes()[0] };
047            }
048            res = new byte[length];
049            j = new int[length];
050            i = 1;
051            j[0] = -1;
052        }
053    
054        public byte[] next()
055        {
056            i--;
057            while ( i >= 0 )
058            {
059                j[i]++;
060                if ( j[i] >= sets[i].length )
061                    i--;
062                else
063                {
064                    res[i] = sets[i][j[i]];
065                    i++;
066                    if ( i == length )
067                    {
068                        return res;
069                    }
070                    else
071                    {
072                        j[i] = -1;
073                    }
074                }
075            }
076            return null;
077        }
078    }
079