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

com.github.raduciumag.shapeshift.model.transformation.TransformationHelper Maven / Gradle / Ivy

There is a newer version: 0.5.2
Show newest version
package com.github.raduciumag.shapeshift.model.transformation;

import com.github.raduciumag.shapeshift.model.server.ServerRequest;
import com.github.raduciumag.shapeshift.model.server.ServerResponse;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

public abstract class TransformationHelper,
        RQ extends ServerRequest, RS extends ServerResponse> {
    private static final Logger LOG = LoggerFactory.getLogger(TransformationHelper.class);

    protected final Map transformations = new HashMap<>();
    protected final Map transformationExecutors = new HashMap<>();

    public List listTransformations() {
        return transformations.values().stream().collect(Collectors.toList());
    }

    /**
     * Add a new transformation.
     *
     * @param transformation The transformation.
     * @return The transformation add status.
     */
    public T saveTransformation(final T transformation) {
        Preconditions.checkArgument(transformation != null, "The transformation is required");

        if (StringUtils.isEmpty(transformation.getId())) {
            transformation.setId(UUID.randomUUID().toString());
        }

        final boolean isValid = isValid(transformation);
        if (!isValid) {
            LOG.error("Will not save invalid transformation {}", transformation);
            return null;
        }

        transformations.put(transformation.getId(), transformation);
        transformationExecutors.put(transformation.getId(), buildExecutor(transformation));

        if (LOG.isDebugEnabled()) {
            LOG.debug("Added transformation: {}", transformation);
        }

        return transformation;
    }

    /**
     * Identify the transformation that matches the request.
     *
     * @param request The request to use.
     * @return The transformation that matches this request.
     */
    public abstract Optional identifyTransformation(final RQ request);

    public abstract RS applyTransformation(final RQ request, final T transformation);

    protected abstract boolean isValid(final T transformation);

    protected abstract TX buildExecutor(final T transformation);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy