001/* 002 * Copyright (C) 2007 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.collect; 016 017import static com.google.common.base.Preconditions.checkNotNull; 018 019import com.google.common.annotations.Beta; 020import com.google.common.annotations.GwtIncompatible; 021import com.google.common.annotations.VisibleForTesting; 022import com.google.common.base.Equivalence; 023import com.google.common.base.Function; 024import com.google.common.collect.MapMaker.Dummy; 025import com.google.common.collect.MapMakerInternalMap.InternalEntry; 026 027/** 028 * Contains static methods pertaining to instances of {@link Interner}. 029 * 030 * @author Kevin Bourrillion 031 * @since 3.0 032 */ 033@Beta 034@GwtIncompatible 035public final class Interners { 036 private Interners() {} 037 038 /** 039 * Builder for {@link Interner} instances. 040 * 041 * @since 21.0 042 */ 043 public static class InternerBuilder { 044 private final MapMaker mapMaker = new MapMaker(); 045 private boolean strong = true; 046 047 private InternerBuilder() { 048 } 049 050 /** 051 * Instructs the {@link InternerBuilder} to build a strong interner. 052 * 053 * @see Interners#newStrongInterner() 054 */ 055 public InternerBuilder strong() { 056 this.strong = true; 057 return this; 058 } 059 060 /** 061 * Instructs the {@link InternerBuilder} to build a weak interner. 062 * 063 * @see Interners#newWeakInterner() 064 */ 065 @GwtIncompatible("java.lang.ref.WeakReference") 066 public InternerBuilder weak() { 067 this.strong = false; 068 return this; 069 } 070 071 /** 072 * Sets the concurrency level that will be used by the to-be-built {@link Interner}. 073 * 074 * @see MapMaker#concurrencyLevel(int) 075 */ 076 public InternerBuilder concurrencyLevel(int concurrencyLevel) { 077 this.mapMaker.concurrencyLevel(concurrencyLevel); 078 return this; 079 } 080 081 public <E> Interner<E> build() { 082 if (!strong) { 083 mapMaker.weakKeys(); 084 } 085 return new InternerImpl<E>(mapMaker); 086 } 087 } 088 089 /** Returns a fresh {@link InternerBuilder} instance. */ 090 public static InternerBuilder newBuilder() { 091 return new InternerBuilder(); 092 } 093 094 /** 095 * Returns a new thread-safe interner which retains a strong reference to each instance it has 096 * interned, thus preventing these instances from being garbage-collected. If this retention is 097 * acceptable, this implementation may perform better than {@link #newWeakInterner}. 098 */ 099 public static <E> Interner<E> newStrongInterner() { 100 return newBuilder().strong().build(); 101 } 102 103 /** 104 * Returns a new thread-safe interner which retains a weak reference to each instance it has 105 * interned, and so does not prevent these instances from being garbage-collected. This most 106 * likely does not perform as well as {@link #newStrongInterner}, but is the best alternative 107 * when the memory usage of that implementation is unacceptable. 108 */ 109 @GwtIncompatible("java.lang.ref.WeakReference") 110 public static <E> Interner<E> newWeakInterner() { 111 return newBuilder().weak().build(); 112 } 113 114 @VisibleForTesting 115 static final class InternerImpl<E> implements Interner<E> { 116 // MapMaker is our friend, we know about this type 117 @VisibleForTesting 118 final MapMakerInternalMap<E, Dummy, ?, ?> map; 119 120 private InternerImpl(MapMaker mapMaker) { 121 this.map = MapMakerInternalMap.createWithDummyValues( 122 mapMaker.keyEquivalence(Equivalence.equals())); 123 } 124 125 @Override 126 public E intern(E sample) { 127 while (true) { 128 // trying to read the canonical... 129 InternalEntry<E, Dummy, ?> entry = map.getEntry(sample); 130 if (entry != null) { 131 E canonical = entry.getKey(); 132 if (canonical != null) { // only matters if weak/soft keys are used 133 return canonical; 134 } 135 } 136 137 // didn't see it, trying to put it instead... 138 Dummy sneaky = map.putIfAbsent(sample, Dummy.VALUE); 139 if (sneaky == null) { 140 return sample; 141 } else { 142 /* Someone beat us to it! Trying again... 143 * 144 * Technically this loop not guaranteed to terminate, so theoretically (extremely 145 * unlikely) this thread might starve, but even then, there is always going to be another 146 * thread doing progress here. 147 */ 148 } 149 } 150 } 151 } 152 153 /** 154 * Returns a function that delegates to the {@link Interner#intern} method of the given interner. 155 * 156 * @since 8.0 157 */ 158 public static <E> Function<E, E> asFunction(Interner<E> interner) { 159 return new InternerFunction<E>(checkNotNull(interner)); 160 } 161 162 private static class InternerFunction<E> implements Function<E, E> { 163 164 private final Interner<E> interner; 165 166 public InternerFunction(Interner<E> interner) { 167 this.interner = interner; 168 } 169 170 @Override 171 public E apply(E input) { 172 return interner.intern(input); 173 } 174 175 @Override 176 public int hashCode() { 177 return interner.hashCode(); 178 } 179 180 @Override 181 public boolean equals(Object other) { 182 if (other instanceof InternerFunction) { 183 InternerFunction<?> that = (InternerFunction<?>) other; 184 return interner.equals(that.interner); 185 } 186 187 return false; 188 } 189 } 190}