java类org.joda.time.format.PeriodParser的实例源码

DateTimeUtils.java 文件源码 项目:presto 阅读 27 收藏 0 点赞 0 评论 0
@Override
public int parseInto(ReadWritablePeriod period, String text, int position, Locale locale)
{
    int bestValidPos = position;
    ReadWritablePeriod bestValidPeriod = null;

    int bestInvalidPos = position;

    for (PeriodParser parser : parsers) {
        ReadWritablePeriod parsedPeriod = new MutablePeriod();
        int parsePos = parser.parseInto(parsedPeriod, text, position, locale);
        if (parsePos >= position) {
            if (parsePos > bestValidPos) {
                bestValidPos = parsePos;
                bestValidPeriod = parsedPeriod;
                if (parsePos >= text.length()) {
                    break;
                }
            }
        }
        else if (parsePos < 0) {
            parsePos = ~parsePos;
            if (parsePos > bestInvalidPos) {
                bestInvalidPos = parsePos;
            }
        }
    }

    if (bestValidPos > position || (bestValidPos == position)) {
        // Restore the state to the best valid parse.
        if (bestValidPeriod != null) {
            period.setPeriod(bestValidPeriod);
        }
        return bestValidPos;
    }

    return ~bestInvalidPos;
}
DateTimeUtils.java 文件源码 项目:presto 阅读 24 收藏 0 点赞 0 评论 0
private static PeriodFormatter cretePeriodFormatter(IntervalField startField, IntervalField endField)
{
    if (endField == null) {
        endField = startField;
    }

    List<PeriodParser> parsers = new ArrayList<>();

    PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
    //CHECKSTYLE.OFF
    switch (startField) {
        case YEAR:
            builder.appendYears();
            parsers.add(builder.toParser());
            if (endField == IntervalField.YEAR) {
                break;
            }
            builder.appendLiteral("-");
        case MONTH:
            builder.appendMonths();
            parsers.add(builder.toParser());
            if (endField != IntervalField.MONTH) {
                throw new IllegalArgumentException("Invalid interval qualifier: " + startField + " to " + endField);
            }
            break;

        case DAY:
            builder.appendDays();
            parsers.add(builder.toParser());
            if (endField == IntervalField.DAY) {
                break;
            }
            builder.appendLiteral(" ");

        case HOUR:
            builder.appendHours();
            parsers.add(builder.toParser());
            if (endField == IntervalField.HOUR) {
                break;
            }
            builder.appendLiteral(":");

        case MINUTE:
            builder.appendMinutes();
            parsers.add(builder.toParser());
            if (endField == IntervalField.MINUTE) {
                break;
            }
            builder.appendLiteral(":");

        case SECOND:
            builder.appendSecondsWithOptionalMillis();
            parsers.add(builder.toParser());
    }
    //CHECKSTYLE.ON

    return new PeriodFormatter(builder.toPrinter(), new OrderedPeriodParser(parsers));
}
DateTimeUtils.java 文件源码 项目:presto 阅读 18 收藏 0 点赞 0 评论 0
private OrderedPeriodParser(List<PeriodParser> parsers)
{
    this.parsers = parsers;
}


问题


面经


文章

微信
公众号

扫码关注公众号