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

com.conveyal.gtfs.loader.BooleanField Maven / Gradle / Ivy

Go to download

A library to load and index GTFS feeds of arbitrary size using disk-backed storage

There is a newer version: 6.2.0
Show newest version
package com.conveyal.gtfs.loader;

import com.conveyal.gtfs.error.NewGTFSError;
import com.conveyal.gtfs.error.NewGTFSErrorType;
import com.conveyal.gtfs.storage.StorageException;

import java.sql.JDBCType;
import java.sql.PreparedStatement;
import java.sql.SQLType;
import java.util.Set;

/**
 * A GTFS boolean field, coded as a single character string 0 or 1.
 * It is stored in an SQL integer field.
 */
public class BooleanField extends Field {

    public BooleanField (String name, Requirement requirement) {
        super(name, requirement);
    }

    private ValidateFieldResult validate (String string) {
        ValidateFieldResult result = new ValidateFieldResult<>();
        if ( ! ("0".equals(string) || "1".equals(string))) {
            result.errors.add(NewGTFSError.forFeed(NewGTFSErrorType.BOOLEAN_FORMAT, string));
        }
        result.clean = "1".equals(string);
        return result;
    }

    @Override
    public Set setParameter (PreparedStatement preparedStatement, int oneBasedIndex, String string) {
        try {
            ValidateFieldResult result = validate(string);
            preparedStatement.setBoolean(oneBasedIndex, result.clean);
            return result.errors;
        } catch (Exception ex) {
            throw new StorageException(ex);
        }
    }

    /**
     * The 0 or 1 will be converted to the string "true" or "false" for SQL COPY.
     */
    @Override
    public ValidateFieldResult validateAndConvert (String string) {
        return ValidateFieldResult.from(validate(string));
    }

    @Override
    public SQLType getSqlType () {
        return JDBCType.BOOLEAN;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy