commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jack, Paul" <pj...@sfaf.org>
Subject RE: [SUBMIT][Collections] ReferenceMap, take 2
Date Tue, 28 May 2002 15:48:22 GMT
> Just an idea....is it possible to write ReferenceMap as a 
> wrapper around
> another Map? If so, then we could have ReferenceSortedMap, 
> ReferenceList
> etc. all within ReferenceCollections.

Yes, but not efficiently.  For weak or soft keys, you'd have
to create a bunch of Reference objects that you don't actually
need:

   public Object get(Object key) {
      key = toReference(keyType, key);
      Object value = underlyingMap.get(key);
      if (valueType > HARD) return ((Reference)value).get();
      return value;
   }

Also I'm not convinced it's possible to write the iterator
effectively:  The hasNext() method must guarantee that the
next call to next() will return a valid object; however, the
Reference to that object might have cleared between the
call to hasNext() and next().  (Though I haven't spent too
much time thinking about this problem, there may be perfectly
sound solutions to it).

Though it would be nice to have a referenced SequencedHashMap
or LRUMap.

Also purging elements from an arbitrary list would be an
expensive operation (you'd have to traverse the entire list
looking for the purged element, and then invoke remove(index),
which would traverse the list again for a sequential list or
require a memory copy for a random access list).

> Also, in your tests I noted that you call System.gc() to 
> cause the garbage
> collector to run. Does this not just 'suggest' that the gc be 
> run, thus
> making the tests unreliable?

It's true.  The tests should use their own ReferenceQueue
to wait until the objects have been GC'd.  I'll modify the
tests accordingly.

-Paul


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message