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

global.maplink.geocode.extensions.GeocodeExtensionManager Maven / Gradle / Ivy

There is a newer version: 1.5.14
Show newest version
package global.maplink.geocode.extensions;

import global.maplink.extensions.SdkExtension;
import global.maplink.geocode.schema.GeocodeServiceRequest;
import global.maplink.geocode.schema.suggestions.SuggestionsResult;
import lombok.RequiredArgsConstructor;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toMap;

public class GeocodeExtensionManager {
    private final Map, GeocodeExtension> group;

    public GeocodeExtensionManager(Collection> extensions) {
        group = Collections.unmodifiableMap(
                extensions.stream()
                        .collect(groupingBy(GeocodeExtension::getRequestType))
                        .entrySet()
                        .stream()
                        .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), parse(e.getValue())))
                        .collect(toMap(Map.Entry::getKey, Map.Entry::getValue))
        );
    }

    @SuppressWarnings("unchecked")
    public  GeocodeExtension get(Class requestType) {
        return ofNullable(group.get(requestType))
                .map(v -> (GeocodeExtension) v)
                .orElseGet(() -> GeocodeExtension.empty(requestType));
    }

    private  GeocodeExtension parse(List> list) {
        Set> sorted = new TreeSet<>(list);
        GeocodeExtension lastResult = null;
        for (GeocodeExtension current : sorted) {
            if (lastResult == null) {
                lastResult = current;
                continue;
            }
            lastResult = new RecursiveExtensionIterator<>(current, lastResult);
        }
        return lastResult;
    }

    @SuppressWarnings("unchecked")
    public static GeocodeExtensionManager from(Collection extensions) {
        return new GeocodeExtensionManager(extensions.stream()
                .filter(e -> e instanceof GeocodeExtension)
                .map(e -> (GeocodeExtension) e)
                .collect(Collectors.toSet()));
    }

    @RequiredArgsConstructor
    private static class RecursiveExtensionIterator implements GeocodeExtension {

        private final GeocodeExtension extension;
        private final GeocodeExtension next;

        @Override
        public CompletableFuture doRequest(T request, Function> action) {
            return extension.doRequest(request, (req) -> next.doRequest(req, action));
        }

        @Override
        public CompletableFuture processResponse(SuggestionsResult response) {
            return extension.processResponse(response);
        }

        @Override
        public CompletableFuture processRequest(T response) {
            return extension.processRequest(response);
        }

        @Override
        public String getName() {
            return extension.getName();
        }

        @Override
        public Class getRequestType() {
            return extension.getRequestType();
        }

        @Override
        public int getPriority() {
            return extension.getPriority();
        }

        @Override
        public int compareTo(SdkExtension o) {
            return extension.compareTo(o);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy