Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WFCORE-6347 Add convenience method to map a dependency to an alternate type. #5828

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}