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.*;
import com.envimate.mapmate.deserialization.Deserializer;
import com.envimate.mapmate.validation.ExceptionMapping;
import java.util.*;
public final class DeserializerBuilder {
private Unmarshaller unmarshaller;
private final List scannablePackages;
private final Definitions definitions;
private Map, ExceptionMapping> exceptionMapping;
public DeserializerBuilder() {
this.scannablePackages = new ArrayList<>(0);
this.definitions = Definitions.empty();
this.exceptionMapping = new HashMap<>(0);
}
public DeserializerBuilder withUnmarshaller(final Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
return this;
}
public ScannablePackageBuilder thatScansThePackage(final String packageName) {
if (packageName == null) {
throw new NullPointerException("package name cannot be null");
}
if (packageName.isEmpty()) {
throw new IllegalArgumentException("package name cannot be empty");
}
return new ScannablePackageBuilder(this, PackageName.fromString(packageName));
}
DeserializerBuilder thatScansThePackage(final ScannablePackageDirective scannablePackage) {
this.scannablePackages.add(scannablePackage);
return this;
}
public CustomPrimitiveBuilder withCustomPrimitive(final Class> type) {
Objects.requireNonNull(type, "type cannot be null");
return new CustomPrimitiveBuilder(this, type);
}
DeserializerBuilder withCustomPrimitive(final Definition type) {
this.definitions.add(type);
return this;
}
public DataTransferObjectBuilder withDataTransferObject(final Class> type) {
Objects.requireNonNull(type, "type cannot be null");
return new DataTransferObjectBuilder(this, type);
}
DeserializerBuilder withDataTransferObject(final Definition dataTransferObject) {
this.definitions.add(dataTransferObject);
return this;
}
public DeserializerBuilder mappingValidationException(final Class extends Throwable> exceptionType,
final ExceptionMapping mapping) {
this.exceptionMapping.put(exceptionType, mapping);
return this;
}
public Deserializer build() {
if (this.unmarshaller == null) {
throw NoUnmarshallerConfiguredException.noUnmarshallerConfiguredException();
}
this.scannablePackages.forEach(scannablePackage -> {
final Definitions scannedDefinitions = Definitions.byScanningPackage(scannablePackage);
this.definitions.addAll(scannedDefinitions);
});
final DefinitionReference reference = this.definitions.firstContainingOutgoingReferences();
if (reference != null) {
throw UnknownReferenceException.fromDefinition(reference);
}
return new Deserializer(this.unmarshaller, this.definitions, this.exceptionMapping);
}
}