Skip to content

Commit

Permalink
Applied string based factory configurations to more factories, to imp…
Browse files Browse the repository at this point in the history
…rove startup performance
  • Loading branch information
rob-baillie-ortoo committed Apr 21, 2022
1 parent 75e4e54 commit 0dcd5ea
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ public inherited sharing class ortoo_AppLogicFactory // NOPMD: specified a mi
*/
public ortoo_AppLogicFactory()
{
this( new Map<Type,Type>() );
this( new Map<String,String>() );
}

/**
* Construct an instance using the given type to implementation mapping
*
* @param Map<Type,Type> The Type to Implementation mapping
* @param Map<String,String> The Type to Implementation mapping
*/
public ortoo_AppLogicFactory( Map<Type,Type> appLogicPerType )
public ortoo_AppLogicFactory( Map<String,String> appLogicPerType )
{
Contract.requires( appLogicPerType != null, 'ortoo_AppLogicFactory instantiated with a null appLogicPerType' );
factory = new ortoo_SimpleObjectFactory( appLogicPerType )

factory = new ortoo_SimpleObjectFactory( appLogicPerType )
.setTypeName( 'App Logic' )
.setErrorOnUnmappedType( false );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ public inherited sharing class ortoo_ChildRecordFinderFactory // NOPMD: speci
*/
public ortoo_ChildRecordFinderFactory()
{
this( new Map<Type,Type>() );
this( new Map<String,String>() );
}

/**
* Construct an instance using the given type to implementation mapping
*
* @param Map<Type,Type> The Type to Implementation mapping
* @param Map<String,String> The Type to Implementation mapping
*/
public ortoo_ChildRecordFinderFactory( Map<Type,Type> childRecordFinderPerType )
public ortoo_ChildRecordFinderFactory( Map<String,String> childRecordFinderPerType )
{
Contract.requires( childRecordFinderPerType != null, 'ortoo_ChildRecordFinderFactory instantiated with a null childRecordFinderPerType' );


factory = new ortoo_SimpleObjectFactory( childRecordFinderPerType )
.setTypeName( 'Child Finder' )
.setErrorOnUnmappedType( false );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ public inherited sharing class ortoo_MessageRendererFactory // NOPMD: specifi
*/
public ortoo_MessageRendererFactory()
{
this( new Map<Type,Type>() );
this( new Map<String,String>() );
}

/**
* Construct an instance using the given type to implementation mapping
*
* @param Map<Type,Type> The Type to Implementation mapping
* @param Map<String,String> The Type to Implementation mapping
*/
public ortoo_MessageRendererFactory( Map<Type,Type> implementationPerType )
public ortoo_MessageRendererFactory( Map<String,String> implementationPerType )
{
Contract.requires( implementationPerType != null, 'ortoo_MessageRendererFactory instantiated with a null implementationPerType' );
factory = new ortoo_SimpleObjectFactory( implementationPerType )

factory = new ortoo_SimpleObjectFactory( implementationPerType )
.setTypeName( 'Message Renderer' )
.setErrorOnUnmappedType( false );
}
Expand All @@ -50,7 +51,7 @@ public inherited sharing class ortoo_MessageRendererFactory // NOPMD: specifi
}
catch ( TypeException e )
{
throw new Exceptions.DeveloperException( 'Implementation registered for Message Renderer ' + requestedType + ' does not implement MessageRendererEngine.IMessageRenderer', e )
throw new Exceptions.DeveloperException( 'Implementation registered for Message Renderer ' + requestedType + ' ('+ ObjectUtils.getClassName( rawRenderer ) +') does not implement MessageRendererEngine.IMessageRenderer', e )
.setErrorCode( FrameworkErrorCodes.MR_IMPLEMENTATION_IS_NOT_MR )
.addContext( 'requestedType', requestedType );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public inherited sharing class ortoo_SimpleObjectFactory // NOPMD: specified
return mockByType.get( requestedType );
}

String requestedTypeName = requestedType.getName();
String requestedTypeName = ObjectUtils.stripLocalNamespace( requestedType.getName() );

if ( errorOnUnmappedType && ! implementationByType.containsKey( requestedType ) && ! implementationNameByTypeName.containsKey( requestedTypeName ) )
{
Expand All @@ -110,10 +110,11 @@ public inherited sharing class ortoo_SimpleObjectFactory // NOPMD: specified
}
else if ( implementationNameByTypeName.containsKey( requestedTypeName ) )
{
typeToConstruct = Type.forName( implementationNameByTypeName.get( requestedTypeName ) );
String typeToConstructName = implementationNameByTypeName.get( requestedTypeName );
typeToConstruct = Type.forName( typeToConstructName );
if ( typeToConstruct == null )
{
throw new Exceptions.DeveloperException( 'Invalid implementation registered for ' + typeName + ' ' + requestedTypeName + ': The class does not exist, or is not visible' )
throw new Exceptions.DeveloperException( 'Invalid implementation registered for ' + typeName + ' ' + requestedTypeName + ': ' + typeToConstructName + ' does not exist, or is not visible' )
.setErrorCode( FrameworkErrorCodes.FACTORY_INVALID_IMPLEMENTATION_REGISTERED )
.addContext( 'typeName', typeName )
.addContext( 'requestedType', requestedType );
Expand All @@ -128,7 +129,7 @@ public inherited sharing class ortoo_SimpleObjectFactory // NOPMD: specified
}
catch ( Exception e )
{
throw new Exceptions.DeveloperException( 'Implementation registered for ' + typeName + ' ' + requestedType + ':' + typeToConstruct + ' does not have parameterless constructor', e )
throw new Exceptions.DeveloperException( 'Implementation registered for ' + typeName + ' ' + requestedType + ':' + typeToConstruct + ' does not have parameterless constructor or cannot be constructed', e )
.setErrorCode( FrameworkErrorCodes.FACTORY_NO_PARAMETERLESS_CONSTRUCTOR )
.addContext( 'typeName', typeName )
.addContext( 'requestedType', requestedType )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ private without sharing class ortoo_AppLogicFactoryTest
private static void newInstance_whenMappingsAreDefined_willReturnTheMappedType() // NOPMD: Test method name format
{
ortoo_AppLogicFactory factory = new ortoo_AppLogicFactory(
new Map<Type,Type>
new Map<String,String>
{
Object.class => RegisterableType.class
'ortoo_AppLogicFactoryTest.ClassToRetrieveVia' => 'ortoo_AppLogicFactoryTest.RegisterableType'
}
);

Test.startTest();
Object returnedInstance = factory.newInstance( Object.class );
Object returnedInstance = factory.newInstance( ClassToRetrieveVia.class );
Test.stopTest();

System.assertNotEquals( null, returnedInstance, 'newInstance, when mappings are defined and a mapped type is mapped, will return an instantiated object' );
Expand All @@ -40,8 +40,8 @@ private without sharing class ortoo_AppLogicFactoryTest
RegisterableType mockInstance = new RegisterableType( 'mockone' );

Test.startTest();
factory.setMock( Object.class, mockInstance );
Object returnedInstance = factory.newInstance( Object.class );
factory.setMock( ClassToRetrieveVia.class, mockInstance );
Object returnedInstance = factory.newInstance( ClassToRetrieveVia.class );
Test.stopTest();

System.assertEquals( mockInstance, returnedInstance, 'newInstance, when called for a type that has a mock set, will return the registered mock' );
Expand Down Expand Up @@ -85,7 +85,9 @@ private without sharing class ortoo_AppLogicFactoryTest
ortoo_Asserts.assertContains( 'ortoo_AppLogicFactory instantiated with a null appLogicPerType', exceptionMessage, 'constructor, when called with null, will throw an exception' );
}

private without sharing class RegisterableType
public without sharing class ClassToRetrieveVia {}

public without sharing class RegisterableType
{
String name;
public RegisterableType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ private without sharing class ortoo_ChildRecordFinderFactoryTest
private static void newInstance_whenMappingsAreDefined_willReturnTheMappedType() // NOPMD: Test method name format
{
ortoo_ChildRecordFinderFactory factory = new ortoo_ChildRecordFinderFactory(
new Map<Type,Type>
new Map<String,String>
{
Object.class => RegisterableType.class
'ortoo_ChildRecordFinderFactoryTest.ClassToRetrieveVia' => 'ortoo_ChildRecordFinderFactoryTest.RegisterableType'
}
);

Test.startTest();
IChildRecordFinder returnedInstance = factory.newInstance( Object.class );
IChildRecordFinder returnedInstance = factory.newInstance( ClassToRetrieveVia.class );
Test.stopTest();

System.assertNotEquals( null, returnedInstance, 'newInstance, when mappings are defined and a mapped type is mapped, will return an instantiated object' );
Expand All @@ -40,8 +40,8 @@ private without sharing class ortoo_ChildRecordFinderFactoryTest
RegisterableType mockInstance = new RegisterableType( 'mockone' );

Test.startTest();
factory.setMock( Object.class, mockInstance );
IChildRecordFinder returnedInstance = factory.newInstance( Object.class );
factory.setMock( ClassToRetrieveVia.class, mockInstance );
IChildRecordFinder returnedInstance = factory.newInstance( ClassToRetrieveVia.class );
Test.stopTest();

System.assertEquals( mockInstance, returnedInstance, 'newInstance, when called for a type that has a mock set, will return the registered mock' );
Expand Down Expand Up @@ -71,9 +71,9 @@ private without sharing class ortoo_ChildRecordFinderFactoryTest
private static void newInstance_whenCalledForAnMappedTypeThatIsNotAChildFinder_willThrowAnException() // NOPMD: Test method name format
{
ortoo_ChildRecordFinderFactory factory = new ortoo_ChildRecordFinderFactory(
new Map<Type,Type>
new Map<String,String>
{
Object.class => NonRegisterableType.class
'Object' => 'ortoo_ChildRecordFinderFactoryTest.NonRegisterableType'
}
);

Expand Down Expand Up @@ -130,7 +130,9 @@ private without sharing class ortoo_ChildRecordFinderFactoryTest
ortoo_Asserts.assertContains( 'ortoo_ChildRecordFinderFactory instantiated with a null childRecordFinderPerType', exceptionMessage, 'constructor, when called with null, will throw an exception' );
}

private without sharing class RegisterableType implements IChildRecordFinder
public without sharing class ClassToRetrieveVia {}

public without sharing class RegisterableType implements IChildRecordFinder
{
String name;
public RegisterableType()
Expand All @@ -149,5 +151,5 @@ private without sharing class ortoo_ChildRecordFinderFactoryTest
}
}

private without sharing class NonRegisterableType {}
public without sharing class NonRegisterableType {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ private without sharing class ortoo_MessageRendererFactoryTest
IRenderableMessageHeader message = (IRenderableMessageHeader)messageController.generateDouble();

ortoo_MessageRendererFactory factory = new ortoo_MessageRendererFactory(
new Map<Type,Type>
new Map<String,String>
{
Object.class => RegisterableType.class
'ortoo_MessageRendererFactoryTest.ClassToRetrieveVia' => 'ortoo_MessageRendererFactoryTest.RegisterableType'
}
);

Test.startTest();
MessageRendererEngine returnedInstance = factory.newInstance( Object.class, message );
MessageRendererEngine returnedInstance = factory.newInstance( ClassToRetrieveVia.class, message );
Test.stopTest();

System.assertNotEquals( null, returnedInstance, 'newInstance, when mappings are defined and a mapped type is passed, will return an instantiated object' );
Expand Down Expand Up @@ -95,25 +95,25 @@ private without sharing class ortoo_MessageRendererFactoryTest
IRenderableMessageHeader message = (IRenderableMessageHeader)messageController.generateDouble();

ortoo_MessageRendererFactory factory = new ortoo_MessageRendererFactory(
new Map<Type,Type>
new Map<String,String>
{
Object.class => NonRegisterableType.class
'ortoo_MessageRendererFactoryTest.ClassToRetrieveVia' => 'ortoo_MessageRendererFactoryTest.NonRegisterableType'
}
);

Test.startTest();
String exceptionMessage;
try
{
factory.newInstance( Object.class, message );
factory.newInstance( ClassToRetrieveVia.class, message );
}
catch ( Exception e )
{
exceptionMessage = e.getMessage();
}
Test.stopTest();

ortoo_Asserts.assertContains( 'Object does not implement MessageRendererEngine.IMessageRenderer', exceptionMessage, 'newInstance, when called for a mapped type that does not implement IMessageRenderer, will throw an exception' );
ortoo_Asserts.assertContains( 'NonRegisterableType) does not implement MessageRendererEngine.IMessageRenderer', exceptionMessage, 'newInstance, when called for a mapped type that does not implement IMessageRenderer, will throw an exception' );
}

@isTest
Expand Down Expand Up @@ -179,13 +179,15 @@ private without sharing class ortoo_MessageRendererFactoryTest
ortoo_Asserts.assertContains( 'ortoo_MessageRendererFactory instantiated with a null implementationPerType', exceptionMessage, 'constructor, when called with null, will throw an exception' );
}

private without sharing class RegisterableType implements IMessageRenderer
public without sharing class ClassToRetrieveVia {}

public without sharing class RegisterableType implements IMessageRenderer
{
public Boolean render( IRenderableMessageHeader message )
{
return true;
}
}

private without sharing class NonRegisterableType {}
public without sharing class NonRegisterableType {}
}
Loading

0 comments on commit 0dcd5ea

Please sign in to comment.