001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import com.google.common.annotations.Beta;
020import com.google.common.annotations.GwtIncompatible;
021import com.google.errorprone.annotations.CanIgnoreReturnValue;
022import com.google.errorprone.annotations.DoNotMock;
023
024/**
025 * Provides similar behavior to {@link String#intern} for any immutable type. Common implementations
026 * are available from the {@link Interners} class.
027 *
028 * <p>Note that {@code String.intern()} has some well-known performance limitations, and should
029 * generally be avoided. Prefer {@link Interners#newWeakInterner} or another {@code Interner}
030 * implementation even for {@code String} interning.
031 *
032 * @author Kevin Bourrillion
033 * @since 3.0
034 */
035@Beta
036@DoNotMock("Use Interners.new*Interner")
037@GwtIncompatible
038public interface Interner<E> {
039  /**
040   * Chooses and returns the representative instance for any of a collection of instances that are
041   * equal to each other. If two {@linkplain Object#equals equal} inputs are given to this method,
042   * both calls will return the same instance. That is, {@code intern(a).equals(a)} always holds,
043   * and {@code intern(a) == intern(b)} if and only if {@code a.equals(b)}. Note that {@code
044   * intern(a)} is permitted to return one instance now and a different instance later if the
045   * original interned instance was garbage-collected.
046   *
047   * <p><b>Warning:</b> do not use with mutable objects.
048   *
049   * @throws NullPointerException if {@code sample} is null
050   */
051  @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
052  E intern(E sample);
053}