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

org.jcamp.parser.ReplacingCheckHandler Maven / Gradle / Ivy

Go to download

The JCAMP-DX project is the reference implemention of the IUPAC JCAMP-DX spectroscopy data standard.

There is a newer version: 0.9.2
Show newest version
package org.jcamp.parser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * check handler for ASDF parser that uses x and y-checks.
 * @author Thomas Weber
 */
public class ReplacingCheckHandler implements ASDFCheckHandler {
    private double firstX;
    private double lastX;
    private double deltaX;
    private double xFactor;
    private int nPoints;
    private static double EPS = Double.MIN_VALUE * 5;
    private static Log log = LogFactory.getLog(ReplacingCheckHandler.class);

    /**
     * ReplacingCheckHandler constructor comment.
     */
    public ReplacingCheckHandler(double firstX, double lastX, double xFactor, int nPoints) {
        super();
        this.firstX = firstX;
        this.lastX = lastX;
        this.xFactor = xFactor;
        this.deltaX = (lastX - firstX) / (nPoints - 1);
        this.nPoints = nPoints;
    }

    /**
     * @see com.creon.chem.jcamp.ASDFCheckHandler
     */
    public int check(ASDFParseState state) {
        // perform x check
        double checkX = xFactor * state.getCheckX();
        double realX = firstX + state.getCurrentIndex() * deltaX;
        if (Math.abs(checkX - realX) * xFactor > 0.1) {
            log.warn(
                "x check failed in line"
                    + state.getCurrentLineNumber()
                    + ": expected x = "
                    + Double.toString(checkX)
                    + " but got "
                    + Double.toString(realX));
            int newIndex = (int) Math.floor((checkX - firstX) / deltaX + .5);
            if (newIndex < 0 || newIndex >= nPoints) {
                // x check is bad, do not modify anything
                log.error("bad x check value");
                return IGNORE;
            } else
                state.setCurrentIndex(newIndex);
            return REPLACE;
        }
        // perform y check
        int checkY = state.getCheckY();
        int realY = state.getCurrentY();
        if (checkY != realY) {
            log.warn(
                "y check failed in line "
                    + state.getCurrentLineNumber()
                    + ": expected y = "
                    + Integer.toString(checkY)
                    + " but got "
                    + Integer.toString(realY));
            return REPLACE;
        }
        if (log.isDebugEnabled())
            log.debug("line " + state.getCurrentLineNumber() + ": check passed");
        return IGNORE;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy