Source code
Description: source: package: org.apache.commons.lang3.text.translate, class: LookupTranslator

    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;
    022import java.util.HashSet;
    023
    024/**
    025 * Translates a value using a lookup table.
    026 *
    027 * @since 3.0
    028 * @deprecated As of 3.6, use Apache Commons Text
    029 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/LookupTranslator.html">
    030 * LookupTranslator</a> instead
    031 */
    032@Deprecated
    033public class LookupTranslator extends CharSequenceTranslator {
    034
    035    private final HashMap<String, String> lookupMap;
    036    private final HashSet<Character> prefixSet;
    037    private final int shortest;
    038    private final int longest;
    039
    040    /**
    041     * Define the lookup table to be used in translation
    042     *
    043     * Note that, as of Lang 3.1, the key to the lookup table is converted to a
    044     * java.lang.String. This is because we need the key to support hashCode and
    045     * equals(Object), allowing it to be the key for a HashMap. See LANG-882.
    046     *
    047     * @param lookup CharSequence[][] table of size [*][2]
    048     */
    049    public LookupTranslator(final CharSequence[]... lookup) {
    050        lookupMap = new HashMap<>();
    051        prefixSet = new HashSet<>();
    052        int tmpShortest = Integer.MAX_VALUE;
    053        int tmpLongest = 0;
    054        if (lookup != null) {
    055            for (final CharSequence[] seq : lookup) {
    056                this.lookupMap.put(seq[0].toString(), seq[1].toString());
    057                this.prefixSet.add(seq[0].charAt(0));
    058                final int sz = seq[0].length();
    059                if (sz < tmpShortest) {
    060                    tmpShortest = sz;
    061                }
    062                if (sz > tmpLongest) {
    063                    tmpLongest = sz;
    064                }
    065            }
    066        }
    067        this.shortest = tmpShortest;
    068        this.longest = tmpLongest;
    069    }
    070
    071    /**
    072     * {@inheritDoc}
    073     */
    074    @Override
    075    public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
    076        // check if translation exists for the input at position index
    077        if (prefixSet.contains(input.charAt(index))) {
    078            int max = longest;
    079            if (index + longest > input.length()) {
    080                max = input.length() - index;
    081            }
    082            // implement greedy algorithm by trying maximum match first
    083            for (int i = max; i >= shortest; i--) {
    084                final CharSequence subSeq = input.subSequence(index, index + i);
    085                final String result = lookupMap.get(subSeq.toString());
    086
    087                if (result != null) {
    088                    out.write(result);
    089                    return i;
    090                }
    091            }
    092        }
    093        return 0;
    094    }
    095}