com.envimate.mapmate.deserialization.builder.DeserializerBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapmate Show documentation
Show all versions of mapmate Show documentation
MapMate is a modern mapping framework in the scope of mapping data in
Json, XML, or YAML format into DTOs composed and vice versa.
/*
* Copyright (C) 2017 [Richard Hauswald, Nune Isabekyan] (envimate GmbH - https://envimate.com/)
*/
package com.envimate.mapmate.deserialization.builder;
import com.envimate.mapmate.deserialization.*;
import com.envimate.mapmate.validation.ExceptionMapping;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import static com.envimate.mapmate.deserialization.DeserializableCustomPrimitive.deserializableCustomPrimitive;
import static com.envimate.mapmate.deserialization.DeserializableDataTransferObject.deserializableDataTransferObject;
import static com.envimate.mapmate.deserialization.DeserializableDefinitions.*;
import static com.envimate.mapmate.deserialization.Deserializer.theDeserializer;
import static com.envimate.mapmate.deserialization.builder.CustomPrimitiveDeserializationMethodBuilder.aCustomPrimitiveDeserializationMethodBuilder;
import static com.envimate.mapmate.deserialization.builder.DataTransferObjectDeserializationMethodBuilder.aDataTransferObjectDeserializationMethodBuilder;
import static com.envimate.mapmate.deserialization.builder.ScannablePackageBuilder.aScannablePackageBuilder;
import static com.envimate.mapmate.reflections.PackageName.fromString;
import static com.envimate.mapmate.validators.NotNullValidator.validateNotNull;
import static com.envimate.mapmate.validators.RequiredStringValidator.validateNotNullNorEmpty;
public final class DeserializerBuilder {
private Unmarshaller unmarshaller;
private final List definitions;
private final Map, ExceptionMapping> exceptionMappings;
private DeserializerBuilder() {
this.definitions = new LinkedList<>();
this.exceptionMappings = new HashMap<>();
}
public static DeserializerBuilder aDeserializerBuilder() {
return new DeserializerBuilder();
}
public DeserializerBuilder withUnmarshaller(final Unmarshaller unmarshaller) {
validateNotNull(unmarshaller, "unmarshaller");
this.unmarshaller = unmarshaller;
return this;
}
public ScannablePackageBuilder thatScansThePackage(final String packageName) {
validateNotNullNorEmpty(packageName, "packageName");
return aScannablePackageBuilder(packageScanner -> {
final DeserializableDefinitions definitions = packageScanner.scan(fromString(packageName));
this.definitions.add(definitions);
return this;
});
}
public CustomPrimitiveDeserializationMethodBuilder withCustomPrimitive(final Class> type) {
validateNotNull(type, "type");
return aCustomPrimitiveDeserializationMethodBuilder(deserializationCPMethod -> {
final DeserializableCustomPrimitive customPrimitive = deserializableCustomPrimitive(type, deserializationCPMethod);
final DeserializableDefinitions definitions = withASingleCustomPrimitive(customPrimitive);
this.definitions.add(definitions);
return this;
});
}
public DataTransferObjectDeserializationMethodBuilder withDataTransferObject(final Class> type) {
validateNotNull(type, "type");
return aDataTransferObjectDeserializationMethodBuilder(deserializationDTOMethod -> {
final DeserializableDataTransferObject dataTransferObject =
deserializableDataTransferObject(type, deserializationDTOMethod);
final DeserializableDefinitions definitions = withASingleDataTransferObject(dataTransferObject);
this.definitions.add(definitions);
return this;
});
}
public DeserializerBuilder mappingValidationException(final Class extends Throwable> exceptionType,
final ExceptionMapping mapping) {
validateNotNull(exceptionType, "exceptionType");
validateNotNull(mapping, "mapping");
this.exceptionMappings.put(exceptionType, mapping);
return this;
}
public Deserializer build() {
DeserializableDefinitions allDefinitions = empty();
for (final DeserializableDefinitions definitions : this.definitions) {
allDefinitions = merge(allDefinitions, definitions);
}
allDefinitions = merge(allDefinitions, theSpeciallyTreatedCustomPrimitives());
return theDeserializer(this.unmarshaller, allDefinitions, exceptionMappings);
}
}