All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.pmd.properties.NumericConstraints Maven / Gradle / Ivy

Go to download

PMD is an extensible multilanguage static code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth. It's mainly concerned with Java and Apex, but supports 16 other languages. It comes with 400+ built-in rules. It can be extended with custom rules. It uses JavaCC and Antlr to parse source files into abstract syntax trees (AST) and runs rules against them to find violations. Rules can be written in Java or using a XPath query. Currently, PMD supports Java, JavaScript, Salesforce.com Apex and Visualforce, Kotlin, Swift, Modelica, PLSQL, Apache Velocity, JSP, WSDL, Maven POM, HTML, XML and XSL. Scala is supported, but there are currently no Scala rules available. Additionally, it includes CPD, the copy-paste-detector. CPD finds duplicated code in Coco, C/C++, C#, Dart, Fortran, Gherkin, Go, Groovy, HTML, Java, JavaScript, JSP, Julia, Kotlin, Lua, Matlab, Modelica, Objective-C, Perl, PHP, PLSQL, Python, Ruby, Salesforce.com Apex and Visualforce, Scala, Swift, T-SQL, Typescript, Apache Velocity, WSDL, XML and XSL.

There is a newer version: 7.5.0-metrics
Show newest version
/*
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 */

package net.sourceforge.pmd.properties;


import static net.sourceforge.pmd.util.CollectionUtil.mapOf;

import net.sourceforge.pmd.util.internal.xml.SchemaConstants;

/**
 * Common constraints for properties dealing with numbers.
 *
 * @author Clément Fournier
 * @see PropertyConstraint
 * @since 6.10.0
 */
public final class NumericConstraints {

    private NumericConstraints() {

    }

    // Methods are named to mix well with the "require" syntax.


    /**
     * Requires the number to be inside a range.
     *
     * @param  Type of number
     *
     * @return A range constraint
     */
    public static > PropertyConstraint inRange(final N minInclusive, final N maxInclusive) {
        return PropertyConstraint.fromPredicate(
            t -> minInclusive.compareTo(t) <= 0 && maxInclusive.compareTo(t) >= 0,
            "Should be between " + minInclusive + " and " + maxInclusive,
                mapOf(SchemaConstants.PROPERTY_MIN.xmlName(), String.valueOf(minInclusive),
                        SchemaConstants.PROPERTY_MAX.xmlName(), String.valueOf(maxInclusive))
        );

    }

    /**
     * Requires the number to be greater than a lower bound.
     *
     * @param  Type of number
     *
     * @return A range constraint
     */
    public static > PropertyConstraint above(final N minInclusive) {
        return PropertyConstraint.fromPredicate(
            t -> minInclusive.compareTo(t) <= 0,
            "Should be greater or equal to " + minInclusive,
                mapOf(SchemaConstants.PROPERTY_MIN.xmlName(), String.valueOf(minInclusive))
        );
    }

    /**
     * Requires the number to be lower than an upper bound.
     *
     * @param  Type of number
     *
     * @return A range constraint
     */
    public static > PropertyConstraint below(final N maxInclusive) {
        return PropertyConstraint.fromPredicate(
            t -> maxInclusive.compareTo(t) >= 0,
            "Should be smaller or equal to " + maxInclusive,
                mapOf(SchemaConstants.PROPERTY_MAX.xmlName(), String.valueOf(maxInclusive))
        );
    }


    /**
     * Requires the number to be strictly positive.
     * The int values of the number is used for comparison
     * so there may be some unexpected behaviour with decimal
     * numbers.
     *
     * 

Note: This constraint cannot be expressed in a XML ruleset.

* * @param Type of number * * @return A positivity constraint */ public static PropertyConstraint positive() { return PropertyConstraint.fromPredicate( t -> t.intValue() > 0, "Should be positive" ); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy