Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 195725 Details for
Bug 345933
Introduce SharedInstanceBuffer
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
SharedInstanceBuffer
clipboard.txt (text/plain), 7.75 KB, created by
RĂ¼diger Herrmann
on 2011-05-16 08:43:46 EDT
(
hide
)
Description:
SharedInstanceBuffer
Filename:
MIME Type:
Creator:
RĂ¼diger Herrmann
Created:
2011-05-16 08:43:46 EDT
Size:
7.75 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.rap.rwt >Index: src/org/eclipse/rwt/internal/util/SharedInstanceBuffer.java >=================================================================== >RCS file: src/org/eclipse/rwt/internal/util/SharedInstanceBuffer.java >diff -N src/org/eclipse/rwt/internal/util/SharedInstanceBuffer.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/rwt/internal/util/SharedInstanceBuffer.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,48 @@ >+/******************************************************************************* >+ * Copyright (c) 2011 EclipseSource and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * EclipseSource - initial API and implementation >+ ******************************************************************************/ >+package org.eclipse.rwt.internal.util; >+ >+import java.util.*; >+ >+ >+ >+public class SharedInstanceBuffer { >+ >+ public interface IInstanceCreator { >+ Object createInstance(); >+ } >+ >+ private final Object lock; >+ private final Map store; >+ >+ public SharedInstanceBuffer() { >+ lock = new Object(); >+ store = new HashMap(); >+ } >+ >+ public Object get( Object key, IInstanceCreator instanceCreator ) { >+ ParamCheck.notNull( instanceCreator, "valueCreator" ); >+ synchronized( lock ) { >+ Object result = store.get( key ); >+ if( result == null ) { >+ result = instanceCreator.createInstance(); >+ store.put( key, result ); >+ } >+ return result; >+ } >+ } >+ >+ public Object remove( Object key ) { >+ synchronized( lock ) { >+ return store.remove( key ); >+ } >+ } >+} >Index: src/org/eclipse/swt/internal/graphics/ResourceFactory.java >=================================================================== >RCS file: /cvsroot/rt/org.eclipse.rap/runtime.rwt/org.eclipse.rap.rwt/src/org/eclipse/swt/internal/graphics/ResourceFactory.java,v >retrieving revision 1.34 >diff -u -r1.34 ResourceFactory.java >--- src/org/eclipse/swt/internal/graphics/ResourceFactory.java 12 Apr 2011 15:46:14 -0000 1.34 >+++ src/org/eclipse/swt/internal/graphics/ResourceFactory.java 16 May 2011 12:43:27 -0000 >@@ -15,18 +15,19 @@ > import java.util.HashMap; > import java.util.Map; > >-import org.eclipse.rwt.internal.util.ClassUtil; >+import org.eclipse.rwt.internal.util.*; >+import org.eclipse.rwt.internal.util.SharedInstanceBuffer.IInstanceCreator; > import org.eclipse.swt.graphics.*; > > > public class ResourceFactory { > >- private final Map colors; >+ private final SharedInstanceBuffer colors; > private final Map fonts; > private final Map cursors; > > public ResourceFactory() { >- colors = new HashMap(); >+ colors = new SharedInstanceBuffer(); > fonts = new HashMap(); > cursors = new HashMap(); > } >@@ -36,18 +37,13 @@ > return getColor( colorNr ); > } > >- private Color getColor( int value ) { >- Color result; >+ private Color getColor( final int value ) { > Integer key = new Integer( value ); >- synchronized( colors ) { >- if( colors.containsKey( key ) ) { >- result = ( Color )colors.get( key ); >- } else { >- result = createColorInstance( value ); >- colors.put( key, result ); >+ return ( Color )colors.get( key, new IInstanceCreator() { >+ public Object createInstance() { >+ return createColorInstance( value ); > } >- } >- return result; >+ } ); > } > > public Font getFont( FontData fontData ) { >#P org.eclipse.rap.rwt.test >Index: src/org/eclipse/rwt/internal/util/SharedInstanceBuffer_Test.java >=================================================================== >RCS file: src/org/eclipse/rwt/internal/util/SharedInstanceBuffer_Test.java >diff -N src/org/eclipse/rwt/internal/util/SharedInstanceBuffer_Test.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/rwt/internal/util/SharedInstanceBuffer_Test.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,124 @@ >+/******************************************************************************* >+ * Copyright (c) 2011 EclipseSource and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * EclipseSource - initial API and implementation >+ ******************************************************************************/ >+package org.eclipse.rwt.internal.util; >+ >+import junit.framework.TestCase; >+ >+import org.eclipse.rwt.internal.util.SharedInstanceBuffer.IInstanceCreator; >+ >+ >+public class SharedInstanceBuffer_Test extends TestCase { >+ >+ private static class TestValueCreator implements IInstanceCreator { >+ >+ private final Object value; >+ >+ TestValueCreator( Object value ) { >+ this.value = value; >+ } >+ >+ public Object createInstance() { >+ return value; >+ } >+ } >+ >+ private SharedInstanceBuffer keyValueStore; >+ >+ public void testGet() { >+ Object key = new Object(); >+ final Object value = new Object(); >+ >+ Object returnedValue = keyValueStore.get( key, new IInstanceCreator() { >+ public Object createInstance() { >+ return value; >+ } >+ } ); >+ >+ assertSame( returnedValue, value ); >+ } >+ >+ public void testGetWithExistingKey() { >+ final boolean[] createValueWasInvoked = { false }; >+ Object key = new Object(); >+ final Object value = new Object(); >+ keyValueStore.get( key, new TestValueCreator( value ) ); >+ >+ Object returnedValue = keyValueStore.get( key, new IInstanceCreator() { >+ public Object createInstance() { >+ createValueWasInvoked[ 0 ] = true; >+ return null; >+ } >+ } ); >+ >+ assertSame( returnedValue, value ); >+ assertFalse( createValueWasInvoked[ 0 ] ); >+ } >+ >+ public void testGetWithNullKey() { >+ IInstanceCreator valueCreator = new TestValueCreator( null ); >+ try { >+ keyValueStore.get( null, valueCreator ); >+ } catch( NullPointerException expected ) { >+ } >+ } >+ >+ public void testGetWithNullValueCreator() { >+ try { >+ keyValueStore.get( new Object(), null ); >+ } catch( NullPointerException expected ) { >+ } >+ } >+ >+ public void testRemoveNonExistingKey() { >+ Object removedValue = keyValueStore.remove( new Object() ); >+ >+ assertNull( removedValue ); >+ } >+ >+ public void testRemoveExistingKey() { >+ Object key = new Object(); >+ Object value = new Object(); >+ keyValueStore.get( key, new TestValueCreator( value ) ); >+ >+ Object removed = keyValueStore.remove( key ); >+ >+ assertSame( value, removed ); >+ } >+ >+ public void testRemoveWithNullKey() { >+ try { >+ keyValueStore.remove( null ); >+ } catch( NullPointerException expected ) { >+ } >+ } >+ >+ public void testGetAfterRemove() { >+ final boolean[] createValueWasInvoked = { false }; >+ Object key = new Object(); >+ final Object value = new Object(); >+ keyValueStore.get( key, new TestValueCreator( value ) ); >+ keyValueStore.remove( key ); >+ >+ Object returnedValue = keyValueStore.get( key, new IInstanceCreator() { >+ public Object createInstance() { >+ createValueWasInvoked[ 0 ] = true; >+ return value; >+ } >+ } ); >+ >+ assertSame( returnedValue, value ); >+ assertTrue( createValueWasInvoked[ 0 ] ); >+ } >+ >+ protected void setUp() throws Exception { >+ keyValueStore = new SharedInstanceBuffer(); >+ } >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 345933
: 195725