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

io.github.mivek.parser.AbstractParser Maven / Gradle / Ivy

package io.github.mivek.parser;

import io.github.mivek.command.AirportSupplier;
import io.github.mivek.command.common.Command;
import io.github.mivek.command.common.CommonCommandSupplier;
import io.github.mivek.enums.Descriptive;
import io.github.mivek.enums.Intensity;
import io.github.mivek.enums.Phenomenon;
import io.github.mivek.exception.ParseException;
import io.github.mivek.model.AbstractWeatherCode;
import io.github.mivek.model.AbstractWeatherContainer;
import io.github.mivek.model.Visibility;
import io.github.mivek.model.WeatherCondition;
import io.github.mivek.utils.Regex;

import java.time.LocalTime;
import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * Abstract class for parser.
 *
 * @param  a concrete subclass of {@link AbstractWeatherCode}.
 * @author mivek
 * Abstract class for Parser.
 */
public abstract class AbstractParser {

    /** From shortcut constant. */
    protected static final String FM = "FM";
    /** Tempo shortcut constant. */
    protected static final String TEMPO = "TEMPO";
    /** BECMG shortcut constant. */
    protected static final String BECMG = "BECMG";
    /** Pattern for RMK. */
    protected static final String RMK = "RMK";
    /** Pattern regex to tokenize the code. */
    private static final Pattern TOKENIZE_REGEX = Pattern.compile("\\s((?=\\d/\\dSM)(?10km");
            return true;
        }

        Command command = commonSupplier.get(pPart);
        if (command != null && command.canParse(pPart)) {
            return command.execute(pContainer, pPart);
        }

        WeatherCondition wc = parseWeatherCondition(pPart);
        return pContainer.addWeatherCondition(wc);
    }

    /***
     * Adds the remark part to the event.
     * @param pContainer the event to update
     * @param pParts the tokens of the event
     * @param index the RMK index in the event.
     */
    void parseRMK(final AbstractWeatherContainer pContainer, final String[] pParts, final int index) {
        String[] subArray = Arrays.copyOfRange(pParts, index + 1, pParts.length);
        pContainer.setRemark(remarkParser.parse(String.join(" ", subArray)));
    }

    /**
     * Splits a string between spaces except if the space is between two digits with
     * SM.
     *
     * @param pCode the string to parse
     * @return a array of tokens
     */
    String[] tokenize(final String pCode) {
        return TOKENIZE_REGEX.split(pCode);
    }

    /**
     * @return the Airport supplier.
     */
    AirportSupplier getAirportSupplier() {
        return airportSupplier;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy