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
027 * {@link ArrayBasedCharEscaper} or {@link ArrayBasedUnicodeEscaper} instances. This class should be
028 * used when more than one escaper is created using the same character replacement mapping to allow
029 * the 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
041public final class ArrayBasedEscaperMap {
042  /**
043   * Returns a new ArrayBasedEscaperMap for creating ArrayBasedCharEscaper or
044   * ArrayBasedUnicodeEscaper instances.
045   *
046   * @param replacements a map of characters to their escaped representations
047   */
048  public static ArrayBasedEscaperMap create(Map<Character, String> replacements) {
049    return new ArrayBasedEscaperMap(createReplacementArray(replacements));
050  }
051
052  // The underlying replacement array we can share between multiple escaper
053  // instances.
054  private final char[][] replacementArray;
055
056  private ArrayBasedEscaperMap(char[][] replacementArray) {
057    this.replacementArray = replacementArray;
058  }
059
060  // Returns the non-null array of replacements for fast lookup.
061  char[][] getReplacementArray() {
062    return replacementArray;
063  }
064
065  // Creates a replacement array from the given map. The returned array is a
066  // linear lookup table of replacement character sequences indexed by the
067  // original character value.
068  @VisibleForTesting
069  static char[][] createReplacementArray(Map<Character, String> map) {
070    checkNotNull(map); // GWT specific check (do not optimize)
071    if (map.isEmpty()) {
072      return EMPTY_REPLACEMENT_ARRAY;
073    }
074    char max = Collections.max(map.keySet());
075    char[][] replacements = new char[max + 1][];
076    for (char c : map.keySet()) {
077      replacements[c] = map.get(c).toCharArray();
078    }
079    return replacements;
080  }
081
082  // Immutable empty array for when there are no replacements.
083  private static final char[][] EMPTY_REPLACEMENT_ARRAY = new char[0][0];
084}