001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of AutomataLib, http://www.automatalib.net/.
003 * 
004 * AutomataLib is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License version 3.0 as published by the Free Software Foundation.
007 * 
008 * AutomataLib is distributed in the hope that it will be useful,
009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 * Lesser General Public License for more details.
012 * 
013 * You should have received a copy of the GNU Lesser General Public
014 * License along with AutomataLib; if not, see
015 * http://www.gnu.de/documents/lgpl.en.html.
016 */
017package net.automatalib.commons.util.strings;
018
019import java.io.IOException;
020
021
022/**
023 * Class for transforming integer index values into string values (using
024 * latin characters, therefore effectively realizing a radix-26 representation
025 * of numbers).
026 * 
027 * @author Malte Isberner <malte.isberner@gmail.com>
028 *
029 */
030public class StringIndexGenerator {
031        public static enum Case {
032                LOWER,
033                UPPER
034        }
035        
036        private final char base;
037        
038        private char getChar(int idx) {
039                return (char)(base + idx);
040        }
041        
042        private static int getInteger(char c) {
043                c = Character.toLowerCase(c);
044                return c - 'a';
045        }
046        
047        public StringIndexGenerator(Case charCase) {
048                switch(charCase) {
049                case LOWER:
050                        this.base = 'a';
051                        break;
052                default: // case UPPER:
053                        this.base = 'A';
054                }
055        }
056        
057        public void appendStringIndex(Appendable a, int idx) throws IOException {
058                do {
059                        a.append(getChar(idx % 26));
060                        idx /= 26;
061                } while(idx > 0);
062        }
063        
064        public void appendStringIndex(StringBuilder sb, int idx) {
065                do {
066                        sb.append(getChar(idx % 26));
067                        idx /= 26;
068                } while(idx > 0);
069        }
070        
071        public String getStringIndex(int idx) {
072                StringBuilder sb = new StringBuilder();
073                appendStringIndex(sb, idx);
074                
075                return sb.toString();
076        }
077        
078        public static int getIntegerIndex(String sidx) {
079                int idx = 0;
080                int value = 1;
081                for(int i = 0; i < sidx.length(); i++) {
082                        char c = sidx.charAt(i);
083                        idx = idx + value*getInteger(c);
084                        value *= 26;
085                }
086                
087                return idx;
088        }
089
090}