dnrajugade / guava-libraries

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

Interner - Optional value "compressor" #1179

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be useful to add an optional "compressor" Function<E,E> to Interners.
This function would be called to adapt the physical representation of a value 
just before it is actually stored/retained in the internal map. However it 
would not be called if the value is already interned to avoid unnecessary 
re-allocation.

See 
https://groups.google.com/forum/?fromgroups=#!topic/guava-discuss/6VZYRWaz0y4

Maybe something like that:

@Override public E intern(E sample) {

      boolean compressed = false; //

      while (true) {
        // trying to read the canonical...
        ReferenceEntry<E, Dummy> entry = map.getEntry(sample);
        if (entry != null) {
          E canonical = entry.getKey();
          if (canonical != null) { // only matters if weak/soft keys are used
            return canonical;
          }
        }

        if (compressor != null && !compressed) {  //  
            sample = compressor.apply(sample);    //
            compressed = true;                    //
        }                                         //

        // didn't see it, trying to put it instead...
        Dummy sneaky = map.putIfAbsent(sample, Dummy.VALUE);
        if (sneaky == null) {
          return sample;
        } else {

          /* Someone beat us to it! Trying again...
           *
           * Technically this loop not guaranteed to terminate, so theoretically (extremely
           * unlikely) this thread might starve, but even then, there is always going to be another
           * thread doing progress here.
           */
        }
      }
    }

Original issue reported on code.google.com by amas...@gmail.com on 27 Oct 2012 at 2:40

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 8 Apr 2013 at 7:00

GoogleCodeExporter commented 9 years ago

Original comment by kak@google.com on 22 Aug 2013 at 11:11

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:18

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08