Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 345933

Summary: Introduce SharedInstanceBuffer
Product: [RT] RAP Reporter: Rüdiger Herrmann <ruediger.herrmann>
Component: RWTAssignee: Project Inbox <rap-inbox>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: P3    
Version: unspecified   
Target Milestone: 1.5 M1   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
SharedInstanceBuffer none

Description Rüdiger Herrmann CLA 2011-05-16 08:43:20 EDT
In several places throughout the RWT code base the same pattern is used to buffer a shared instance under a key:
* lookup the key in a map
* if not contained, 
  a) create the instance
  b) put the key - instance pair into the map
* return the instance for the given key

I suggest to rework these occurences to make use of the attached class. The SharedInstanceBuffer provides a get() method that accepts a key and an IInstanceCreator. If there is already an instance for a given it, it is simple returned. Otherwise the IInstanceCreator is consulted to create an instance. This instance is then buffered for later use and returned.

In ResourceFactory#getFont() for example the code is like the following:
  Font font = ( Font )fonts.get( key );
  if( font == null ) {
    font = createFontInstance( fontData );
    fonts.put( key, font );
  }
and could be reworked like this:
  return ( Font )fonts.get( key, new IInstanceCreator() {
    public Object createInstance() {
      return createFontInstance( fontData );
    }
  } );

A quick search reveals the places listed below:
* ResourceFactory#colors, #fonts, #cursors
* InternalImageFactory#cache
* ImageFactory#cache
* ImageDataCache#cache (?)
* SingletonManager#singletons, #typeLocks (not yet in HEAD, see patch for bug 345702)
* AdapterFactoryRegistry#registry
* LifeCycleAdapterFactory#widgetAdapters
* WidgetLCAUtil#parsedFonts
* FontDataFactory

Since all places found so far have to be thread-safe, the current implementation of SharedInstanceBuffer is also thread-safe. As an example for how the SharedInstanceBuffer is inteded to be used, the ResourceFactory was changed to use the KeyValueStore to manage its colors.
Comment 1 Rüdiger Herrmann CLA 2011-05-16 08:43:46 EDT
Created attachment 195725 [details]
SharedInstanceBuffer
Comment 2 Rüdiger Herrmann CLA 2011-05-19 03:24:24 EDT
Put SharedInstanceBuffer in use in the classes listed in the description, except for these:
* ImageDataCache and AdapterFactoryRegistry, SharedInstanceBuffer does not fit here
* LifeCycleAdapterFactory, postponed until bug bug 346089 is resolved
Changes are in CVS HEAD