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.GwtCompatible;
020import com.google.common.annotations.VisibleForTesting;
021import java.util.Collections;
022import java.util.Map;
023
024/**
025 * An implementation-specific parameter class suitable for initializing {@link
026 * ArrayBasedCharEscaper} or {@link ArrayBasedUnicodeEscaper} instances. This class should be used
027 * when more than one escaper is created using the same character replacement mapping to allow the
028 * underlying (implementation specific) data structures to be shared.
029 *
030 * <p>The size of the data structure used by ArrayBasedCharEscaper and ArrayBasedUnicodeEscaper is
031 * proportional to the highest valued character that has a replacement. For example a replacement
032 * map containing the single character '{@literal \}u1000' will require approximately 16K of memory.
033 * As such sharing this data structure between escaper instances is the primary goal of this class.
034 *
035 * @author David Beaumont
036 * @since 15.0
037 */
038@GwtCompatible
039@ElementTypesAreNonnullByDefault
040public final class ArrayBasedEscaperMap {
041  /**
042   * Returns a new ArrayBasedEscaperMap for creating ArrayBasedCharEscaper or
043   * ArrayBasedUnicodeEscaper instances.
044   *
045   * @param replacements a map of characters to their escaped representations
046   */
047  public static ArrayBasedEscaperMap create(Map<Character, String> replacements) {
048    return new ArrayBasedEscaperMap(createReplacementArray(replacements));
049  }
050
051  // The underlying replacement array we can share between multiple escaper
052  // instances.
053  private final char[][] replacementArray;
054
055  private ArrayBasedEscaperMap(char[][] replacementArray) {
056    this.replacementArray = replacementArray;
057  }
058
059  // Returns the non-null array of replacements for fast lookup.
060  char[][] getReplacementArray() {
061    return replacementArray;
062  }
063
064  // Creates a replacement array from the given map. The returned array is a
065  // linear lookup table of replacement character sequences indexed by the
066  // original character value.
067  @VisibleForTesting
068  static char[][] createReplacementArray(Map<Character, String> map) {
069    checkNotNull(map); // GWT specific check (do not optimize)
070    if (map.isEmpty()) {
071      return EMPTY_REPLACEMENT_ARRAY;
072    }
073    char max = Collections.max(map.keySet());
074    char[][] replacements = new char[max + 1][];
075    for (Character c : map.keySet()) {
076      replacements[c] = map.get(c).toCharArray();
077    }
078    return replacements;
079  }
080
081  // Immutable empty array for when there are no replacements.
082  private static final char[][] EMPTY_REPLACEMENT_ARRAY = new char[0][0];
083}