From e2d8aad01f63d71343b970b1b22a16878d8201de Mon Sep 17 00:00:00 2001 From: GoToLoop Date: Sat, 9 Jul 2016 06:25:50 -0300 Subject: [PATCH 1/4] Change Vector to ArrayList Since __*ArrayList*__ isn't safe like __*Vector*__, I'm also adding `synchronized ()` at places where either of its structure's **size()** changes. Those 6 methods are: **add()**, **addDrawable()**, **remove()**, **removeDrawable()**, **clear()** and **clearDrawable()**. Of course, all loops traversing this class still needs to be synchronized externally as well; either over **get()** or **getDrawables()**. Last word: This patch is just a standalone performant refactoring. It's not obligatory for __*ControllerGroup*__'s previous patch. --- src/controlP5/ControllerList.java | 92 +++++++++++++++++++------------ 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/src/controlP5/ControllerList.java b/src/controlP5/ControllerList.java index ddc9f78..c87433e 100755 --- a/src/controlP5/ControllerList.java +++ b/src/controlP5/ControllerList.java @@ -1,5 +1,3 @@ -package controlP5; - /** * controlP5 is a processing gui library. * @@ -22,47 +20,64 @@ package controlP5; * @author Andreas Schlegel (http://www.sojamo.de) * @modified ##date## * @version ##version## - * */ +package controlP5; + import java.util.List; -import java.util.Vector; +import java.util.ArrayList; +import java.util.RandomAccess; /** * Stores objects of type ControllerInterface and CDrawable, mainly for internal use. */ -public class ControllerList { +public class ControllerList implements RandomAccess { + protected final List< ControllerInterface< ? >> controllers = new ArrayList< >( ); + protected final List< CDrawable > drawables = new ArrayList< >( ); - protected List< ControllerInterface< ? >> controllers; - - protected List< CDrawable > drawables; - - public ControllerList( ) { - controllers = new Vector< ControllerInterface< ? >>( ); - drawables = new Vector< CDrawable >( ); - } - - public void add( ControllerInterface< ? > theController ) { - if ( controllers.indexOf( theController ) < 0 ) { + public ControllerList add( final ControllerInterface< ? > theController ) { + if ( !contains( theController ) ) synchronized ( controllers ) { controllers.add( theController ); } + return this; } - protected void remove( ControllerInterface< ? > theController ) { - controllers.remove( theController ); - } - - protected void addDrawable( CDrawable theController ) { - if ( drawables.indexOf( theController ) < 0 ) { + public ControllerList addDrawable( final CDrawable theController ) { + if ( !containsDrawable( theController ) ) synchronized ( drawables ) { drawables.add( theController ); } + return this; } - protected void removeDrawable( CDrawable theController ) { - drawables.remove( theController ); + public ControllerList remove( final ControllerInterface< ? > theController ) { + if ( !isEmpty( ) ) synchronized ( controllers ) { + controllers.remove( theController ); + } + return this; } - public ControllerInterface< ? > get( int theIndex ) { + public ControllerList removeDrawable( final CDrawable theController ) { + if ( !isEmptyDrawable( ) ) synchronized ( drawables ) { + drawables.remove( theController ); + } + return this; + } + + public ControllerList clear( ) { + if ( !isEmpty( ) ) synchronized ( controllers ) { + controllers.clear( ); + } + return this; + } + + public ControllerList clearDrawable( ) { + if ( !isEmptyDrawable( ) ) synchronized ( drawables ) { + drawables.clear( ); + } + return this; + } + + public ControllerInterface< ? > get( final int theIndex ) { return controllers.get( theIndex ); } @@ -70,7 +85,7 @@ public class ControllerList { return controllers; } - public CDrawable getDrawable( int theIndex ) { + public CDrawable getDrawable( final int theIndex ) { return drawables.get( theIndex ); } @@ -78,20 +93,27 @@ public class ControllerList { return drawables; } - public int sizeDrawable( ) { - return drawables.size( ); + public boolean contains( final ControllerInterface< ? > theController ) { + return controllers.contains( theController ); + } + + public boolean containsDrawable( final CDrawable theController ) { + return drawables.contains( theController ); + } + + public boolean isEmpty( ) { + return controllers.isEmpty( ); + } + + public boolean isEmptyDrawable( ) { + return drawables.isEmpty( ); } public int size( ) { return controllers.size( ); } - protected void clear( ) { - controllers.clear( ); + public int sizeDrawable( ) { + return drawables.size( ); } - - protected void clearDrawable( ) { - drawables.clear( ); - } - } From 7b1b52f2d4ad3c7d2ed1b8671aead8a21b87cced Mon Sep 17 00:00:00 2001 From: GoToLoop Date: Sat, 9 Jul 2016 08:24:55 -0300 Subject: [PATCH 2/4] Update ControllerList.java --- src/controlP5/ControllerList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controlP5/ControllerList.java b/src/controlP5/ControllerList.java index c87433e..abbf413 100755 --- a/src/controlP5/ControllerList.java +++ b/src/controlP5/ControllerList.java @@ -32,7 +32,7 @@ import java.util.RandomAccess; * Stores objects of type ControllerInterface and CDrawable, mainly for internal use. */ public class ControllerList implements RandomAccess { - protected final List< ControllerInterface< ? >> controllers = new ArrayList< >( ); + protected final List< ControllerInterface< ? > > controllers = new ArrayList< >( ); protected final List< CDrawable > drawables = new ArrayList< >( ); public ControllerList add( final ControllerInterface< ? > theController ) { From f611e4d653ce32e378eabb05fa55eb70a29a0d6a Mon Sep 17 00:00:00 2001 From: GoToLoop Date: Sat, 9 Jul 2016 15:44:03 -0300 Subject: [PATCH 3/4] Update ControllerList.java __*ControllerInterface< ? extends ControllerInterface< ? >*__ --- src/controlP5/ControllerList.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/controlP5/ControllerList.java b/src/controlP5/ControllerList.java index abbf413..6d1e510 100755 --- a/src/controlP5/ControllerList.java +++ b/src/controlP5/ControllerList.java @@ -32,10 +32,10 @@ import java.util.RandomAccess; * Stores objects of type ControllerInterface and CDrawable, mainly for internal use. */ public class ControllerList implements RandomAccess { - protected final List< ControllerInterface< ? > > controllers = new ArrayList< >( ); + protected final List< ControllerInterface< ? extends ControllerInterface< ? > > > controllers = new ArrayList< >( ); protected final List< CDrawable > drawables = new ArrayList< >( ); - public ControllerList add( final ControllerInterface< ? > theController ) { + public ControllerList add( final ControllerInterface< ? extends ControllerInterface< ? > > theController ) { if ( !contains( theController ) ) synchronized ( controllers ) { controllers.add( theController ); } @@ -49,7 +49,7 @@ public class ControllerList implements RandomAccess { return this; } - public ControllerList remove( final ControllerInterface< ? > theController ) { + public ControllerList remove( final ControllerInterface< ? extends ControllerInterface< ? > > theController ) { if ( !isEmpty( ) ) synchronized ( controllers ) { controllers.remove( theController ); } @@ -77,11 +77,11 @@ public class ControllerList implements RandomAccess { return this; } - public ControllerInterface< ? > get( final int theIndex ) { + public ControllerInterface< ? extends ControllerInterface< ? > > get( final int theIndex ) { return controllers.get( theIndex ); } - public List< ControllerInterface< ? >> get( ) { + public List< ControllerInterface< ? extends ControllerInterface< ? > > > get( ) { return controllers; } @@ -93,7 +93,7 @@ public class ControllerList implements RandomAccess { return drawables; } - public boolean contains( final ControllerInterface< ? > theController ) { + public boolean contains( final ControllerInterface< ? extends ControllerInterface< ? > > theController ) { return controllers.contains( theController ); } From 4fd4105367d0233033aa53743abcb6153bed67f6 Mon Sep 17 00:00:00 2001 From: GoToLoop Date: Sat, 9 Jul 2016 22:09:59 -0300 Subject: [PATCH 4/4] Update ControllerList.java --- src/controlP5/ControllerList.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controlP5/ControllerList.java b/src/controlP5/ControllerList.java index 6d1e510..242b825 100755 --- a/src/controlP5/ControllerList.java +++ b/src/controlP5/ControllerList.java @@ -32,7 +32,7 @@ import java.util.RandomAccess; * Stores objects of type ControllerInterface and CDrawable, mainly for internal use. */ public class ControllerList implements RandomAccess { - protected final List< ControllerInterface< ? extends ControllerInterface< ? > > > controllers = new ArrayList< >( ); + protected final List< ControllerInterface< ? > > controllers = new ArrayList< >( ); protected final List< CDrawable > drawables = new ArrayList< >( ); public ControllerList add( final ControllerInterface< ? extends ControllerInterface< ? > > theController ) { @@ -81,7 +81,7 @@ public class ControllerList implements RandomAccess { return controllers.get( theIndex ); } - public List< ControllerInterface< ? extends ControllerInterface< ? > > > get( ) { + public List< ControllerInterface< ? > > get( ) { return controllers; }