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

com.speedment.common.codegen.internal.BridgeTransform Maven / Gradle / Ivy

Go to download

A Speedment bundle that shades all dependencies into one jar. This is useful when deploying an application on a server.

The newest version!
/*
 *
 * Copyright (c) 2006-2019, Speedment, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); You may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.speedment.common.codegen.internal;

import com.speedment.common.codegen.Generator;
import com.speedment.common.codegen.Meta;
import com.speedment.common.codegen.Transform;
import com.speedment.common.codegen.TransformFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import static java.util.Objects.requireNonNull;

/**
 * A transform that uses a series of transforms to complete the transformation.
 * For an example, if you have two transforms A → B and A → C you can define
 * a BridgeTransform for A → C.
 * 
 * @author Emil Forslund
 * @param  the from type
 * @param  the to type
 */
public class BridgeTransform implements Transform {
    
    private final List> steps;
    private final Class from;
    private final Class to;
    private final TransformFactory factory;
    private Class end;
    
    /**
     * Constructs a new transform from one model class to another. The bridge
     * requires a factory to create the intermediate steps.
     * 
     * @param from the type to transform from
     * @param to the type to transform to
     * @param factory a factory with all the required steps installed
     */
    private BridgeTransform(Class from, Class to, TransformFactory factory) {
        this.from    = requireNonNull(from);
        this.to      = requireNonNull(to);
        this.factory = requireNonNull(factory);
        this.steps   = new ArrayList<>();
        this.end     = requireNonNull(from);
    }
    
    /**
     * Creates a shallow copy of a bridge.
     * 
     * @param prototype the prototype
     */
    private BridgeTransform(BridgeTransform prototype) {
        steps   = new ArrayList<>(prototype.steps);
        from    = prototype.from;
        to      = prototype.to;
        end     = prototype.end;
        factory = prototype.factory;
    }
    
    /**
     * Transforms the specified model using this transform. A code generator is
     * supplied so that the transform can initiate new generation processes to
     * resolve dependencies.
     * 
     * @param gen   the current code generator 
     * @param model the model to transform
     * @return      the transformation result or empty if any of the steps 
     *              returned empty
     */
    @Override
    public Optional transform(Generator gen, A model) {
        requireNonNull(gen);
        requireNonNull(model);
        
        Object o = model;
        
        for (final Transform step : steps) {
            if (o == null) {
                return Optional.empty();
            } else {
                @SuppressWarnings("unchecked")
                final Transform step2 = (Transform) step;
                
                o = gen.transform(step2, o, factory)
                    .map(Meta::getResult)
                    .orElse(null);
            }
        }
        
        if (o == null) {
            return Optional.empty();
        } else {
            if (to.isAssignableFrom(o.getClass())) {
                @SuppressWarnings("unchecked")
                final B result = (B) o;
                return Optional.of(result);
            } else {
                throw new IllegalStateException(
                    "The bridge between '" + 
                    from.getSimpleName() + 
                    "' to '" + 
                    to.getSimpleName() + 
                    "' is not complete."
                );
            }
        }
    }
    
    /**
     * Creates a bridge from one model type to another. A factory is supplied so
     * that intermediate steps can be resolved.
     * 
     * @param      the initial type of a model to transform
     * @param      the final type of a model after transformation
     * @param      the type of a transform between A and B
     * @param factory a factory with all intermediate steps installed
     * @param from    the initial class of a model to transform
     * @param to      the final class of a model after transformation
     * @return        a Stream of all unique paths between A and B
     */
    public static > Stream create(TransformFactory factory, Class from, Class to) {
        return create(factory, new BridgeTransform<>(from, to, factory));
    }
    
    /**
     * Takes a bridge and completes it if it is not finished. Returns all valid 
     * paths through the graph as a Stream.
     * 
     * @param      the initial type of a model to transform
     * @param      the final type of a model after transformation
     * @param      the type of a transform between A and B
     * @param factory a factory with all intermediate steps installed
     * @param bridge  the incomplete bridge to finish
     * @return        a Stream of all unique paths between A and B
     */
    private static > Stream create(TransformFactory factory, BridgeTransform bridge) {
        requireNonNull(factory);
        requireNonNull(bridge);
        
        if (bridge.end.equals(bridge.to)) {
            @SuppressWarnings("unchecked")
            final T result = (T) bridge;
            return Stream.of(result);
        } else {
            final List> bridges = new ArrayList<>();
            
            factory.allFrom(bridge.end).stream().forEachOrdered(e -> {
                
                final BridgeTransform br = new BridgeTransform<>(bridge);
                
                @SuppressWarnings("unchecked")
                Class a = (Class) bridge.end;
                
                @SuppressWarnings("unchecked")
                Class b = (Class) e.getKey();
                
                @SuppressWarnings("unchecked")
                Transform transform = (Transform) e.getValue();
                
                if (br.addStep(a, b, transform)) {
                    bridges.add(create(factory, br));
                }
            });
            
            return bridges.stream().flatMap(i -> i);
        }
    }

    /**
     * Returns true if this transform is or contains the specified 
     * transformer. This is used internally by the code generator to avoid 
     * circular paths.
     * 
     * @param transformer  the type of the transformer to check
     * @return             true if this transform is or contains the input
     */
    @Override
    public boolean is(Class> transformer) {
        return steps.stream().anyMatch(t -> t.is(requireNonNull(transformer)));
    }
    
    /**
     * Attempts to add a new step to the bridge. If the step is already part of
     * the bridge, it will not be added. Returns true if the step was added.
     * 
     * @param   the initial type of the step
     * @param   the output type of the step
     * @param from  the initial type of the step
     * @param to    the output type of the step
     * @param step  the step to attempt to add
     * @return      true if the step was added
     */
    private  boolean addStep(Class from, Class to, Transform step) {
        requireNonNull(from);
        requireNonNull(to);
        requireNonNull(step);
        
        if (end == null || from.equals(end)) {
            if (steps.contains(step)) {
                return false;
            } else {
                steps.add(step);
                end = to;
                return true;
            }
        } else {
            throw new IllegalArgumentException("Transform " + step + " has a different entry class (" + from + ") than the last class in the current build (" + end + ").");
        }
    }
}