001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.util;
020
021import org.apache.hadoop.classification.InterfaceAudience;
022import org.apache.hadoop.classification.InterfaceStability;
023
024import com.google.common.collect.Interner;
025import com.google.common.collect.Interners;
026
027/**
028 * Provides string interning utility methods. For weak interning,
029 * we use the standard String.intern() call, that performs very well
030 * (no problems with PermGen overflowing, etc.) starting from JDK 7.
031 */
032@InterfaceAudience.Public
033@InterfaceStability.Stable
034public class StringInterner {
035  
036  /**
037   * Retains a strong reference to each string instance it has interned.
038   */
039  private final static Interner<String> STRONG_INTERNER =
040      Interners.newStrongInterner();
041
042  /**
043   * Interns and returns a reference to the representative instance 
044   * for any of a collection of string instances that are equal to each other.
045   * Retains strong reference to the instance, 
046   * thus preventing it from being garbage-collected. 
047   * 
048   * @param sample string instance to be interned
049   * @return strong reference to interned string instance
050   */
051  public static String strongIntern(String sample) {
052    if (sample == null) {
053      return null;
054    }
055    return STRONG_INTERNER.intern(sample);
056  }
057  
058  /**
059   * Interns and returns a reference to the representative instance 
060   * for any of a collection of string instances that are equal to each other.
061   * Retains weak reference to the instance, 
062   * and so does not prevent it from being garbage-collected.
063   * 
064   * @param sample string instance to be interned
065   * @return weak reference to interned string instance
066   */
067  public static String weakIntern(String sample) {
068    if (sample == null) {
069      return null;
070    }
071    return sample.intern();
072  }
073
074  /**
075   * Interns all the strings in the given array in place,
076   * returning the same array.
077   */
078  public static String[] internStringsInArray(String[] strings) {
079    for (int i = 0; i < strings.length; i++) {
080      strings[i] = weakIntern(strings[i]);
081    }
082    return strings;
083  }
084
085}