001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.escape; 016 017import static com.google.common.base.Preconditions.checkNotNull; 018 019import com.google.common.annotations.Beta; 020import com.google.common.annotations.GwtCompatible; 021import com.google.common.annotations.VisibleForTesting; 022import java.util.Collections; 023import java.util.Map; 024 025/** 026 * An implementation-specific parameter class suitable for initializing {@link 027 * ArrayBasedCharEscaper} or {@link ArrayBasedUnicodeEscaper} instances. This class should be used 028 * when more than one escaper is created using the same character replacement mapping to allow the 029 * underlying (implementation specific) data structures to be shared. 030 * 031 * <p>The size of the data structure used by ArrayBasedCharEscaper and ArrayBasedUnicodeEscaper is 032 * proportional to the highest valued character that has a replacement. For example a replacement 033 * map containing the single character '{@literal \}u1000' will require approximately 16K of memory. 034 * As such sharing this data structure between escaper instances is the primary goal of this class. 035 * 036 * @author David Beaumont 037 * @since 15.0 038 */ 039@Beta 040@GwtCompatible 041@ElementTypesAreNonnullByDefault 042public final class ArrayBasedEscaperMap { 043 /** 044 * Returns a new ArrayBasedEscaperMap for creating ArrayBasedCharEscaper or 045 * ArrayBasedUnicodeEscaper instances. 046 * 047 * @param replacements a map of characters to their escaped representations 048 */ 049 public static ArrayBasedEscaperMap create(Map<Character, String> replacements) { 050 return new ArrayBasedEscaperMap(createReplacementArray(replacements)); 051 } 052 053 // The underlying replacement array we can share between multiple escaper 054 // instances. 055 private final char[][] replacementArray; 056 057 private ArrayBasedEscaperMap(char[][] replacementArray) { 058 this.replacementArray = replacementArray; 059 } 060 061 // Returns the non-null array of replacements for fast lookup. 062 char[][] getReplacementArray() { 063 return replacementArray; 064 } 065 066 // Creates a replacement array from the given map. The returned array is a 067 // linear lookup table of replacement character sequences indexed by the 068 // original character value. 069 @VisibleForTesting 070 static char[][] createReplacementArray(Map<Character, String> map) { 071 checkNotNull(map); // GWT specific check (do not optimize) 072 if (map.isEmpty()) { 073 return EMPTY_REPLACEMENT_ARRAY; 074 } 075 char max = Collections.max(map.keySet()); 076 char[][] replacements = new char[max + 1][]; 077 for (Character c : map.keySet()) { 078 replacements[c] = map.get(c).toCharArray(); 079 } 080 return replacements; 081 } 082 083 // Immutable empty array for when there are no replacements. 084 private static final char[][] EMPTY_REPLACEMENT_ARRAY = new char[0][0]; 085}