Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 345933 - Introduce SharedInstanceBuffer
Summary: Introduce SharedInstanceBuffer
Status: RESOLVED FIXED
Alias: None
Product: RAP
Classification: RT
Component: RWT (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 1.5 M1   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-16 08:43 EDT by Rüdiger Herrmann CLA
Modified: 2011-05-19 03:24 EDT (History)
0 users

See Also:


Attachments
SharedInstanceBuffer (7.75 KB, patch)
2011-05-16 08:43 EDT, Rüdiger Herrmann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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