Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com)
   *
   * This software is dual-licensed under:
   *
   * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
   *   later version;
   * - the Apache Software License (ASL) version 2.0.
   *
  * The text of both licenses is available under the src/resources/ directory of
  * this project (under the names LGPL-3.0.txt and ASL-2.0.txt respectively).
  *
  * Direct link to the sources:
  *
  * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
  * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
  */
 
 package com.github.fge.jsonschema.keyword.validator.helpers;
 
Helper class for keywords validating numeric values

This class' role is to switch between two different validation methods: validateLong(com.github.fge.jsonschema.core.report.ProcessingReport,com.github.fge.msgsimple.bundle.MessageBundle,com.github.fge.jsonschema.processors.data.FullData) if both the keyword value and instance fit exactly into a long (for performance reasons), validateDecimal(com.github.fge.jsonschema.core.report.ProcessingReport,com.github.fge.msgsimple.bundle.MessageBundle,com.github.fge.jsonschema.processors.data.FullData) otherwise (for accuracy reasons).

 
 public abstract class NumericValidator
     extends AbstractKeywordValidator
 {
    
The keyword value
 
     protected final JsonNode number;

    
Does the keyword value fits into a long?
 
     private final boolean isLong;
 
     protected NumericValidator(final String keywordfinal JsonNode digest)
     {
         super(keyword);
          = digest.get(keyword);
          = digest.get("valueIsLong").booleanValue();
     }
 
     @Override
     public final void validate(final Processor<FullDataFullDataprocessor,
         final ProcessingReport reportfinal MessageBundle bundle,
         final FullData data)
         throws ProcessingException
     {
         final JsonNode instance = data.getInstance().getNode();
         if (valueIsLong(instance) && )
             validateLong(reportbundledata);
         else
             validateDecimal(reportbundledata);
     }

    
Method to be implemented by a numeric validator if both the keyword value and instance value fit into a long

Parameters:
report the validation report
bundle the message bundle to use
data the validation data
 
     protected abstract void validateLong(final ProcessingReport report,
         final MessageBundle bundlefinal FullData data)
         throws ProcessingException;

    
Method to be implemented by a numeric validator if either of the keyword value or instance value do not fit into a long

Parameters:
report the validation report
bundle the message bundle to use
data the validation data
 
     protected abstract void validateDecimal(final ProcessingReport report,
         final MessageBundle bundlefinal FullData data)
         throws ProcessingException;
 
     @Override
     public final String toString()
     {
         return  + ": " + ;
    }

    
Test whether a numeric instance is a long

We use both a test on the instance type and Jackson's com.fasterxml.jackson.databind.JsonNode.canConvertToLong(). The first test is needed since the latter method will also return true if the value is a decimal which integral part fits into a long, and we don't want that.

Parameters:
node the node to test
Returns:
true if both conditions are true
    private static boolean valueIsLong(final JsonNode node)
    {
        return NodeType.getNodeType(node) == .
            && node.canConvertToLong();
    }
New to GrepCode? Check out our FAQ X