View Javadoc
1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.escape;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import com.google.common.annotations.Beta;
20  import com.google.common.annotations.GwtCompatible;
21  import com.google.common.annotations.VisibleForTesting;
22  import java.util.Collections;
23  import java.util.Map;
24  
25  /**
26   * An implementation-specific parameter class suitable for initializing
27   * {@link ArrayBasedCharEscaper} or {@link ArrayBasedUnicodeEscaper} instances. This class should be
28   * used when more than one escaper is created using the same character replacement mapping to allow
29   * the underlying (implementation specific) data structures to be shared.
30   *
31   * <p>The size of the data structure used by ArrayBasedCharEscaper and ArrayBasedUnicodeEscaper is
32   * proportional to the highest valued character that has a replacement. For example a replacement
33   * map containing the single character '{@literal \}u1000' will require approximately 16K of memory.
34   * As such sharing this data structure between escaper instances is the primary goal of this class.
35   *
36   * @author David Beaumont
37   * @since 15.0
38   */
39  @Beta
40  @GwtCompatible
41  public final class ArrayBasedEscaperMap {
42    /**
43     * Returns a new ArrayBasedEscaperMap for creating ArrayBasedCharEscaper or
44     * ArrayBasedUnicodeEscaper instances.
45     *
46     * @param replacements a map of characters to their escaped representations
47     */
48    public static ArrayBasedEscaperMap create(Map<Character, String> replacements) {
49      return new ArrayBasedEscaperMap(createReplacementArray(replacements));
50    }
51  
52    // The underlying replacement array we can share between multiple escaper
53    // instances.
54    private final char[][] replacementArray;
55  
56    private ArrayBasedEscaperMap(char[][] replacementArray) {
57      this.replacementArray = replacementArray;
58    }
59  
60    // Returns the non-null array of replacements for fast lookup.
61    char[][] getReplacementArray() {
62      return replacementArray;
63    }
64  
65    // Creates a replacement array from the given map. The returned array is a
66    // linear lookup table of replacement character sequences indexed by the
67    // original character value.
68    @VisibleForTesting
69    static char[][] createReplacementArray(Map<Character, String> map) {
70      checkNotNull(map); // GWT specific check (do not optimize)
71      if (map.isEmpty()) {
72        return EMPTY_REPLACEMENT_ARRAY;
73      }
74      char max = Collections.max(map.keySet());
75      char[][] replacements = new char[max + 1][];
76      for (char c : map.keySet()) {
77        replacements[c] = map.get(c).toCharArray();
78      }
79      return replacements;
80    }
81  
82    // Immutable empty array for when there are no replacements.
83    private static final char[][] EMPTY_REPLACEMENT_ARRAY = new char[0][0];
84  }