I have the following class:
public class SomeAssebler {
private EnrichedRequest assembleBasicEnrichedRequest(BasicRequest request) {
EnrichedRequest enrichedRequest = new EnrichedRequest();
if (request != null) {
enrichedRequest.setId(request.getId());
enrichedRequest.setDate(request.getDate());
enrichedRequest.setParam(request.getParam());
}
return enrichedRequest;
}
private static EnrichedRequest assembleGeoDataEnrichedRequest(EnrichedRequest request, GeoData geoData) {
EnrichedRequest geoEnrichedRequest = Optional.ofNullable(request).orElse(new EnrichedRequest());
if (geoData != null) {
geoEnrichedRequest.setCity(geoData.getCity());
geoEnrichedRequest.setCountryCode(geoData.getCountryCode());
}
return geoEnrichedRequest;
}
private static EnrichedRequest assembleUserAgentEnrichedRequest(EnrichedRequest request, UserAgentData userAgentData) {
EnrichedRequest userAgentEnrichedRequest = Optional.ofNullable(request).orElse(new EnrichedRequest());
if (userAgentData != null) {
userAgentEnrichedRequest.setBrowser(userAgentData.getBrowserCode());
userAgentEnrichedRequest.setOperatingSystem(userAgentData.getOsCode());
userAgentEnrichedRequest.setManufacturer(userAgentData.getBrand());
}
}
public static EnrichedRequest assembleEnrichedRequest(BasicRequest request, GeoData geoData, UserAgentData userAgentData) {
EnrichedRequest enrichedRequest = assembleBasicEnrichedRequest(request);
enrichedRequest = assembleGeoDataEnrichedRequest(enrichedRequest, geoData);
enrichedRequest = assembleUserAgentEnrichedRequest(enrichedRequest, userAgentData);
return enrichedRequest;
}
}
It is then called in different transformers: In the GeoDataTransformer
new KeyValue<>(key, assembleEnrichedRequest(basicRequest, geoData, null))
In the UserAgentTransformer:
new KeyValue<>(key, assembleEnrichedRequest(enrichedRequest, null, userAgent))
If more "enrichers" will be added in the future this can get very ugly. Anyone has an idea how I can design this better? I though of the Builder design pattern but I'm not sure.
Aucun commentaire:
Enregistrer un commentaire