(E sample)
| 70 | private final MapMakerInternalMap<E, Dummy> map = new MapMaker().weakKeys().keyEquivalence(Equivalence.equals()).makeCustomMap(); |
| 71 | |
| 72 | @Override |
| 73 | public E intern(E sample) { |
| 74 | while (true) { |
| 75 | // trying to read the canonical... |
| 76 | ReferenceEntry<E, Dummy> entry = map.getEntry(sample); |
| 77 | if (entry != null) { |
| 78 | E canonical = entry.getKey(); |
| 79 | if (canonical != null) { // only matters if weak/soft keys are used |
| 80 | return canonical; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // didn't see it, trying to put it instead... |
| 85 | Dummy sneaky = map.putIfAbsent(sample, Dummy.VALUE); |
| 86 | if (sneaky == null) { |
| 87 | return sample; |
| 88 | } else { |
| 89 | /* Someone beat us to it! Trying again... |
| 90 | * |
| 91 | * Technically this loop not guaranteed to terminate, so theoretically (extremely |
| 92 | * unlikely) this thread might starve, but even then, there is always going to be another |
| 93 | * thread doing progress here. |
| 94 | */} |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private enum Dummy { |
| 99 | VALUE |
nothing calls this directly
no test coverage detected