ACMNexus / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Add Interner #136

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
According to this thread, Google has an Interner class:

http://groups.google.com/group/google-collections-users/browse_thread/thread/598
9b42be75f9d5c#msg_140ae5d0060794ff

This would be useful and a welcome addition to the open-source
google-collections library.

Original issue reported on code.google.com by blair-ol...@orcaware.com on 7 Apr 2009 at 6:01

GoogleCodeExporter commented 8 years ago
Unfortunately, the status of this remains unchanged.  The interner 
implementation we 
use internally has a serious flaw that makes it unreleasable; we know how to go 
about 
reimplementing it; but we haven't had/made the time for it yet.

Original comment by kevin...@gmail.com on 12 Aug 2009 at 6:23

GoogleCodeExporter commented 8 years ago
:(
A bit disappointing, I think many of us were looking forward to that...

Original comment by jim.andreou on 12 Aug 2009 at 6:54

GoogleCodeExporter commented 8 years ago
Can you make the current code available and what needs to be done with it to 
fix the
flaw and somebody that wants it enough would do the correct implementation.  The
power of open-source :)

Original comment by blair-ol...@orcaware.com on 12 Aug 2009 at 7:43

GoogleCodeExporter commented 8 years ago
Sadly... I hate having to say this... but we are not yet set up to be able to 
accept 
code from non-Googlers.  The reasons are complicated internal technical ones 
that 
can't easily be explained.  We'll get there soon, I hope.

Original comment by kevin...@gmail.com on 12 Aug 2009 at 9:09

GoogleCodeExporter commented 8 years ago
is there anything hideously wrong with the following code for an interner for
immutable objects? a very simple interner, should be reasonably thread-save but 
maybe
not high-performance because it uses synchronizedMap instead of 
ConcurrentHashMap.

unfortuately, the [new MapMaker().weakKeys().makeMap()] does NOT work as 
expected. it
fails my testcasesand and crashes with OOME, it seems to hold references it 
should
not hold. this could be a seperate issue, or did i misread the javadocs?

public class Interner {
    private static final Map<Object, Object> WEAK_REFERERNCES
        = Collections.synchronizedMap(new WeakHashMap<Object, Object>());
//  private static final Map<Object, Object> WEAK_REFERERNCES
//  = new MapMaker().weakKeys().makeMap();

    public static <T> T intern(T newObject) {
        @SuppressWarnings("unckecked")
        final T cached = (T) WEAK_REFERERNCES.get(newObject);
        if (cached != null) {
            return cached;
        } else {
            WEAK_REFERERNCES.put(newObject, newObject);
            return newObject;
        }
    }
}

Original comment by heroldsi...@googlemail.com on 9 Sep 2009 at 3:17

GoogleCodeExporter commented 8 years ago
shouldn't you use putIfAbsent
(http://java.sun.com/javase/6/docs/api/java/util/concurrent/ConcurrentMap.html#p
utIfAbsent(K,%20V))
here?

Original comment by andre.ru...@gmail.com on 10 Sep 2009 at 8:49

GoogleCodeExporter commented 8 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 5:45

GoogleCodeExporter commented 8 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 5:57

GoogleCodeExporter commented 8 years ago
It looks like one could implement an interner using the JSR166 
CustomConcurrentHashMap

http://gee.cs.oswego.edu/dl/jsr166/dist/extra166ydocs/extra166y/CustomConcurrent
HashMap.html

See this thread discussing it:

http://concurrency.markmail.org/search/?q=intern#query:intern+page:1+mid:4pedmyv
l3llhqakn+state:results

Original comment by blair-ol...@orcaware.com on 25 Nov 2009 at 6:26

GoogleCodeExporter commented 8 years ago
This issue has been moved to the Guava project (keeping the same id number). 
Simply replace 'google-collections' with 'guava-libraries' in your address 
bar and it should take you there.

Original comment by kevinb@google.com on 5 Jan 2010 at 11:09