org.apache.beam.sdk.transforms.SimpleFunction Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.beam.sdk.transforms;
import org.apache.beam.sdk.transforms.display.DisplayData;
import org.apache.beam.sdk.transforms.display.HasDisplayData;
import org.apache.beam.sdk.values.TypeDescriptor;
/**
* A {@link SerializableFunction} which is not a functional interface.
* Concrete subclasses allow us to infer type information, which in turn aids
* {@link org.apache.beam.sdk.coders.Coder Coder} inference.
*/
public abstract class SimpleFunction
implements SerializableFunction, HasDisplayData {
public static
SimpleFunction fromSerializableFunctionWithOutputType(
SerializableFunction fn, TypeDescriptor outputType) {
return new SimpleFunctionWithOutputType<>(fn, outputType);
}
/**
* Returns a {@link TypeDescriptor} capturing what is known statically
* about the input type of this {@link SimpleFunction} instance's most-derived
* class.
*
* See {@link #getOutputTypeDescriptor} for more discussion.
*/
public TypeDescriptor getInputTypeDescriptor() {
return new TypeDescriptor(this) {};
}
/**
* Returns a {@link TypeDescriptor} capturing what is known statically
* about the output type of this {@link SimpleFunction} instance's
* most-derived class.
*
* In the normal case of a concrete {@link SimpleFunction} subclass with
* no generic type parameters of its own (including anonymous inner
* classes), this will be a complete non-generic type, which is good
* for choosing a default output {@code Coder} for the output
* {@code PCollection}.
*/
public TypeDescriptor getOutputTypeDescriptor() {
return new TypeDescriptor(this) {};
}
/**
* {@inheritDoc}
*
* By default, does not register any display data. Implementors may override this method
* to provide their own display data.
*/
@Override
public void populateDisplayData(DisplayData.Builder builder) {}
/**
* A {@link SimpleFunction} built from a {@link SerializableFunction}, having
* a known output type that is explicitly set.
*/
private static class SimpleFunctionWithOutputType
extends SimpleFunction {
private final SerializableFunction fn;
private final TypeDescriptor outputType;
public SimpleFunctionWithOutputType(
SerializableFunction fn,
TypeDescriptor outputType) {
this.fn = fn;
this.outputType = outputType;
}
@Override
public OutputT apply(InputT input) {
return fn.apply(input);
}
@Override
public TypeDescriptor getOutputTypeDescriptor() {
return outputType;
}
}
}