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

org.incendo.cloud.services.ServiceRepository Maven / Gradle / Ivy

The newest version!
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.services;

import io.leangen.geantyref.TypeToken;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.services.annotation.Order;
import org.incendo.cloud.services.type.Service;

/**
 * Repository that contains implementations for a given service type
 *
 * @param   Service context
 * @param  Service response type
 */
public final class ServiceRepository {

    private final Object lock = new Object();
    private final TypeToken> serviceType;
    private final List>> implementations;

    private int registrationOrder = 0;

    /**
     * Create a new service repository for a given service type
     *
     * @param serviceType Service type
     */
    ServiceRepository(final @NonNull TypeToken> serviceType) {
        this.serviceType = serviceType;
        this.implementations = new LinkedList<>();
    }

    /**
     * Register a new implementation for the service
     *
     * @param service Implementation
     * @param filters Filters that will be used to determine whether or not the service gets used
     * @param      Type of the implementation
     */
    > void registerImplementation(
            final @NonNull T service,
            final @NonNull Collection> filters
    ) {
        synchronized (this.lock) {
            this.implementations.add(new ServiceWrapper<>(service, filters));
        }
    }

    /**
     * Returns a queue containing all implementations.
     *
     * @return queue containing all implementations
     */
    @SuppressWarnings("NonApiType")
    @NonNull LinkedList>> queue() {
        synchronized (this.lock) {
            return new LinkedList<>(this.implementations);
        }
    }


    /**
     * Used to store {@link Service} implementations together with their state
     *
     * @param  Service type
     */
    final class ServiceWrapper>
            implements Comparable> {

        private final boolean defaultImplementation;
        private final T implementation;
        private final Collection> filters;

        private final int registrationOrder = ServiceRepository.this.registrationOrder++;
        private final ExecutionOrder executionOrder;

        private ServiceWrapper(
                final @NonNull T implementation,
                final @NonNull Collection> filters
        ) {
            this.defaultImplementation = ServiceRepository.this.implementations.isEmpty();
            this.implementation = implementation;
            this.filters = filters;
            ExecutionOrder executionOrder = implementation.order();
            if (executionOrder == null) {
                final Order order = implementation.getClass().getAnnotation(Order.class);
                if (order != null) {
                    executionOrder = order.value();
                } else {
                    executionOrder = ExecutionOrder.SOON;
                }
            }
            this.executionOrder = executionOrder;
        }

        @NonNull T implementation() {
            return this.implementation;
        }

        @NonNull Collection> filters() {
            return Collections.unmodifiableCollection(this.filters);
        }

        boolean isDefaultImplementation() {
            return this.defaultImplementation;
        }

        @Override
        public String toString() {
            return String.format(
                    "ServiceWrapper{type=%s,implementation=%s}",
                    ServiceRepository.this.serviceType.getType().getTypeName(),
                    TypeToken.get(this.implementation.getClass()).getType().getTypeName()
            );
        }

        @SuppressWarnings("EnumOrdinal")
        @Override
        public int compareTo(final @NonNull ServiceWrapper other) {
            return Comparator.>comparingInt(
                            wrapper -> wrapper.isDefaultImplementation()
                                    ? Integer.MIN_VALUE
                                    : Integer.MAX_VALUE).thenComparingInt(wrapper -> wrapper.executionOrder.ordinal())
                    .thenComparingInt(wrapper -> wrapper.registrationOrder).compare(this, other);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy