001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.lang3.text.translate;
018    
019    import java.io.IOException;
020    import java.io.Writer;
021    import java.util.HashMap;
022    
023    /**
024     * Translates a value using a lookup table.
025     * 
026     * @since 3.0
027     * @version $Id: LookupTranslator.java 1091096 2011-04-11 15:07:29Z mbenson $
028     */
029    public class LookupTranslator extends CharSequenceTranslator {
030    
031        private final HashMap<CharSequence, CharSequence> lookupMap;
032        private final int shortest;
033        private final int longest;
034    
035        /**
036         * Define the lookup table to be used in translation
037         *
038         * @param lookup CharSequence[][] table of size [*][2]
039         */
040        public LookupTranslator(CharSequence[]... lookup) {
041            lookupMap = new HashMap<CharSequence, CharSequence>();
042            int _shortest = Integer.MAX_VALUE;
043            int _longest = 0;
044            if (lookup != null) {
045                for (CharSequence[] seq : lookup) {
046                    this.lookupMap.put(seq[0], seq[1]);
047                    int sz = seq[0].length();
048                    if (sz < _shortest) {
049                        _shortest = sz;
050                    }
051                    if (sz > _longest) {
052                        _longest = sz;
053                    }
054                }
055            }
056            shortest = _shortest;
057            longest = _longest;
058        }
059    
060        /**
061         * {@inheritDoc}
062         */
063        @Override
064        public int translate(CharSequence input, int index, Writer out) throws IOException {
065            int max = longest;
066            if (index + longest > input.length()) {
067                max = input.length() - index;
068            }
069            // descend so as to get a greedy algorithm
070            for (int i = max; i >= shortest; i--) {
071                CharSequence subSeq = input.subSequence(index, index + i);
072                CharSequence result = lookupMap.get(subSeq);
073                if (result != null) {
074                    out.write(result.toString());
075                    return i;
076                }
077            }
078            return 0;
079        }
080    }