-
Notifications
You must be signed in to change notification settings - Fork 9
7. Overriding Converters
Converter Interface - This interface is where the actual mappings happen. The target objects are created in the map function and all the mappings from source to target are performed in the enhance function. The OpenRtb 2.4 and 2.3 mappings are provided out of the box by the library using overriding converter feature of the library.
public interface Converter<U, V> {
V map(U source, Config config, Provider<Conversion, Converter> converterProvider) throws
OpenRtbConverterException;
void enhance(U source, V target, Config config, Provider<Conversion, Converter>
converterProvider) throws OpenRtbConverterException;
}
Library allows you to create your own converter implementations for a particular conversion from class A to class B in case an overriding converter is provided in the convert or enhance functions then that overrides the default converter provided by the library for that conversion. The scope is limited to that particular request.
Example of an Overriding Converter-
Suppose Existing implementation is -
public class LinkToLinkAssetConverter implements Converter<Link, LinkAsset> {
@Override
public LinkAsset map(Link source, Config config, Provider converterProvider) throws OpenRtbConverterException {
if (source == null) {
return null;
}
LinkAsset linkAsset = new LinkAsset();
enhance(source, linkAsset, config, converterProvider);
return linkAsset;
}
@Override
public void enhance(Link source, LinkAsset target, Config config, Provider converterProvider)throws OpenRtbConverterException {
if (source == null || target == null) {
return;
}
target.setUrl(source.getUrl());
target.setUrlfb(source.getFallback());
target.setTrkr(source.getClicktrackers());
target.setExt(source.getExt());
}
}
suppose in our new implementation url field of LinkAsset is set from urlNew field of Link, so new implementation becomes -
public class NewLinkToLinkAssetConverter implements Converter<Link, LinkAsset> {
@Override
public LinkAsset map(Link source, Config config, Provider converterProvider) throws OpenRtbConverterException {
if (source == null) {
return null;
}
LinkAsset linkAsset = new LinkAsset();
enhance(source, linkAsset, config, converterProvider);
return linkAsset;
}
@Override
public void enhance(Link source, LinkAsset target, Config config, Provider converterProvider)throws OpenRtbConverterException {
if (source == null || target == null) {
return;
}
target.setUrl(source.getUrlNew());
target.setUrlfb(source.getFallback());
target.setTrkr(source.getClicktrackers());
target.setExt(source.getExt());
}
}
To make this converter effective we need to supply it in an overriding map to the conversion or enhance function.
Map<Conversion, Converter> overridingConverters = new HashMap<>();
overridingConverters.put(new Conversion<>(Link.class, LinkAsset.class), new NewLinkToLinkAssetConverter());
OpenRtbConverter openRtbConverter = …;
openRtbConverter.convert(source, Link.class, LinkAsset.class, overridingConverters);