提交 5ea97cd3 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add DateTimeUtils.validateInterval()

上级 ca01805e
......@@ -106,16 +106,8 @@ public final class Interval {
* values of all remaining fields
*/
public Interval(IntervalQualifier qualifier, boolean negative, long leading, long remaining) {
if (qualifier == null) {
throw new NullPointerException();
}
if (leading == 0L && remaining == 0L) {
negative = false;
} else if (leading < 0L || remaining < 0L) {
throw new RuntimeException();
}
this.qualifier = qualifier;
this.negative = negative;
this.negative = DateTimeUtils.validateInterval(qualifier, negative, leading, remaining);
this.leading = leading;
this.remaining = remaining;
}
......
......@@ -2054,6 +2054,70 @@ public class DateTimeUtils {
return Math.abs(absolute.longValue());
}
/**
* Ensures that all fields in interval are valid.
*
* @param qualifier
* qualifier
* @param negative
* whether interval is negative
* @param leading
* value of leading field
* @param remaining
* values of all remaining fields
* @return fixed value of negative field
*/
public static boolean validateInterval(IntervalQualifier qualifier, boolean negative, long leading, long remaining)
{
if (qualifier == null) {
throw new NullPointerException();
}
if (leading == 0L && remaining == 0L) {
return false;
}
// Upper bound for remaining value (exclusive)
long bound;
switch (qualifier) {
case YEAR:
case MONTH:
case DAY:
case HOUR:
case MINUTE:
bound = 1;
break;
case SECOND:
bound = NANOS_PER_SECOND;
break;
case YEAR_TO_MONTH:
bound = 12;
break;
case DAY_TO_HOUR:
bound = 24;
break;
case DAY_TO_MINUTE:
bound = 24 * 60;
break;
case DAY_TO_SECOND:
bound = NANOS_PER_DAY;
break;
case HOUR_TO_MINUTE:
bound = 60;
break;
case HOUR_TO_SECOND:
bound = NANOS_PER_HOUR;
break;
case MINUTE_TO_SECOND:
bound = NANOS_PER_MINUTE;
break;
default:
throw DbException.getInvalidValueException("interval", qualifier);
}
if (leading < 0L || leading >= 1_000_000_000_000_000_000L || remaining < 0L || remaining >= bound) {
throw DbException.getInvalidValueException("interval", Long.toString(leading));
}
return negative;
}
/**
* @param qualifier
* qualifier
......
......@@ -58,11 +58,7 @@ public class ValueInterval extends Value {
* @return interval value
*/
public static ValueInterval from(IntervalQualifier qualifier, boolean negative, long leading, long remaining) {
if (leading == 0L && remaining == 0L) {
negative = false;
} else if (leading < 0L || remaining < 0L) {
throw new RuntimeException();
}
negative = DateTimeUtils.validateInterval(qualifier, negative, leading, remaining);
return (ValueInterval) Value.cache(
new ValueInterval(qualifier.ordinal() + INTERVAL_YEAR, negative, leading, remaining));
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论