io.openmanufacturing.sds.aspectmodel.java.ConstraintAnnotationBuilder Maven / Gradle / Ivy
/*
* Copyright (c) 2021 Robert Bosch Manufacturing Solutions GmbH
*
* See the AUTHORS file(s) distributed with this work for additional
* information regarding authorship.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/
package io.openmanufacturing.sds.aspectmodel.java;
import java.math.BigInteger;
import java.util.Optional;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import io.openmanufacturing.sds.aspectmodel.java.rangeconstraint.AnnotationExpression;
import io.openmanufacturing.sds.aspectmodel.java.rangeconstraint.AnnotationFactory;
import io.openmanufacturing.sds.aspectmodel.java.rangeconstraint.AnnotationTypeMapping;
import io.openmanufacturing.sds.metamodel.Constraint;
import io.openmanufacturing.sds.metamodel.FixedPointConstraint;
import io.openmanufacturing.sds.metamodel.LengthConstraint;
import io.openmanufacturing.sds.metamodel.RangeConstraint;
import io.openmanufacturing.sds.metamodel.RegularExpressionConstraint;
import io.openmanufacturing.sds.metamodel.impl.BoundDefinition;
/**
* Provides functionality to create javax.validation.constraints that are displayed in the generated java class
*/
public class ConstraintAnnotationBuilder {
private static final String ANNOTATION_MARKING = "@";
private static final String LEFT_BRACKET = "(";
private static final String RIGHT_BRACKET = ")";
private final StringBuilder constraintAnnotation = new StringBuilder( 110 );
private Constraint constraintClass;
private ImportTracker importTracker;
public ConstraintAnnotationBuilder setConstraintClass( final Constraint constraintClass ) {
this.constraintClass = constraintClass;
return this;
}
public ConstraintAnnotationBuilder setImportTracker( final ImportTracker importTracker ) {
this.importTracker = importTracker;
return this;
}
public String build() {
return createConstraintAnnotation();
}
private String createConstraintAnnotation() {
createRegularExpressionConstraint();
createRangeConstraint();
createLengthConstraint();
createFixedPointConstraint();
return constraintAnnotation.toString();
}
private void createRegularExpressionConstraint() {
if ( !(constraintClass instanceof RegularExpressionConstraint) ) {
return;
}
final RegularExpressionConstraint regularExpressionConstraint = (RegularExpressionConstraint) constraintClass;
appendStringBuilder( Pattern.class, "regexp = \"" + regularExpressionConstraint.getValue() + "\"" );
}
private void createRangeConstraint() {
if ( !(constraintClass instanceof RangeConstraint) ) {
return;
}
final RangeConstraint rangeConstraint = (RangeConstraint) constraintClass;
final Optional