All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wildfly.service.ServiceDependency Maven / Gradle / Ivy

There is a newer version: 27.0.0.Final
Show newest version
/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */
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;

/**
 * Encapsulates a dependency on a service value.
 * @author Paul Ferraro
 */
public interface ServiceDependency extends Dependency, V> {

    @Override
    default ServiceDependency andThen(Consumer> after) {
        Objects.requireNonNull(after);
        return new ServiceDependency<>() {
            @Override
            public void accept(ServiceBuilder builder) {
                ServiceDependency.this.accept(builder);
                after.accept(builder);
            }

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

    @Override
    default  ServiceDependency map(Function mapper) {
        Objects.requireNonNull(mapper);
        return new ServiceDependency<>() {
            @Override
            public void accept(ServiceBuilder builder) {
                ServiceDependency.this.accept(builder);
            }

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

    @Override
    default  ServiceDependency combine(Dependency, T> dependency, BiFunction 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 an empty pseudo-dependency whose {@link #get()} returns null.
     * @param  the value type
     * @return an empty service dependency
     */
    @SuppressWarnings("unchecked")
    static  ServiceDependency empty() {
        return (ServiceDependency) SimpleServiceDependency.EMPTY;
    }

    /**
     * Returns a pseudo-dependency whose {@link #get()} returns the specified value.
     * @param  the value type
     * @param value a service value
     * @return a pseudo-dependency whose {@link #get()} returns the specified value.
     */
    static  ServiceDependency of(V value) {
        return (value != null) ? new SimpleServiceDependency<>(value) : empty();
    }

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

    /**
     * Returns a dependency on the service with the specified name.
     * @param  the dependency type
     * @param name a service name
     * @return a service dependency
     */
    static  ServiceDependency on(ServiceName name) {
        return (name != null) ? new DefaultServiceDependency<>(name) : empty();
    }

    class SimpleServiceDependency extends SimpleDependency, V> implements ServiceDependency {
        static final ServiceDependency EMPTY = new SimpleServiceDependency<>(null);

        SimpleServiceDependency(V value) {
            super(value);
        }
    }

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

    class DefaultServiceDependency extends DefaultDependency, V> implements ServiceDependency {

        DefaultServiceDependency(ServiceName name) {
            super(name);
        }
    }
}