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 */
017package org.apache.commons.lang3.text.translate;
018
019import java.io.IOException;
020import java.io.Writer;
021import java.util.HashMap;
022
023/**
024 * Translates a value using a lookup table.
025 *
026 * @since 3.0
027 * @version $Id: LookupTranslator.java 1470822 2013-04-23 06:00:41Z bayard $
028 */
029public class LookupTranslator extends CharSequenceTranslator {
030
031    private final HashMap<String, 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     * Note that, as of Lang 3.1, the key to the lookup table is converted to a 
039     * java.lang.String, while the value remains as a java.lang.CharSequence. 
040     * This is because we need the key to support hashCode and equals(Object), 
041     * allowing it to be the key for a HashMap. See LANG-882.
042     *
043     * @param lookup CharSequence[][] table of size [*][2]
044     */
045    public LookupTranslator(final CharSequence[]... lookup) {
046        lookupMap = new HashMap<String, CharSequence>();
047        int _shortest = Integer.MAX_VALUE;
048        int _longest = 0;
049        if (lookup != null) {
050            for (final CharSequence[] seq : lookup) {
051                this.lookupMap.put(seq[0].toString(), seq[1]);
052                final int sz = seq[0].length();
053                if (sz < _shortest) {
054                    _shortest = sz;
055                }
056                if (sz > _longest) {
057                    _longest = sz;
058                }
059            }
060        }
061        shortest = _shortest;
062        longest = _longest;
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
070        int max = longest;
071        if (index + longest > input.length()) {
072            max = input.length() - index;
073        }
074        // descend so as to get a greedy algorithm
075        for (int i = max; i >= shortest; i--) {
076            final CharSequence subSeq = input.subSequence(index, index + i);
077            final CharSequence result = lookupMap.get(subSeq.toString());
078            if (result != null) {
079                out.write(result.toString());
080                return i;
081            }
082        }
083        return 0;
084    }
085}