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-6975 Add convenience method to combine a ServiceDependency with another. #6157

Merged
merged 5 commits into from
Sep 5, 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
16 changes: 8 additions & 8 deletions service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -45,17 +45,17 @@
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<scope>test</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
72 changes: 72 additions & 0 deletions service/src/main/java/org/wildfly/service/Dependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package org.wildfly.service;

import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
Expand All @@ -18,7 +20,32 @@
*/
public interface Dependency<B extends ServiceBuilder<?>, V> extends Consumer<B>, Supplier<V> {

@Override
default Dependency<B, V> andThen(Consumer<? super B> after) {
Objects.requireNonNull(after);
return new Dependency<>() {
pferraro marked this conversation as resolved.
Show resolved Hide resolved
@Override
public void accept(B builder) {
Dependency.this.accept(builder);
after.accept(builder);
}

@Override
public V get() {
return Dependency.this.get();
}
};
}

/**
* Returns a dependency whose value is the result of applying the specified mapping function.
* @param <R> the mapped value type
* @param mapper a mapping function
* @return a dependency whose value is the result of applying the specified mapping function.
* @throws NullPointerException if {@code mapper} was null
*/
default <R> Dependency<B, R> map(Function<V, R> mapper) {
Objects.requireNonNull(mapper);
return new Dependency<>() {
@Override
public void accept(B builder) {
Expand All @@ -32,6 +59,32 @@ public R get() {
};
}

/**
* Returns a dependency that combines this dependency with the specified dependency, whose value is determined by the specified mapping function.
* @param <T> the other dependency type
* @param <R> the mapped value type
* @param dependency another dependency
* @param mapper a mapping function
* @return a dependency that combines this dependency with the specified dependency, whose value is determined by the specified mapping function.
* @throws NullPointerException if {@code dependency} or {@code mapper} were null.
*/
default <T, R> Dependency<B, R> combine(Dependency<B, T> dependency, BiFunction<V, T, R> mapper) {
Objects.requireNonNull(dependency);
Objects.requireNonNull(mapper);
return new Dependency<>() {
@Override
public void accept(B builder) {
Dependency.this.accept(builder);
dependency.accept(builder);
}

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

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

private final V value;
Expand All @@ -51,6 +104,25 @@ public void accept(B builder) {
}
}

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

private final Supplier<V> supplier;

protected SuppliedDependency(Supplier<V> supplier) {
this.supplier = supplier;
}

@Override
public V get() {
return this.supplier.get();
}

@Override
public void accept(B builder) {
// Nothing to register
}
}

abstract class AbstractDependency<B extends ServiceBuilder<?>, V> implements Dependency<B, V>, Function<B, Supplier<V>> {

private volatile Supplier<V> supplier = Functions.constantSupplier(null);
Expand Down
58 changes: 58 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,7 +4,11 @@
*/
package org.wildfly.service;

import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceName;
Expand All @@ -15,8 +19,26 @@
*/
public interface ServiceDependency<V> extends Dependency<ServiceBuilder<?>, V> {

@Override
default ServiceDependency<V> andThen(Consumer<? super ServiceBuilder<?>> after) {
Objects.requireNonNull(after);
return new ServiceDependency<>() {
pferraro marked this conversation as resolved.
Show resolved Hide resolved
@Override
public void accept(ServiceBuilder<?> builder) {
ServiceDependency.this.accept(builder);
after.accept(builder);
}

@Override
public V get() {
return ServiceDependency.this.get();
}
};
}

@Override
default <R> ServiceDependency<R> map(Function<V, R> mapper) {
Objects.requireNonNull(mapper);
return new ServiceDependency<>() {
@Override
public void accept(ServiceBuilder<?> builder) {
Expand All @@ -30,6 +52,24 @@ public R get() {
};
}

@Override
default <T, R> ServiceDependency<R> combine(Dependency<ServiceBuilder<?>, T> dependency, BiFunction<V, T, R> mapper) {
Objects.requireNonNull(dependency);
Objects.requireNonNull(mapper);
return new ServiceDependency<>() {
@Override
public void accept(ServiceBuilder<?> builder) {
ServiceDependency.this.accept(builder);
dependency.accept(builder);
}

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

/**
* Returns a pseudo-dependency whose {@link #get()} returns the specified value.
* @param <V> the value type
Expand All @@ -41,6 +81,18 @@ static <V> ServiceDependency<V> of(V value) {
return (value != null) ? new SimpleServiceDependency<>(value) : (ServiceDependency<V>) SimpleServiceDependency.NULL;
}

/**
* Returns a pseudo-dependency whose {@link #get()} returns the value from the specified supplier.
* @param <V> the value type
* @param supplier a service value supplier
* @return a service dependency
* @throws NullPointerException if {@code supplier} was null
*/
static <V> ServiceDependency<V> from(Supplier<V> supplier) {
Objects.requireNonNull(supplier);
return new SuppliedServiceDependency<>(supplier);
}

/**
* Returns a dependency on the service with the specified name.
* @param <V> the dependency type
Expand All @@ -59,6 +111,12 @@ class SimpleServiceDependency<V> extends SimpleDependency<ServiceBuilder<?>, V>
}
}

class SuppliedServiceDependency<V> extends SuppliedDependency<ServiceBuilder<?>, V> implements ServiceDependency<V> {
SuppliedServiceDependency(Supplier<V> supplier) {
super(supplier);
}
}

class DefaultServiceDependency<V> extends DefaultDependency<ServiceBuilder<?>, V> implements ServiceDependency<V> {

DefaultServiceDependency(ServiceName name) {
Expand Down
20 changes: 8 additions & 12 deletions subsystem/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright The WildFly Authors
~ SPDX-License-Identifier: Apache-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -32,25 +32,21 @@

<dependencies>
<!-- Internal dependencies -->
<dependency>
<groupId>org.wildfly.core</groupId>
<artifactId>wildfly-controller</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.core</groupId>
<artifactId>wildfly-server</artifactId>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

Expand All @@ -25,8 +28,26 @@
*/
public interface ServiceDependency<V> extends Dependency<RequirementServiceBuilder<?>, V> {

@Override
default ServiceDependency<V> andThen(Consumer<? super RequirementServiceBuilder<?>> after) {
Objects.requireNonNull(after);
return new ServiceDependency<>() {
pferraro marked this conversation as resolved.
Show resolved Hide resolved
@Override
public void accept(RequirementServiceBuilder<?> builder) {
ServiceDependency.this.accept(builder);
after.accept(builder);
}

@Override
public V get() {
return ServiceDependency.this.get();
}
};
}

@Override
default <R> ServiceDependency<R> map(Function<V, R> mapper) {
Objects.requireNonNull(mapper);
return new ServiceDependency<>() {
@Override
public void accept(RequirementServiceBuilder<?> builder) {
Expand All @@ -40,6 +61,24 @@ public R get() {
};
}

@Override
default <T, R> ServiceDependency<R> combine(Dependency<RequirementServiceBuilder<?>, T> dependency, BiFunction<V, T, R> mapper) {
Objects.requireNonNull(dependency);
Objects.requireNonNull(mapper);
return new ServiceDependency<>() {
@Override
public void accept(RequirementServiceBuilder<?> builder) {
ServiceDependency.this.accept(builder);
dependency.accept(builder);
}

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

/**
* Returns a pseudo-dependency whose {@link #get()} returns the specified value.
* @param <T> the value type
Expand All @@ -51,6 +90,18 @@ static <T> ServiceDependency<T> of(T value) {
return (value != null) ? new SimpleServiceDependency<>(value) : (ServiceDependency<T>) SimpleServiceDependency.NULL;
}

/**
* Returns a pseudo-dependency whose {@link #get()} returns the value from the specified supplier.
* @param <V> the value type
* @param factory a service value supplier
* @return a service dependency
* @throws NullPointerException if {@code supplier} was null
*/
static <V> ServiceDependency<V> from(Supplier<V> supplier) {
Objects.requireNonNull(supplier);
return new SuppliedServiceDependency<>(supplier);
}

/**
* Returns a dependency on the service with the specified name.
* @param <T> the dependency type
Expand All @@ -65,8 +116,10 @@ static <T> ServiceDependency<T> on(ServiceName name) {
* Wraps a {@link org.wildfly.service.ServiceDependency} as a {@link ServiceDependency}.
* @param <T> the dependency type
* @return a service dependency
* @throws NullPointerException if {@code dependency} was null
*/
static <T> ServiceDependency<T> from(org.wildfly.service.ServiceDependency<T> dependency) {
Objects.requireNonNull(dependency);
return new ServiceDependency<>() {
@Override
public void accept(RequirementServiceBuilder<?> builder) {
Expand Down Expand Up @@ -222,6 +275,12 @@ class SimpleServiceDependency<V> extends SimpleDependency<RequirementServiceBuil
}
}

class SuppliedServiceDependency<V> extends SuppliedDependency<RequirementServiceBuilder<?>, V> implements ServiceDependency<V> {
SuppliedServiceDependency(Supplier<V> supplier) {
super(supplier);
}
}

class DefaultServiceDependency<V> extends Dependency.DefaultDependency<RequirementServiceBuilder<?>, V> implements ServiceDependency<V> {

DefaultServiceDependency(ServiceName name) {
Expand Down
Loading