Community
Participate
Working Groups
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.
Created attachment 195725 [details] SharedInstanceBuffer
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