Skip to content

Commit

Permalink
Merge pull request #5828 from pferraro/main
Browse files Browse the repository at this point in the history
WFCORE-6347 Add convenience method to map a dependency to an alternate type.
  • Loading branch information
pferraro authored Jan 12, 2024
2 parents e28e52a + 7592b1a commit 837bbc1
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 8 deletions.
14 changes: 14 additions & 0 deletions service/src/main/java/org/wildfly/service/Dependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
*/
public interface Dependency<B extends ServiceBuilder<?>, V> extends Consumer<B>, Supplier<V> {

default <R> Dependency<B, R> map(Function<V, R> mapper) {
return new Dependency<>() {
@Override
public void accept(B builder) {
Dependency.this.accept(builder);
}

@Override
public R get() {
return mapper.apply(Dependency.this.get());
}
};
}

class SimpleDependency<B extends ServiceBuilder<?>, V> implements Dependency<B, V> {

private final V value;
Expand Down
17 changes: 17 additions & 0 deletions service/src/main/java/org/wildfly/service/ServiceDependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package org.wildfly.service;

import java.util.function.Function;

import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceName;

Expand All @@ -13,6 +15,21 @@
*/
public interface ServiceDependency<V> extends Dependency<ServiceBuilder<?>, V> {

@Override
default <R> ServiceDependency<R> map(Function<V, R> mapper) {
return new ServiceDependency<>() {
@Override
public void accept(ServiceBuilder<?> builder) {
ServiceDependency.this.accept(builder);
}

@Override
public R get() {
return mapper.apply(ServiceDependency.this.get());
}
};
}

/**
* Returns a pseudo-dependency supplying a fixed value.
* @return a dependency supplier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;

import java.util.function.Function;
import java.util.function.Supplier;

import org.jboss.msc.service.ServiceBuilder;
Expand All @@ -24,27 +26,66 @@ public class ServiceDependencyTestCase {
@Test
public void simple() {
ServiceBuilder<?> builder = mock(ServiceBuilder.class);
Object value = new Object();
String value = "foo";

ServiceDependency<Object> dependency = ServiceDependency.of(value);

dependency.accept(builder);

verifyNoInteractions(builder);

Assert.assertSame(value, dependency.get());

ServiceDependency<Object> mapped = dependency.map(Functions.cast(Function.identity()));

mapped.accept(builder);

verifyNoInteractions(builder);

Assert.assertSame(value, mapped.get());

ServiceDependency<String> cast = mapped.map(String.class::cast);

cast.accept(builder);

verifyNoInteractions(builder);

Assert.assertSame(value, cast.get());
}

@Test
public void service() {
ServiceBuilder<?> builder = mock(ServiceBuilder.class);
ServiceName name = ServiceName.JBOSS;
Supplier<Object> injection = Functions.constantSupplier(new Object());
Supplier<String> injection1 = Functions.constantSupplier("foo");
Supplier<String> injection2 = Functions.constantSupplier("bar");
Supplier<String> injection3 = Functions.constantSupplier("qux");

ServiceDependency<Object> dependency = ServiceDependency.on(name);

doReturn(injection).when(builder).requires(name);
doReturn(injection1, injection2, injection3).when(builder).requires(name);

dependency.accept(builder);

Assert.assertSame(injection.get(), dependency.get());
Assert.assertSame(injection1.get(), dependency.get());

ServiceDependency<Object> mapped = dependency.map(Functions.cast(Function.identity()));

Assert.assertSame(injection1.get(), mapped.get());

mapped.accept(builder);

Assert.assertSame(injection2.get(), mapped.get());
Assert.assertSame(injection2.get(), dependency.get());

ServiceDependency<String> cast = mapped.map(String.class::cast);

Assert.assertSame(injection2.get(), cast.get());

cast.accept(builder);

Assert.assertSame(injection3.get(), cast.get());
Assert.assertSame(injection3.get(), mapped.get());
Assert.assertSame(injection3.get(), dependency.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import org.jboss.as.controller.RequirementServiceBuilder;
Expand All @@ -23,6 +24,21 @@
*/
public interface ServiceDependency<V> extends Dependency<RequirementServiceBuilder<?>, V> {

@Override
default <R> ServiceDependency<R> map(Function<V, R> mapper) {
return new ServiceDependency<>() {
@Override
public void accept(RequirementServiceBuilder<?> builder) {
ServiceDependency.this.accept(builder);
}

@Override
public R get() {
return mapper.apply(ServiceDependency.this.get());
}
};
}

/**
* Returns a dependency on the service with the specified name.
* @param <T> the dependency type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;

import java.util.function.Function;
import java.util.function.Supplier;

import org.jboss.as.controller.RequirementServiceBuilder;
Expand All @@ -24,27 +26,66 @@ public class ServiceDependencyTestCase {
@Test
public void simple() {
RequirementServiceBuilder<?> builder = mock(RequirementServiceBuilder.class);
Object value = new Object();
String value = "foo";

ServiceDependency<Object> dependency = ServiceDependency.of(value);

dependency.accept(builder);

verifyNoInteractions(builder);

Assert.assertSame(value, dependency.get());

ServiceDependency<Object> mapped = dependency.map(Functions.cast(Function.identity()));

mapped.accept(builder);

verifyNoInteractions(builder);

Assert.assertSame(value, mapped.get());

ServiceDependency<String> cast = mapped.map(String.class::cast);

cast.accept(builder);

verifyNoInteractions(builder);

Assert.assertSame(value, cast.get());
}

@Test
public void service() {
RequirementServiceBuilder<?> builder = mock(RequirementServiceBuilder.class);
ServiceName name = ServiceName.JBOSS;
Supplier<Object> injection = Functions.constantSupplier(new Object());
Supplier<String> injection1 = Functions.constantSupplier("foo");
Supplier<String> injection2 = Functions.constantSupplier("bar");
Supplier<String> injection3 = Functions.constantSupplier("qux");

ServiceDependency<Object> dependency = ServiceDependency.on(name);

doReturn(injection).when(builder).requires(name);
doReturn(injection1, injection2, injection3).when(builder).requires(name);

dependency.accept(builder);

Assert.assertSame(injection.get(), dependency.get());
Assert.assertSame(injection1.get(), dependency.get());

ServiceDependency<Object> mapped = dependency.map(Functions.cast(Function.identity()));

Assert.assertSame(injection1.get(), mapped.get());

mapped.accept(builder);

Assert.assertSame(injection2.get(), mapped.get());
Assert.assertSame(injection2.get(), dependency.get());

ServiceDependency<String> cast = mapped.map(String.class::cast);

Assert.assertSame(injection2.get(), cast.get());

cast.accept(builder);

Assert.assertSame(injection3.get(), cast.get());
Assert.assertSame(injection3.get(), mapped.get());
Assert.assertSame(injection3.get(), dependency.get());
}
}

0 comments on commit 837bbc1

Please sign in to comment.