Coverage Summary for Class: ArrayBasedEscaperMap (com.google.common.escape)

Class Class, % Method, % Line, %
ArrayBasedEscaperMap 0% (0/1) 0% (0/5) 0% (0/14)


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 {@link 27  * ArrayBasedCharEscaper} or {@link ArrayBasedUnicodeEscaper} instances. This class should be used 28  * when more than one escaper is created using the same character replacement mapping to allow the 29  * 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 @ElementTypesAreNonnullByDefault 42 public final class ArrayBasedEscaperMap { 43  /** 44  * Returns a new ArrayBasedEscaperMap for creating ArrayBasedCharEscaper or 45  * ArrayBasedUnicodeEscaper instances. 46  * 47  * @param replacements a map of characters to their escaped representations 48  */ 49  public static ArrayBasedEscaperMap create(Map<Character, String> replacements) { 50  return new ArrayBasedEscaperMap(createReplacementArray(replacements)); 51  } 52  53  // The underlying replacement array we can share between multiple escaper 54  // instances. 55  private final char[][] replacementArray; 56  57  private ArrayBasedEscaperMap(char[][] replacementArray) { 58  this.replacementArray = replacementArray; 59  } 60  61  // Returns the non-null array of replacements for fast lookup. 62  char[][] getReplacementArray() { 63  return replacementArray; 64  } 65  66  // Creates a replacement array from the given map. The returned array is a 67  // linear lookup table of replacement character sequences indexed by the 68  // original character value. 69  @VisibleForTesting 70  static char[][] createReplacementArray(Map<Character, String> map) { 71  checkNotNull(map); // GWT specific check (do not optimize) 72  if (map.isEmpty()) { 73  return EMPTY_REPLACEMENT_ARRAY; 74  } 75  char max = Collections.max(map.keySet()); 76  char[][] replacements = new char[max + 1][]; 77  for (Character c : map.keySet()) { 78  replacements[c] = map.get(c).toCharArray(); 79  } 80  return replacements; 81  } 82  83  // Immutable empty array for when there are no replacements. 84  private static final char[][] EMPTY_REPLACEMENT_ARRAY = new char[0][0]; 85 }