Skip to content

Commit

Permalink
Merge pull request #9 from BorderTech/FactoryQualifiers
Browse files Browse the repository at this point in the history
Factory qualifiers
  • Loading branch information
jonathanaustin authored Jan 17, 2019
2 parents 7337cd3 + f6a2892 commit eef1dff
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,18 @@ public static <T> T getService(final Class<T> service, final Annotation... quali
* no binding.
*
* @param <T> the service class type
* @param <U> the default service implementation type
* @param service the service class
* @param defaultImpl the default implementation if an implementation is not found
* @param qualifiers the service qualifiers
* @return the implementation for this service and qualifiers or null if none available
*/
public static <T> T getService(final Class<T> service, final Class<T> defaultImpl, final Annotation... qualifiers) {
public static <T, U extends T> T getService(final Class<T> service, final Class<U> defaultImpl, final Annotation... qualifiers) {
// Provider
T impl = PROVIDER.getService(service, qualifiers);
// Fallback to basic factory
if (impl == null) {
impl = Factory.newInstance(service, buildFactoryQualifiers(qualifiers));
impl = Factory.newInstance(service, defaultImpl, buildFactoryQualifiers(qualifiers));
}
return impl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static <T> T newInstance(final Class<T> contract, final String... qualifi
* implementation defined.
*
* @param <T> the contract type
* @param <U> the default contract type
* @param <U> the default contract implementation type
* @param contract the contract to find and create new implementation
* @param defaultImpl the default implementation if an implementation is not found
* @param qualifiers the contract qualifiers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.github.bordertech.config.Config;
import java.lang.annotation.Annotation;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

/**
Expand Down Expand Up @@ -67,6 +67,12 @@ public void testNewInstanceWithImplQualifier() {
Assert.assertTrue("Should be an instanceof TestFactoryInterfaceImpl2", impl instanceof TestDidumsInterfaceImpl2);
}

@Test
public void testNewInstanceDefaultImpl() {
TestDidumsInterface impl = Didums.getService(TestDidumsInterface.class, TestDidumsInterfaceImpl.class);
Assert.assertTrue("Should be an instanceof the default impl", impl instanceof TestDidumsInterfaceImpl);
}

/**
* A test interface to use with the factory.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.bordertech.didums;

import com.github.bordertech.config.Config;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

/**
Expand Down Expand Up @@ -66,6 +66,12 @@ public void testNewInstanceWithImplQualifier() {
Assert.assertTrue("Should be an instanceof TestFactoryInterfaceImpl2", impl instanceof TestFactoryInterfaceImpl2);
}

@Test
public void testNewInstanceDefaultImpl() {
TestFactoryInterface impl = Factory.newInstance(TestFactoryInterface.class, TestFactoryInterfaceImpl.class);
Assert.assertTrue("Should be an instanceof the defualt impl", impl instanceof TestFactoryInterfaceImpl);
}

/**
* A test interface to use with the factory.
*/
Expand Down

0 comments on commit eef1dff

Please sign in to comment.