From: Robert Varga Date: Wed, 2 Jul 2014 19:57:11 +0000 (+0200) Subject: BUG-1281: optimize ControllerContext X-Git-Tag: release/helium~556^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=43ee3f6bb0d69d1171cd998616740f37ef435c47 BUG-1281: optimize ControllerContext - use a single instanceof of Splitter - do not instantiate unneeded collections - static methods should be static - do not use string concat in LOG.error() - do not use String.split() Change-Id: I16d2232ab2f9ee86f64f7abc95d9b7ce073f0c22 Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java index 7a16105056..85c8e59539 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java @@ -18,7 +18,6 @@ import com.google.common.collect.BiMap; import com.google.common.collect.FluentIterable; import com.google.common.collect.HashBiMap; import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; import java.io.UnsupportedEncodingException; import java.net.URI; @@ -83,6 +82,10 @@ public class ControllerContext implements SchemaContextListener { private final static String URI_ENCODING_CHAR_SET = "ISO-8859-1"; + private static final Splitter SLASH_SPLITTER = Splitter.on('/'); + + private static final Splitter COLON_SPLITTER = Splitter.on(':'); + private final BiMap uriToModuleName = HashBiMap. create(); private final Map moduleNameToUri = uriToModuleName.inverse(); @@ -129,10 +132,8 @@ public class ControllerContext implements SchemaContextListener { final boolean toMountPointIdentifier ) { this.checkPreconditions(); - Iterable split = Splitter.on( "/" ).split( restconfInstance ); - final ArrayList encodedPathArgs = Lists. newArrayList( split ); - final List pathArgs = this.urlPathArgsDecode( encodedPathArgs ); - this.omitFirstAndLastEmptyString( pathArgs ); + final List pathArgs = urlPathArgsDecode( SLASH_SPLITTER.split( restconfInstance ) ); + omitFirstAndLastEmptyString( pathArgs ); if( pathArgs.isEmpty() ) { return null; } @@ -158,7 +159,7 @@ public class ControllerContext implements SchemaContextListener { return iiWithSchemaNode; } - private List omitFirstAndLastEmptyString( final List list ) { + private static List omitFirstAndLastEmptyString( final List list ) { if( list.isEmpty() ) { return list; } @@ -562,7 +563,7 @@ public class ControllerContext implements SchemaContextListener { } String head = strings.iterator().next(); - final String nodeName = this.toNodeName( head ); + final String nodeName = toNodeName( head ); final String moduleName = ControllerContext.toModuleName( head ); DataSchemaNode targetNode = null; @@ -839,20 +840,20 @@ public class ControllerContext implements SchemaContextListener { private static String toModuleName( final String str ) { Preconditions. checkNotNull( str ); - if( str.contains( ":" ) ) { - final String[] args = str.split( ":" ); - if( args.length == 2 ) { - return args[0]; + if( str.indexOf( ':' ) != -1 ) { + final Iterable args = COLON_SPLITTER.split( str ); + if( Iterables.size( args ) == 2 ) { + return args.iterator().next(); } } return null; } - private String toNodeName( final String str ) { - if( str.contains( ":" ) ) { - final String[] args = str.split( ":" ); - if( args.length == 2 ) { - return args[1]; + private static String toNodeName( final String str ) { + if( str.indexOf( ':' ) != -1 ) { + final Iterable args = COLON_SPLITTER.split( str ); + if( Iterables.size( args ) == 2 ) { + return Iterables.get( args, 1 ); } } return str; @@ -860,7 +861,7 @@ public class ControllerContext implements SchemaContextListener { private QName toQName( final String name ) { final String module = toModuleName( name ); - final String node = this.toNodeName( name ); + final String node = toNodeName( name ); Set modules = globalSchema.getModules(); final Comparator comparator = new Comparator() { @@ -916,7 +917,7 @@ public class ControllerContext implements SchemaContextListener { } } - public List urlPathArgsDecode( final List strings ) { + public static List urlPathArgsDecode( final Iterable strings ) { try { List decodedPathArgs = new ArrayList(); for( final String pathArg : strings ) { @@ -998,7 +999,7 @@ public class ControllerContext implements SchemaContextListener { try { builder.append( this.toUriString( keyValues.get( key ) ) ); } catch( UnsupportedEncodingException e ) { - LOG.error( "Error parsing URI: " + keyValues.get( key ), e ); + LOG.error( "Error parsing URI: {}", keyValues.get( key ), e ); return null; } }