@Test(expected = IllegalStateException.class) public void conflict() { ApiStub<String> stub = new ApiStub<>("DEFAULT"); try (ApiStub.Reference<String> r0 = stub.activate("ANOTHER")) { try (ApiStub.Reference<String> r1 = stub.activate("CONFLICT")) { fail(); } } }
public Reference<T> activate(T implementation) { synchronized (this) { if (activeImplementation == null) { activeImplementation = implementation; referenceCount = 1; } else if (activeImplementation == implementation) { referenceCount++; } else { throw new IllegalStateException(MessageFormat.format( "Asakusa Framework API has conflict implementations (internal error): {0} <=> {1}", activeImplementation, implementation)); } return new Reference<>(this, implementation); } }
ApiStub { public Reference<T> activate(T implementation) { synchronized (this) { if (activeImplementation == null) { activeImplementation = implementation; referenceCount = 1; } else if (activeImplementation == implementation) { referenceCount++; } else { throw new IllegalStateException(MessageFormat.format( "Asakusa Framework API has conflict implementations (internal error): {0} <=> {1}", activeImplementation, implementation)); } return new Reference<>(this, implementation); } } }
ApiStub { public Reference<T> activate(T implementation) { synchronized (this) { if (activeImplementation == null) { activeImplementation = implementation; referenceCount = 1; } else if (activeImplementation == implementation) { referenceCount++; } else { throw new IllegalStateException(MessageFormat.format( "Asakusa Framework API has conflict implementations (internal error): {0} <=> {1}", activeImplementation, implementation)); } return new Reference<>(this, implementation); } } ApiStub();  ApiStub(T defaultImplementation); }
ApiStub { public Reference<T> activate(T implementation) { synchronized (this) { if (activeImplementation == null) { activeImplementation = implementation; referenceCount = 1; } else if (activeImplementation == implementation) { referenceCount++; } else { throw new IllegalStateException(MessageFormat.format( "Asakusa Framework API has conflict implementations (internal error): {0} <=> {1}", activeImplementation, implementation)); } return new Reference<>(this, implementation); } } ApiStub();  ApiStub(T defaultImplementation); T get(); Reference<T> activate(T implementation); }
ApiStub { public Reference<T> activate(T implementation) { synchronized (this) { if (activeImplementation == null) { activeImplementation = implementation; referenceCount = 1; } else if (activeImplementation == implementation) { referenceCount++; } else { throw new IllegalStateException(MessageFormat.format( "Asakusa Framework API has conflict implementations (internal error): {0} <=> {1}", activeImplementation, implementation)); } return new Reference<>(this, implementation); } } ApiStub();  ApiStub(T defaultImplementation); T get(); Reference<T> activate(T implementation);  }