    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     * @author Apache Software Foundation
    027     * @since 3.0
    028     * @version $Id: LookupTranslator.java 967237 2010-07-23 20:08:57Z mbenson $
    029     */
    030    // TODO: Replace with a RegexLookup? Performance test.
    031    public class LookupTranslator extends CharSequenceTranslator {
    032
    033        private final HashMap<CharSequence, CharSequence> lookupMap;
    034        private final int shortest;
    035        private final int longest;
    036
    037        /**
    038         * Define the lookup table to be used in translation
    039         *
    040         * @param lookup CharSequence[][] table of size [*][2]
    041         */
    042        public LookupTranslator(CharSequence[][] lookup) {
    043            lookupMap = new HashMap<CharSequence, CharSequence>();
    044            int _shortest = Integer.MAX_VALUE;
    045            int _longest = 0;
    046            for(CharSequence[] seq : lookup) {
    047                this.lookupMap.put(seq[0], seq[1]);
    048                int sz = seq[0].length();
    049                if(sz < _shortest) {
    050                    _shortest = sz;
    051                }
    052                if(sz > _longest) {
    053                    _longest = sz;
    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    }