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 * @version $Id: LookupTranslator.java 1669520 2015-03-27 08:03:41Z britter $ 029 */ 030public class LookupTranslator extends CharSequenceTranslator { 031 032 private final HashMap<String, String> lookupMap; 033 private final HashSet<Character> prefixSet; 034 private final int shortest; 035 private final int longest; 036 037 /** 038 * Define the lookup table to be used in translation 039 * 040 * Note that, as of Lang 3.1, the key to the lookup table is converted to a 041 * java.lang.String. This is because we need the key to support hashCode and 042 * equals(Object), allowing it to be the key for a HashMap. See LANG-882. 043 * 044 * @param lookup CharSequence[][] table of size [*][2] 045 */ 046 public LookupTranslator(final CharSequence[]... lookup) { 047 lookupMap = new HashMap<String, String>(); 048 prefixSet = new HashSet<Character>(); 049 int _shortest = Integer.MAX_VALUE; 050 int _longest = 0; 051 if (lookup != null) { 052 for (final CharSequence[] seq : lookup) { 053 this.lookupMap.put(seq[0].toString(), seq[1].toString()); 054 this.prefixSet.add(seq[0].charAt(0)); 055 final int sz = seq[0].length(); 056 if (sz < _shortest) { 057 _shortest = sz; 058 } 059 if (sz > _longest) { 060 _longest = sz; 061 } 062 } 063 } 064 shortest = _shortest; 065 longest = _longest; 066 } 067 068 /** 069 * {@inheritDoc} 070 */ 071 @Override 072 public int translate(final CharSequence input, final int index, final Writer out) throws IOException { 073 // check if translation exists for the input at position index 074 if (prefixSet.contains(input.charAt(index))) { 075 int max = longest; 076 if (index + longest > input.length()) { 077 max = input.length() - index; 078 } 079 // implement greedy algorithm by trying maximum match first 080 for (int i = max; i >= shortest; i--) { 081 final CharSequence subSeq = input.subSequence(index, index + i); 082 final String result = lookupMap.get(subSeq.toString()); 083 084 if (result != null) { 085 out.write(result); 086 return i; 087 } 088 } 089 } 090 return 0; 091 } 092}