Returns a new thread-safe interner which retains a strong reference to each instance it has interned, thus preventing these instances from being garbage-collected. If this retention is acceptable, this implementation may perform better than #newWeakInterner.
()
| 43 | |
| 44 | |
| 45 | public static <E> Interner<E> newStrongInterner() { |
| 46 | final ConcurrentMap<E, E> map = new MapMaker().makeMap(); |
| 47 | return new Interner<E>() { |
| 48 | @Override |
| 49 | public E intern(E sample) { |
| 50 | E canonical = map.putIfAbsent(checkNotNull(sample), sample); |
| 51 | return (canonical == null) ? sample : canonical; |
| 52 | } |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns a new thread-safe interner which retains a weak reference to each instance it has |