Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2013, Francis Galiegue <fgaliegue@gmail.com>
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the Lesser GNU General Public License as
   * published by the Free Software Foundation, either version 3 of the
   * License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * Lesser GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 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.report.ProcessingReport,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.report.ProcessingReport,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 FullData data)
         throws ProcessingException
     {
         final JsonNode instance = data.getInstance().getNode();
         if (valueIsLong(instance) && )
             validateLong(reportdata);
         else
             validateDecimal(reportdata);
     }

    
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
data the validation data
 
     protected abstract void validateLong(final ProcessingReport report,
         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
data the validation data
 
     protected abstract void validateDecimal(final ProcessingReport report,
         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