com.itshidu.ffmpeg.modelmapper.Mapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ffmpeg-java Show documentation
Show all versions of ffmpeg-java Show documentation
Java Interface for FFmpeg Command-line
The newest version!
package com.itshidu.ffmpeg.modelmapper;
import static com.itshidu.ffmpeg.modelmapper.NotDefaultCondition.notDefault;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeMap;
import org.modelmapper.config.Configuration;
import org.modelmapper.convention.NameTokenizers;
import com.itshidu.ffmpeg.builder.AbstractFFmpegStreamBuilder;
import com.itshidu.ffmpeg.builder.FFmpegOutputBuilder;
import com.itshidu.ffmpeg.options.AudioEncodingOptions;
import com.itshidu.ffmpeg.options.EncodingOptions;
import com.itshidu.ffmpeg.options.MainEncodingOptions;
import com.itshidu.ffmpeg.options.VideoEncodingOptions;
/**
* Copies values from one type of object to another
*
* @author bramp
*/
public class Mapper {
private Mapper() {
throw new InstantiationError("Must not instantiate this class");
}
private static final ModelMapper mapper = newModelMapper();
private static TypeMap createTypeMap(
ModelMapper mapper, Class sourceType, Class destinationType, Configuration config) {
return mapper
.createTypeMap(sourceType, destinationType, config)
// We setPropertyCondition because ModelMapper seems to ignore this in
// the config
.setPropertyCondition(config.getPropertyCondition());
}
private static ModelMapper newModelMapper() {
final ModelMapper mapper = new ModelMapper();
Configuration config =
mapper
.getConfiguration()
.copy()
.setFieldMatchingEnabled(true)
.setPropertyCondition(notDefault)
.setSourceNameTokenizer(NameTokenizers.UNDERSCORE);
createTypeMap(mapper, MainEncodingOptions.class, FFmpegOutputBuilder.class, config);
createTypeMap(mapper, AudioWrapper.class, FFmpegOutputBuilder.class, config);
createTypeMap(mapper, VideoWrapper.class, FFmpegOutputBuilder.class, config);
return mapper;
}
/** Simple wrapper object, to inject the word "audio" in the property name */
static class AudioWrapper {
public final AudioEncodingOptions audio;
AudioWrapper(AudioEncodingOptions audio) {
this.audio = audio;
}
}
/** Simple wrapper object, to inject the word "video" in the property name */
static class VideoWrapper {
public final VideoEncodingOptions video;
VideoWrapper(VideoEncodingOptions video) {
this.video = video;
}
}
public static > void map(
MainEncodingOptions opts, AbstractFFmpegStreamBuilder dest) {
mapper.map(opts, dest);
}
public static > void map(
AudioEncodingOptions opts, AbstractFFmpegStreamBuilder dest) {
mapper.map(new AudioWrapper(opts), dest);
}
public static > void map(
VideoEncodingOptions opts, AbstractFFmpegStreamBuilder dest) {
mapper.map(new VideoWrapper(opts), dest);
}
public static > void map(
EncodingOptions opts, AbstractFFmpegStreamBuilder dest) {
map(opts.getMain(), dest);
if (opts.getAudio().enabled) {
map(opts.getAudio(), dest);
}
if (opts.getVideo().enabled) {
map(opts.getVideo(), dest);
}
}
}