| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util; |
| 7 |
|
| 8 |
import static org.h2.util.DateTimeUtils.NANOS_PER_DAY; |
| 9 |
import static org.h2.util.DateTimeUtils.NANOS_PER_HOUR; |
| 10 |
import static org.h2.util.DateTimeUtils.NANOS_PER_MINUTE; |
| 11 |
import static org.h2.util.DateTimeUtils.NANOS_PER_SECOND; |
| 12 |
|
| 13 |
import java.math.BigInteger; |
| 14 |
|
| 15 |
import org.h2.api.ErrorCode; |
| 16 |
import org.h2.api.IntervalQualifier; |
| 17 |
import org.h2.message.DbException; |
| 18 |
import org.h2.value.ValueInterval; |
| 19 |
|
| 20 |
/** |
| 21 |
* This utility class contains interval conversion functions. |
| 22 |
*/ |
| 23 |
public class IntervalUtils { |
| 24 |
|
| 25 |
private static final BigInteger NANOS_PER_SECOND_BI = BigInteger.valueOf(NANOS_PER_SECOND); |
| 26 |
|
| 27 |
private static final BigInteger NANOS_PER_MINUTE_BI = BigInteger.valueOf(NANOS_PER_MINUTE); |
| 28 |
|
| 29 |
private static final BigInteger NANOS_PER_HOUR_BI = BigInteger.valueOf(NANOS_PER_HOUR); |
| 30 |
|
| 31 |
/** |
| 32 |
* The number of nanoseconds per day as BigInteger. |
| 33 |
*/ |
| 34 |
public static final BigInteger NANOS_PER_DAY_BI = BigInteger.valueOf(NANOS_PER_DAY); |
| 35 |
|
| 36 |
private static final BigInteger MONTHS_PER_YEAR_BI = BigInteger.valueOf(12); |
| 37 |
|
| 38 |
private static final BigInteger HOURS_PER_DAY_BI = BigInteger.valueOf(24); |
| 39 |
|
| 40 |
private static final BigInteger MINUTES_PER_DAY_BI = BigInteger.valueOf(24 * 60); |
| 41 |
|
| 42 |
private static final BigInteger MINUTES_PER_HOUR_BI = BigInteger.valueOf(60); |
| 43 |
|
| 44 |
private static final BigInteger LEADING_MIN = BigInteger.valueOf(-999_999_999_999_999_999L); |
| 45 |
|
| 46 |
private static final BigInteger LEADING_MAX = BigInteger.valueOf(999_999_999_999_999_999L); |
| 47 |
|
| 48 |
private IntervalUtils() { |
| 49 |
// utility class |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Parses the specified string as {@code INTERVAL} value. |
| 54 |
* |
| 55 |
* @param qualifier |
| 56 |
* the default qualifier to use if string does not have one |
| 57 |
* @param s |
| 58 |
* the string with type information to parse |
| 59 |
* @return the interval value. Type of value can be different from the |
| 60 |
* specified qualifier. |
| 61 |
*/ |
| 62 |
public static ValueInterval parseFormattedInterval(IntervalQualifier qualifier, String s) { |
| 63 |
int i = 0; |
| 64 |
i = skipWS(s, i); |
| 65 |
if (!s.regionMatches(true, i, "INTERVAL", 0, 8)) { |
| 66 |
return parseInterval(qualifier, false, s); |
| 67 |
} |
| 68 |
i = skipWS(s, i + 8); |
| 69 |
boolean negative = false; |
| 70 |
char ch = s.charAt(i); |
| 71 |
if (ch == '-') { |
| 72 |
negative = true; |
| 73 |
i = skipWS(s, i + 1); |
| 74 |
ch = s.charAt(i); |
| 75 |
} else if (ch == '+') { |
| 76 |
i = skipWS(s, i + 1); |
| 77 |
ch = s.charAt(i); |
| 78 |
} |
| 79 |
if (ch != '\'') { |
| 80 |
throw new IllegalArgumentException(s); |
| 81 |
} |
| 82 |
int start = ++i; |
| 83 |
int l = s.length(); |
| 84 |
for (;;) { |
| 85 |
if (i == l) { |
| 86 |
throw new IllegalArgumentException(s); |
| 87 |
} |
| 88 |
if (s.charAt(i) == '\'') { |
| 89 |
break; |
| 90 |
} |
| 91 |
i++; |
| 92 |
} |
| 93 |
String v = s.substring(start, i); |
| 94 |
i = skipWS(s, i + 1); |
| 95 |
if (s.regionMatches(true, i, "YEAR", 0, 4)) { |
| 96 |
i += 4; |
| 97 |
int j = skipWSEnd(s, i); |
| 98 |
if (j == l) { |
| 99 |
return parseInterval(IntervalQualifier.YEAR, negative, v); |
| 100 |
} |
| 101 |
if (j > i && s.regionMatches(true, j, "TO", 0, 2)) { |
| 102 |
j += 2; |
| 103 |
i = skipWS(s, j); |
| 104 |
if (i > j && s.regionMatches(true, i, "MONTH", 0, 5)) { |
| 105 |
if (skipWSEnd(s, i + 5) == l) { |
| 106 |
return parseInterval(IntervalQualifier.YEAR_TO_MONTH, negative, v); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} else if (s.regionMatches(true, i, "MONTH", 0, 5)) { |
| 111 |
if (skipWSEnd(s, i + 5) == l) { |
| 112 |
return parseInterval(IntervalQualifier.MONTH, negative, v); |
| 113 |
} |
| 114 |
} |
| 115 |
if (s.regionMatches(true, i, "DAY", 0, 3)) { |
| 116 |
i += 3; |
| 117 |
int j = skipWSEnd(s, i); |
| 118 |
if (j == l) { |
| 119 |
return parseInterval(IntervalQualifier.DAY, negative, v); |
| 120 |
} |
| 121 |
if (j > i && s.regionMatches(true, j, "TO", 0, 2)) { |
| 122 |
j += 2; |
| 123 |
i = skipWS(s, j); |
| 124 |
if (i > j) { |
| 125 |
if (s.regionMatches(true, i, "HOUR", 0, 4)) { |
| 126 |
if (skipWSEnd(s, i + 4) == l) { |
| 127 |
return parseInterval(IntervalQualifier.DAY_TO_HOUR, negative, v); |
| 128 |
} |
| 129 |
} else if (s.regionMatches(true, i, "MINUTE", 0, 6)) { |
| 130 |
if (skipWSEnd(s, i + 6) == l) { |
| 131 |
return parseInterval(IntervalQualifier.DAY_TO_MINUTE, negative, v); |
| 132 |
} |
| 133 |
} else if (s.regionMatches(true, i, "SECOND", 0, 6)) { |
| 134 |
if (skipWSEnd(s, i + 6) == l) { |
| 135 |
return parseInterval(IntervalQualifier.DAY_TO_SECOND, negative, v); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
if (s.regionMatches(true, i, "HOUR", 0, 4)) { |
| 142 |
i += 4; |
| 143 |
int j = skipWSEnd(s, i); |
| 144 |
if (j == l) { |
| 145 |
return parseInterval(IntervalQualifier.HOUR, negative, v); |
| 146 |
} |
| 147 |
if (j > i && s.regionMatches(true, j, "TO", 0, 2)) { |
| 148 |
j += 2; |
| 149 |
i = skipWS(s, j); |
| 150 |
if (i > j) { |
| 151 |
if (s.regionMatches(true, i, "MINUTE", 0, 6)) { |
| 152 |
if (skipWSEnd(s, i + 6) == l) { |
| 153 |
return parseInterval(IntervalQualifier.HOUR_TO_MINUTE, negative, v); |
| 154 |
} |
| 155 |
} else if (s.regionMatches(true, i, "SECOND", 0, 6)) { |
| 156 |
if (skipWSEnd(s, i + 6) == l) { |
| 157 |
return parseInterval(IntervalQualifier.HOUR_TO_SECOND, negative, v); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
if (s.regionMatches(true, i, "MINUTE", 0, 6)) { |
| 164 |
i += 6; |
| 165 |
int j = skipWSEnd(s, i); |
| 166 |
if (j == l) { |
| 167 |
return parseInterval(IntervalQualifier.MINUTE, negative, v); |
| 168 |
} |
| 169 |
if (j > i && s.regionMatches(true, j, "TO", 0, 2)) { |
| 170 |
j += 2; |
| 171 |
i = skipWS(s, j); |
| 172 |
if (i > j && s.regionMatches(true, i, "SECOND", 0, 6)) { |
| 173 |
if (skipWSEnd(s, i + 6) == l) { |
| 174 |
return parseInterval(IntervalQualifier.MINUTE_TO_SECOND, negative, v); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
if (s.regionMatches(true, i, "SECOND", 0, 6)) { |
| 180 |
if (skipWSEnd(s, i + 6) == l) { |
| 181 |
return parseInterval(IntervalQualifier.SECOND, negative, v); |
| 182 |
} |
| 183 |
} |
| 184 |
throw new IllegalArgumentException(s); |
| 185 |
} |
| 186 |
|
| 187 |
private static int skipWS(String s, int i) { |
| 188 |
for (int l = s.length();; i++) { |
| 189 |
if (i == l) { |
| 190 |
throw new IllegalArgumentException(s); |
| 191 |
} |
| 192 |
if (!Character.isWhitespace(s.charAt(i))) { |
| 193 |
return i; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
private static int skipWSEnd(String s, int i) { |
| 199 |
for (int l = s.length();; i++) { |
| 200 |
if (i == l) { |
| 201 |
return i; |
| 202 |
} |
| 203 |
if (!Character.isWhitespace(s.charAt(i))) { |
| 204 |
return i; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Parses the specified string as {@code INTERVAL} value. |
| 211 |
* |
| 212 |
* @param qualifier |
| 213 |
* the qualifier of interval |
| 214 |
* @param negative |
| 215 |
* whether the interval is negative |
| 216 |
* @param s |
| 217 |
* the string to parse |
| 218 |
* @return the interval value |
| 219 |
*/ |
| 220 |
public static ValueInterval parseInterval(IntervalQualifier qualifier, boolean negative, String s) { |
| 221 |
long leading, remaining; |
| 222 |
switch (qualifier) { |
| 223 |
case YEAR: |
| 224 |
case MONTH: |
| 225 |
case DAY: |
| 226 |
case HOUR: |
| 227 |
case MINUTE: |
| 228 |
leading = parseIntervalLeading(s, 0, s.length(), negative); |
| 229 |
remaining = 0; |
| 230 |
break; |
| 231 |
case SECOND: { |
| 232 |
int dot = s.indexOf('.'); |
| 233 |
if (dot < 0) { |
| 234 |
leading = parseIntervalLeading(s, 0, s.length(), negative); |
| 235 |
remaining = 0; |
| 236 |
} else { |
| 237 |
leading = parseIntervalLeading(s, 0, dot, negative); |
| 238 |
remaining = DateTimeUtils.parseNanos(s, dot + 1, s.length()); |
| 239 |
} |
| 240 |
break; |
| 241 |
} |
| 242 |
case YEAR_TO_MONTH: |
| 243 |
return parseInterval2(qualifier, s, '-', 11, negative); |
| 244 |
case DAY_TO_HOUR: |
| 245 |
return parseInterval2(qualifier, s, ' ', 23, negative); |
| 246 |
case DAY_TO_MINUTE: { |
| 247 |
int space = s.indexOf(' '); |
| 248 |
if (space < 0) { |
| 249 |
leading = parseIntervalLeading(s, 0, s.length(), negative); |
| 250 |
remaining = 0; |
| 251 |
} else { |
| 252 |
leading = parseIntervalLeading(s, 0, space, negative); |
| 253 |
int colon = s.indexOf(':', space + 1); |
| 254 |
if (colon < 0) { |
| 255 |
remaining = parseIntervalRemaining(s, space + 1, s.length(), 23) * 60; |
| 256 |
} else { |
| 257 |
remaining = parseIntervalRemaining(s, space + 1, colon, 23) * 60 |
| 258 |
+ parseIntervalRemaining(s, colon + 1, s.length(), 59); |
| 259 |
} |
| 260 |
} |
| 261 |
break; |
| 262 |
} |
| 263 |
case DAY_TO_SECOND: { |
| 264 |
int space = s.indexOf(' '); |
| 265 |
if (space < 0) { |
| 266 |
leading = parseIntervalLeading(s, 0, s.length(), negative); |
| 267 |
remaining = 0; |
| 268 |
} else { |
| 269 |
leading = parseIntervalLeading(s, 0, space, negative); |
| 270 |
int colon = s.indexOf(':', space + 1); |
| 271 |
if (colon < 0) { |
| 272 |
remaining = parseIntervalRemaining(s, space + 1, s.length(), 23) * NANOS_PER_HOUR; |
| 273 |
} else { |
| 274 |
int colon2 = s.indexOf(':', colon + 1); |
| 275 |
if (colon2 < 0) { |
| 276 |
remaining = parseIntervalRemaining(s, space + 1, colon, 23) * NANOS_PER_HOUR |
| 277 |
+ parseIntervalRemaining(s, colon + 1, s.length(), 59) * NANOS_PER_MINUTE; |
| 278 |
} else { |
| 279 |
remaining = parseIntervalRemaining(s, space + 1, colon, 23) * NANOS_PER_HOUR |
| 280 |
+ parseIntervalRemaining(s, colon + 1, colon2, 59) * NANOS_PER_MINUTE |
| 281 |
+ parseIntervalRemainingSeconds(s, colon2 + 1); |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
break; |
| 286 |
} |
| 287 |
case HOUR_TO_MINUTE: |
| 288 |
return parseInterval2(qualifier, s, ':', 59, negative); |
| 289 |
case HOUR_TO_SECOND: { |
| 290 |
int colon = s.indexOf(':'); |
| 291 |
if (colon < 0) { |
| 292 |
leading = parseIntervalLeading(s, 0, s.length(), negative); |
| 293 |
remaining = 0; |
| 294 |
} else { |
| 295 |
leading = parseIntervalLeading(s, 0, colon, negative); |
| 296 |
int colon2 = s.indexOf(':', colon + 1); |
| 297 |
if (colon2 < 0) { |
| 298 |
remaining = parseIntervalRemaining(s, colon + 1, s.length(), 59) * NANOS_PER_MINUTE; |
| 299 |
} else { |
| 300 |
remaining = parseIntervalRemaining(s, colon + 1, colon2, 59) * NANOS_PER_MINUTE |
| 301 |
+ parseIntervalRemainingSeconds(s, colon2 + 1); |
| 302 |
} |
| 303 |
} |
| 304 |
break; |
| 305 |
} |
| 306 |
case MINUTE_TO_SECOND: { |
| 307 |
int dash = s.indexOf(':'); |
| 308 |
if (dash < 0) { |
| 309 |
leading = parseIntervalLeading(s, 0, s.length(), negative); |
| 310 |
remaining = 0; |
| 311 |
} else { |
| 312 |
leading = parseIntervalLeading(s, 0, dash, negative); |
| 313 |
remaining = parseIntervalRemainingSeconds(s, dash + 1); |
| 314 |
} |
| 315 |
break; |
| 316 |
} |
| 317 |
default: |
| 318 |
throw new IllegalArgumentException(); |
| 319 |
} |
| 320 |
negative = leading < 0; |
| 321 |
if (negative) { |
| 322 |
if (leading != Long.MIN_VALUE) { |
| 323 |
leading = -leading; |
| 324 |
} else { |
| 325 |
leading = 0; |
| 326 |
} |
| 327 |
} |
| 328 |
return ValueInterval.from(qualifier, negative, leading, remaining); |
| 329 |
} |
| 330 |
|
| 331 |
private static ValueInterval parseInterval2(IntervalQualifier qualifier, String s, |
| 332 |
char ch, int max, boolean negative) { |
| 333 |
long leading; |
| 334 |
long remaining; |
| 335 |
int dash = s.indexOf(ch, 1); |
| 336 |
if (dash < 0) { |
| 337 |
leading = parseIntervalLeading(s, 0, s.length(), negative); |
| 338 |
remaining = 0; |
| 339 |
} else { |
| 340 |
leading = parseIntervalLeading(s, 0, dash, negative); |
| 341 |
remaining = parseIntervalRemaining(s, dash + 1, s.length(), max); |
| 342 |
} |
| 343 |
negative = leading < 0; |
| 344 |
if (negative) { |
| 345 |
if (leading != Long.MIN_VALUE) { |
| 346 |
leading = -leading; |
| 347 |
} else { |
| 348 |
leading = 0; |
| 349 |
} |
| 350 |
} |
| 351 |
return ValueInterval.from(qualifier, negative, leading, remaining); |
| 352 |
} |
| 353 |
|
| 354 |
private static long parseIntervalLeading(String s, int start, int end, boolean negative) { |
| 355 |
long leading = Long.parseLong(s.substring(start, end)); |
| 356 |
if (leading == 0) { |
| 357 |
return negative ^ s.charAt(start) == '-' ? Long.MIN_VALUE : 0; |
| 358 |
} |
| 359 |
return negative ? -leading : leading; |
| 360 |
} |
| 361 |
|
| 362 |
private static long parseIntervalRemaining(String s, int start, int end, int max) { |
| 363 |
int v = StringUtils.parseUInt31(s, start, end); |
| 364 |
if (v > max) { |
| 365 |
throw new IllegalArgumentException(s); |
| 366 |
} |
| 367 |
return v; |
| 368 |
} |
| 369 |
|
| 370 |
private static long parseIntervalRemainingSeconds(String s, int start) { |
| 371 |
int seconds, nanos; |
| 372 |
int dot = s.indexOf('.', start + 1); |
| 373 |
if (dot < 0) { |
| 374 |
seconds = StringUtils.parseUInt31(s, start, s.length()); |
| 375 |
nanos = 0; |
| 376 |
} else { |
| 377 |
seconds = StringUtils.parseUInt31(s, start, dot); |
| 378 |
nanos = DateTimeUtils.parseNanos(s, dot + 1, s.length()); |
| 379 |
} |
| 380 |
if (seconds > 59) { |
| 381 |
throw new IllegalArgumentException(s); |
| 382 |
} |
| 383 |
return seconds * NANOS_PER_SECOND + nanos; |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Formats interval as a string and appends it to a specified string |
| 388 |
* builder. |
| 389 |
* |
| 390 |
* @param buff |
| 391 |
* string builder to append to |
| 392 |
* @param qualifier |
| 393 |
* qualifier of the interval |
| 394 |
* @param negative |
| 395 |
* whether interval is negative |
| 396 |
* @param leading |
| 397 |
* the value of leading field |
| 398 |
* @param remaining |
| 399 |
* the value of all remaining fields |
| 400 |
* @return the specified string builder |
| 401 |
*/ |
| 402 |
public static StringBuilder appendInterval(StringBuilder buff, IntervalQualifier qualifier, boolean negative, |
| 403 |
long leading, long remaining) { |
| 404 |
buff.append("INTERVAL '"); |
| 405 |
if (negative) { |
| 406 |
buff.append('-'); |
| 407 |
} |
| 408 |
switch (qualifier) { |
| 409 |
case YEAR: |
| 410 |
case MONTH: |
| 411 |
case DAY: |
| 412 |
case HOUR: |
| 413 |
case MINUTE: |
| 414 |
buff.append(leading); |
| 415 |
break; |
| 416 |
case SECOND: |
| 417 |
DateTimeUtils.appendNanos(buff.append(leading), (int) remaining); |
| 418 |
break; |
| 419 |
case YEAR_TO_MONTH: |
| 420 |
buff.append(leading).append('-').append(remaining); |
| 421 |
break; |
| 422 |
case DAY_TO_HOUR: |
| 423 |
buff.append(leading).append(' '); |
| 424 |
StringUtils.appendTwoDigits(buff, (int) remaining); |
| 425 |
break; |
| 426 |
case DAY_TO_MINUTE: { |
| 427 |
buff.append(leading).append(' '); |
| 428 |
int r = (int) remaining; |
| 429 |
StringUtils.appendTwoDigits(buff, r / 60).append(':'); |
| 430 |
StringUtils.appendTwoDigits(buff, r % 60); |
| 431 |
break; |
| 432 |
} |
| 433 |
case DAY_TO_SECOND: { |
| 434 |
long nanos = remaining % NANOS_PER_MINUTE; |
| 435 |
int r = (int) (remaining / NANOS_PER_MINUTE); |
| 436 |
buff.append(leading).append(' '); |
| 437 |
StringUtils.appendTwoDigits(buff, r / 60).append(':'); |
| 438 |
StringUtils.appendTwoDigits(buff, r % 60).append(':'); |
| 439 |
StringUtils.appendTwoDigits(buff, (int) (nanos / NANOS_PER_SECOND)); |
| 440 |
DateTimeUtils.appendNanos(buff, (int) (nanos % NANOS_PER_SECOND)); |
| 441 |
break; |
| 442 |
} |
| 443 |
case HOUR_TO_MINUTE: |
| 444 |
buff.append(leading).append(':'); |
| 445 |
StringUtils.appendTwoDigits(buff, (int) remaining); |
| 446 |
break; |
| 447 |
case HOUR_TO_SECOND: { |
| 448 |
buff.append(leading).append(':'); |
| 449 |
StringUtils.appendTwoDigits(buff, (int) (remaining / NANOS_PER_MINUTE)).append(':'); |
| 450 |
long s = remaining % NANOS_PER_MINUTE; |
| 451 |
StringUtils.appendTwoDigits(buff, (int) (s / NANOS_PER_SECOND)); |
| 452 |
DateTimeUtils.appendNanos(buff, (int) (s % NANOS_PER_SECOND)); |
| 453 |
break; |
| 454 |
} |
| 455 |
case MINUTE_TO_SECOND: |
| 456 |
buff.append(leading).append(':'); |
| 457 |
StringUtils.appendTwoDigits(buff, (int) (remaining / NANOS_PER_SECOND)); |
| 458 |
DateTimeUtils.appendNanos(buff, (int) (remaining % NANOS_PER_SECOND)); |
| 459 |
break; |
| 460 |
} |
| 461 |
return buff.append("' ").append(qualifier); |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Converts interval value to an absolute value. |
| 466 |
* |
| 467 |
* @param interval |
| 468 |
* the interval value |
| 469 |
* @return absolute value in months for year-month intervals, in nanoseconds |
| 470 |
* for day-time intervals |
| 471 |
*/ |
| 472 |
public static BigInteger intervalToAbsolute(ValueInterval interval) { |
| 473 |
BigInteger r; |
| 474 |
switch (interval.getQualifier()) { |
| 475 |
case YEAR: |
| 476 |
r = BigInteger.valueOf(interval.getLeading()).multiply(MONTHS_PER_YEAR_BI); |
| 477 |
break; |
| 478 |
case MONTH: |
| 479 |
r = BigInteger.valueOf(interval.getLeading()); |
| 480 |
break; |
| 481 |
case DAY: |
| 482 |
r = BigInteger.valueOf(interval.getLeading()).multiply(NANOS_PER_DAY_BI); |
| 483 |
break; |
| 484 |
case HOUR: |
| 485 |
r = BigInteger.valueOf(interval.getLeading()).multiply(NANOS_PER_HOUR_BI); |
| 486 |
break; |
| 487 |
case MINUTE: |
| 488 |
r = BigInteger.valueOf(interval.getLeading()).multiply(NANOS_PER_MINUTE_BI); |
| 489 |
break; |
| 490 |
case SECOND: |
| 491 |
r = intervalToAbsolute(interval, NANOS_PER_SECOND_BI); |
| 492 |
break; |
| 493 |
case YEAR_TO_MONTH: |
| 494 |
r = intervalToAbsolute(interval, MONTHS_PER_YEAR_BI); |
| 495 |
break; |
| 496 |
case DAY_TO_HOUR: |
| 497 |
r = intervalToAbsolute(interval, HOURS_PER_DAY_BI, NANOS_PER_HOUR_BI); |
| 498 |
break; |
| 499 |
case DAY_TO_MINUTE: |
| 500 |
r = intervalToAbsolute(interval, MINUTES_PER_DAY_BI, NANOS_PER_MINUTE_BI); |
| 501 |
break; |
| 502 |
case DAY_TO_SECOND: |
| 503 |
r = intervalToAbsolute(interval, NANOS_PER_DAY_BI); |
| 504 |
break; |
| 505 |
case HOUR_TO_MINUTE: |
| 506 |
r = intervalToAbsolute(interval, MINUTES_PER_HOUR_BI, NANOS_PER_MINUTE_BI); |
| 507 |
break; |
| 508 |
case HOUR_TO_SECOND: |
| 509 |
r = intervalToAbsolute(interval, NANOS_PER_HOUR_BI); |
| 510 |
break; |
| 511 |
case MINUTE_TO_SECOND: |
| 512 |
r = intervalToAbsolute(interval, NANOS_PER_MINUTE_BI); |
| 513 |
break; |
| 514 |
default: |
| 515 |
throw new IllegalArgumentException(); |
| 516 |
} |
| 517 |
return interval.isNegative() ? r.negate() : r; |
| 518 |
} |
| 519 |
|
| 520 |
private static BigInteger intervalToAbsolute(ValueInterval interval, BigInteger multiplier, |
| 521 |
BigInteger totalMultiplier) { |
| 522 |
return intervalToAbsolute(interval, multiplier).multiply(totalMultiplier); |
| 523 |
} |
| 524 |
|
| 525 |
private static BigInteger intervalToAbsolute(ValueInterval interval, BigInteger multiplier) { |
| 526 |
return BigInteger.valueOf(interval.getLeading()).multiply(multiplier) |
| 527 |
.add(BigInteger.valueOf(interval.getRemaining())); |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* Converts absolute value to an interval value. |
| 532 |
* |
| 533 |
* @param qualifier |
| 534 |
* the qualifier of interval |
| 535 |
* @param absolute |
| 536 |
* absolute value in months for year-month intervals, in |
| 537 |
* nanoseconds for day-time intervals |
| 538 |
* @return the interval value |
| 539 |
*/ |
| 540 |
public static ValueInterval intervalFromAbsolute(IntervalQualifier qualifier, BigInteger absolute) { |
| 541 |
switch (qualifier) { |
| 542 |
case YEAR: |
| 543 |
return ValueInterval.from(qualifier, absolute.signum() < 0, |
| 544 |
leadingExact(absolute.divide(MONTHS_PER_YEAR_BI)), 0); |
| 545 |
case MONTH: |
| 546 |
return ValueInterval.from(qualifier, absolute.signum() < 0, leadingExact(absolute), 0); |
| 547 |
case DAY: |
| 548 |
return ValueInterval.from(qualifier, absolute.signum() < 0, |
| 549 |
leadingExact(absolute.divide(NANOS_PER_DAY_BI)), 0); |
| 550 |
case HOUR: |
| 551 |
return ValueInterval.from(qualifier, absolute.signum() < 0, |
| 552 |
leadingExact(absolute.divide(NANOS_PER_HOUR_BI)), 0); |
| 553 |
case MINUTE: |
| 554 |
return ValueInterval.from(qualifier, absolute.signum() < 0, |
| 555 |
leadingExact(absolute.divide(NANOS_PER_MINUTE_BI)), 0); |
| 556 |
case SECOND: |
| 557 |
return intervalFromAbsolute(qualifier, absolute, NANOS_PER_SECOND_BI); |
| 558 |
case YEAR_TO_MONTH: |
| 559 |
return intervalFromAbsolute(qualifier, absolute, MONTHS_PER_YEAR_BI); |
| 560 |
case DAY_TO_HOUR: |
| 561 |
return intervalFromAbsolute(qualifier, absolute.divide(NANOS_PER_HOUR_BI), HOURS_PER_DAY_BI); |
| 562 |
case DAY_TO_MINUTE: |
| 563 |
return intervalFromAbsolute(qualifier, absolute.divide(NANOS_PER_MINUTE_BI), MINUTES_PER_DAY_BI); |
| 564 |
case DAY_TO_SECOND: |
| 565 |
return intervalFromAbsolute(qualifier, absolute, NANOS_PER_DAY_BI); |
| 566 |
case HOUR_TO_MINUTE: |
| 567 |
return intervalFromAbsolute(qualifier, absolute.divide(NANOS_PER_MINUTE_BI), MINUTES_PER_HOUR_BI); |
| 568 |
case HOUR_TO_SECOND: |
| 569 |
return intervalFromAbsolute(qualifier, absolute, NANOS_PER_HOUR_BI); |
| 570 |
case MINUTE_TO_SECOND: |
| 571 |
return intervalFromAbsolute(qualifier, absolute, NANOS_PER_MINUTE_BI); |
| 572 |
default: |
| 573 |
throw new IllegalArgumentException(); |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
private static ValueInterval intervalFromAbsolute(IntervalQualifier qualifier, BigInteger absolute, |
| 578 |
BigInteger divisor) { |
| 579 |
BigInteger[] dr = absolute.divideAndRemainder(divisor); |
| 580 |
return ValueInterval.from(qualifier, absolute.signum() < 0, leadingExact(dr[0]), Math.abs(dr[1].longValue())); |
| 581 |
} |
| 582 |
|
| 583 |
private static long leadingExact(BigInteger absolute) { |
| 584 |
if (absolute.compareTo(LEADING_MAX) > 0 || absolute.compareTo(LEADING_MIN) < 0) { |
| 585 |
throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, absolute.toString()); |
| 586 |
} |
| 587 |
return Math.abs(absolute.longValue()); |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Ensures that all fields in interval are valid. |
| 592 |
* |
| 593 |
* @param qualifier |
| 594 |
* qualifier |
| 595 |
* @param negative |
| 596 |
* whether interval is negative |
| 597 |
* @param leading |
| 598 |
* value of leading field |
| 599 |
* @param remaining |
| 600 |
* values of all remaining fields |
| 601 |
* @return fixed value of negative field |
| 602 |
*/ |
| 603 |
public static boolean validateInterval(IntervalQualifier qualifier, boolean negative, long leading, |
| 604 |
long remaining) { |
| 605 |
if (qualifier == null) { |
| 606 |
throw new NullPointerException(); |
| 607 |
} |
| 608 |
if (leading == 0L && remaining == 0L) { |
| 609 |
return false; |
| 610 |
} |
| 611 |
// Upper bound for remaining value (exclusive) |
| 612 |
long bound; |
| 613 |
switch (qualifier) { |
| 614 |
case YEAR: |
| 615 |
case MONTH: |
| 616 |
case DAY: |
| 617 |
case HOUR: |
| 618 |
case MINUTE: |
| 619 |
bound = 1; |
| 620 |
break; |
| 621 |
case SECOND: |
| 622 |
bound = NANOS_PER_SECOND; |
| 623 |
break; |
| 624 |
case YEAR_TO_MONTH: |
| 625 |
bound = 12; |
| 626 |
break; |
| 627 |
case DAY_TO_HOUR: |
| 628 |
bound = 24; |
| 629 |
break; |
| 630 |
case DAY_TO_MINUTE: |
| 631 |
bound = 24 * 60; |
| 632 |
break; |
| 633 |
case DAY_TO_SECOND: |
| 634 |
bound = NANOS_PER_DAY; |
| 635 |
break; |
| 636 |
case HOUR_TO_MINUTE: |
| 637 |
bound = 60; |
| 638 |
break; |
| 639 |
case HOUR_TO_SECOND: |
| 640 |
bound = NANOS_PER_HOUR; |
| 641 |
break; |
| 642 |
case MINUTE_TO_SECOND: |
| 643 |
bound = NANOS_PER_MINUTE; |
| 644 |
break; |
| 645 |
default: |
| 646 |
throw DbException.getInvalidValueException("interval", qualifier); |
| 647 |
} |
| 648 |
if (leading < 0L || leading >= 1_000_000_000_000_000_000L) { |
| 649 |
throw DbException.getInvalidValueException("interval", Long.toString(leading)); |
| 650 |
} |
| 651 |
if (remaining < 0L || remaining >= bound) { |
| 652 |
throw DbException.getInvalidValueException("interval", Long.toString(remaining)); |
| 653 |
} |
| 654 |
return negative; |
| 655 |
} |
| 656 |
|
| 657 |
/** |
| 658 |
* Returns years value of interval, if any. |
| 659 |
* |
| 660 |
* @param qualifier |
| 661 |
* qualifier |
| 662 |
* @param negative |
| 663 |
* whether interval is negative |
| 664 |
* @param leading |
| 665 |
* value of leading field |
| 666 |
* @param remaining |
| 667 |
* values of all remaining fields |
| 668 |
* @return years, or 0 |
| 669 |
*/ |
| 670 |
public static long yearsFromInterval(IntervalQualifier qualifier, boolean negative, long leading, long remaining) { |
| 671 |
if (qualifier == IntervalQualifier.YEAR || qualifier == IntervalQualifier.YEAR_TO_MONTH) { |
| 672 |
long v = leading; |
| 673 |
if (negative) { |
| 674 |
v = -v; |
| 675 |
} |
| 676 |
return v; |
| 677 |
} else { |
| 678 |
return 0; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
/** |
| 683 |
* Returns months value of interval, if any. |
| 684 |
* |
| 685 |
* @param qualifier |
| 686 |
* qualifier |
| 687 |
* @param negative |
| 688 |
* whether interval is negative |
| 689 |
* @param leading |
| 690 |
* value of leading field |
| 691 |
* @param remaining |
| 692 |
* values of all remaining fields |
| 693 |
* @return months, or 0 |
| 694 |
*/ |
| 695 |
public static long monthsFromInterval(IntervalQualifier qualifier, boolean negative, long leading, // |
| 696 |
long remaining) { |
| 697 |
long v; |
| 698 |
if (qualifier == IntervalQualifier.MONTH) { |
| 699 |
v = leading; |
| 700 |
} else if (qualifier == IntervalQualifier.YEAR_TO_MONTH) { |
| 701 |
v = remaining; |
| 702 |
} else { |
| 703 |
return 0; |
| 704 |
} |
| 705 |
if (negative) { |
| 706 |
v = -v; |
| 707 |
} |
| 708 |
return v; |
| 709 |
} |
| 710 |
|
| 711 |
/** |
| 712 |
* Returns days value of interval, if any. |
| 713 |
* |
| 714 |
* @param qualifier |
| 715 |
* qualifier |
| 716 |
* @param negative |
| 717 |
* whether interval is negative |
| 718 |
* @param leading |
| 719 |
* value of leading field |
| 720 |
* @param remaining |
| 721 |
* values of all remaining fields |
| 722 |
* @return days, or 0 |
| 723 |
*/ |
| 724 |
public static long daysFromInterval(IntervalQualifier qualifier, boolean negative, long leading, long remaining) { |
| 725 |
switch (qualifier) { |
| 726 |
case DAY: |
| 727 |
case DAY_TO_HOUR: |
| 728 |
case DAY_TO_MINUTE: |
| 729 |
case DAY_TO_SECOND: |
| 730 |
long v = leading; |
| 731 |
if (negative) { |
| 732 |
v = -v; |
| 733 |
} |
| 734 |
return v; |
| 735 |
default: |
| 736 |
return 0; |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
/** |
| 741 |
* Returns hours value of interval, if any. |
| 742 |
* |
| 743 |
* @param qualifier |
| 744 |
* qualifier |
| 745 |
* @param negative |
| 746 |
* whether interval is negative |
| 747 |
* @param leading |
| 748 |
* value of leading field |
| 749 |
* @param remaining |
| 750 |
* values of all remaining fields |
| 751 |
* @return hours, or 0 |
| 752 |
*/ |
| 753 |
public static long hoursFromInterval(IntervalQualifier qualifier, boolean negative, long leading, long remaining) { |
| 754 |
long v; |
| 755 |
switch (qualifier) { |
| 756 |
case HOUR: |
| 757 |
case HOUR_TO_MINUTE: |
| 758 |
case HOUR_TO_SECOND: |
| 759 |
v = leading; |
| 760 |
break; |
| 761 |
case DAY_TO_HOUR: |
| 762 |
v = remaining; |
| 763 |
break; |
| 764 |
case DAY_TO_MINUTE: |
| 765 |
v = remaining / 60; |
| 766 |
break; |
| 767 |
case DAY_TO_SECOND: |
| 768 |
v = remaining / NANOS_PER_HOUR; |
| 769 |
break; |
| 770 |
default: |
| 771 |
return 0; |
| 772 |
} |
| 773 |
if (negative) { |
| 774 |
v = -v; |
| 775 |
} |
| 776 |
return v; |
| 777 |
} |
| 778 |
|
| 779 |
/** |
| 780 |
* Returns minutes value of interval, if any. |
| 781 |
* |
| 782 |
* @param qualifier |
| 783 |
* qualifier |
| 784 |
* @param negative |
| 785 |
* whether interval is negative |
| 786 |
* @param leading |
| 787 |
* value of leading field |
| 788 |
* @param remaining |
| 789 |
* values of all remaining fields |
| 790 |
* @return minutes, or 0 |
| 791 |
*/ |
| 792 |
public static long minutesFromInterval(IntervalQualifier qualifier, boolean negative, long leading, |
| 793 |
long remaining) { |
| 794 |
long v; |
| 795 |
switch (qualifier) { |
| 796 |
case MINUTE: |
| 797 |
case MINUTE_TO_SECOND: |
| 798 |
v = leading; |
| 799 |
break; |
| 800 |
case DAY_TO_MINUTE: |
| 801 |
v = remaining % 60; |
| 802 |
break; |
| 803 |
case DAY_TO_SECOND: |
| 804 |
v = remaining / NANOS_PER_MINUTE % 60; |
| 805 |
break; |
| 806 |
case HOUR_TO_MINUTE: |
| 807 |
v = remaining; |
| 808 |
break; |
| 809 |
case HOUR_TO_SECOND: |
| 810 |
v = remaining / NANOS_PER_MINUTE; |
| 811 |
break; |
| 812 |
default: |
| 813 |
return 0; |
| 814 |
} |
| 815 |
if (negative) { |
| 816 |
v = -v; |
| 817 |
} |
| 818 |
return v; |
| 819 |
} |
| 820 |
|
| 821 |
/** |
| 822 |
* Returns nanoseconds value of interval, if any. |
| 823 |
* |
| 824 |
* @param qualifier |
| 825 |
* qualifier |
| 826 |
* @param negative |
| 827 |
* whether interval is negative |
| 828 |
* @param leading |
| 829 |
* value of leading field |
| 830 |
* @param remaining |
| 831 |
* values of all remaining fields |
| 832 |
* @return nanoseconds, or 0 |
| 833 |
*/ |
| 834 |
public static long nanosFromInterval(IntervalQualifier qualifier, boolean negative, long leading, long remaining) { |
| 835 |
long v; |
| 836 |
switch (qualifier) { |
| 837 |
case SECOND: |
| 838 |
v = leading * NANOS_PER_SECOND + remaining; |
| 839 |
break; |
| 840 |
case DAY_TO_SECOND: |
| 841 |
case HOUR_TO_SECOND: |
| 842 |
v = remaining % NANOS_PER_MINUTE; |
| 843 |
break; |
| 844 |
case MINUTE_TO_SECOND: |
| 845 |
v = remaining; |
| 846 |
break; |
| 847 |
default: |
| 848 |
return 0; |
| 849 |
} |
| 850 |
if (negative) { |
| 851 |
v = -v; |
| 852 |
} |
| 853 |
return v; |
| 854 |
} |
| 855 |
|
| 856 |
} |