com.speedment.common.codegen.internal.BridgeTransform Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of generator-deploy Show documentation
Show all versions of generator-deploy Show documentation
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