| 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.api; |
| 7 |
|
| 8 |
import static org.h2.util.DateTimeUtils.NANOS_PER_MINUTE; |
| 9 |
import static org.h2.util.DateTimeUtils.NANOS_PER_SECOND; |
| 10 |
|
| 11 |
import org.h2.message.DbException; |
| 12 |
import org.h2.util.IntervalUtils; |
| 13 |
|
| 14 |
/** |
| 15 |
* INTERVAL representation for result sets. |
| 16 |
*/ |
| 17 |
public final class Interval { |
| 18 |
|
| 19 |
private final IntervalQualifier qualifier; |
| 20 |
|
| 21 |
/** |
| 22 |
* {@code false} for zero or positive intervals, {@code true} for negative |
| 23 |
* intervals. |
| 24 |
*/ |
| 25 |
private final boolean negative; |
| 26 |
|
| 27 |
/** |
| 28 |
* Non-negative long with value of leading field. For INTERVAL SECOND |
| 29 |
* contains only integer part of seconds. |
| 30 |
*/ |
| 31 |
private final long leading; |
| 32 |
|
| 33 |
/** |
| 34 |
* Non-negative long with combined value of all remaining field, or 0 for |
| 35 |
* single-field intervals, with exception for INTERVAL SECOND that uses this |
| 36 |
* field to store fractional part of seconds measured in nanoseconds. |
| 37 |
*/ |
| 38 |
private final long remaining; |
| 39 |
|
| 40 |
/** |
| 41 |
* Creates a new INTERVAL YEAR. |
| 42 |
* |
| 43 |
* @param years |
| 44 |
* years, |years|<10<sup>18</sup> |
| 45 |
* @return INTERVAL YEAR |
| 46 |
*/ |
| 47 |
public static Interval ofYears(long years) { |
| 48 |
return new Interval(IntervalQualifier.YEAR, years < 0, Math.abs(years), 0); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Creates a new INTERVAL MONTH. |
| 53 |
* |
| 54 |
* @param months |
| 55 |
* months, |months|<10<sup>18</sup> |
| 56 |
* @return INTERVAL MONTH |
| 57 |
*/ |
| 58 |
public static Interval ofMonths(long months) { |
| 59 |
return new Interval(IntervalQualifier.MONTH, months < 0, Math.abs(months), 0); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Creates a new INTERVAL DAY. |
| 64 |
* |
| 65 |
* @param days |
| 66 |
* days, |days|<10<sup>18</sup> |
| 67 |
* @return INTERVAL DAY |
| 68 |
*/ |
| 69 |
public static Interval ofDays(long days) { |
| 70 |
return new Interval(IntervalQualifier.DAY, days < 0, Math.abs(days), 0); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Creates a new INTERVAL HOUR. |
| 75 |
* |
| 76 |
* @param hours |
| 77 |
* hours, |hours|<10<sup>18</sup> |
| 78 |
* @return INTERVAL HOUR |
| 79 |
*/ |
| 80 |
public static Interval ofHours(long hours) { |
| 81 |
return new Interval(IntervalQualifier.HOUR, hours < 0, Math.abs(hours), 0); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Creates a new INTERVAL MINUTE. |
| 86 |
* |
| 87 |
* @param minutes |
| 88 |
* minutes, |minutes|<10<sup>18</sup> |
| 89 |
* @return interval |
| 90 |
*/ |
| 91 |
public static Interval ofMinutes(long minutes) { |
| 92 |
return new Interval(IntervalQualifier.MINUTE, minutes < 0, Math.abs(minutes), 0); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Creates a new INTERVAL SECOND. |
| 97 |
* |
| 98 |
* @param seconds |
| 99 |
* seconds, |seconds|<10<sup>18</sup> |
| 100 |
* @return INTERVAL SECOND |
| 101 |
*/ |
| 102 |
public static Interval ofSeconds(long seconds) { |
| 103 |
return new Interval(IntervalQualifier.SECOND, seconds < 0, Math.abs(seconds), 0); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Creates a new INTERVAL SECOND. |
| 108 |
* |
| 109 |
* <p> |
| 110 |
* If both arguments are not equal to zero they should have the same sign. |
| 111 |
* </p> |
| 112 |
* |
| 113 |
* @param seconds |
| 114 |
* seconds, |seconds|<10<sup>18</sup> |
| 115 |
* @param nanos |
| 116 |
* nanoseconds, |nanos|<1,000,000,000 |
| 117 |
* @return INTERVAL SECOND |
| 118 |
*/ |
| 119 |
public static Interval ofSeconds(long seconds, int nanos) { |
| 120 |
// Interval is negative if any field is negative |
| 121 |
boolean negative = (seconds | nanos) < 0; |
| 122 |
if (negative) { |
| 123 |
// Ensure that all fields are negative or zero |
| 124 |
if (seconds > 0 || nanos > 0) { |
| 125 |
throw new IllegalArgumentException(); |
| 126 |
} |
| 127 |
// Make them positive |
| 128 |
seconds = -seconds; |
| 129 |
nanos = -nanos; |
| 130 |
// Long.MIN_VALUE and Integer.MIN_VALUE will be rejected by |
| 131 |
// constructor |
| 132 |
} |
| 133 |
return new Interval(IntervalQualifier.SECOND, negative, seconds, nanos); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Creates a new INTERVAL SECOND. |
| 138 |
* |
| 139 |
* @param nanos |
| 140 |
* nanoseconds (including seconds) |
| 141 |
* @return INTERVAL SECOND |
| 142 |
*/ |
| 143 |
public static Interval ofNanos(long nanos) { |
| 144 |
boolean negative = nanos < 0; |
| 145 |
if (negative) { |
| 146 |
nanos = -nanos; |
| 147 |
if (nanos < 0) { |
| 148 |
// Long.MIN_VALUE = -9_223_372_036_854_775_808L |
| 149 |
return new Interval(IntervalQualifier.SECOND, true, 9_223_372_036L, 854_775_808); |
| 150 |
} |
| 151 |
} |
| 152 |
return new Interval(IntervalQualifier.SECOND, negative, nanos / NANOS_PER_SECOND, nanos % NANOS_PER_SECOND); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Creates a new INTERVAL YEAR TO MONTH. |
| 157 |
* |
| 158 |
* <p> |
| 159 |
* If both arguments are not equal to zero they should have the same sign. |
| 160 |
* </p> |
| 161 |
* |
| 162 |
* @param years |
| 163 |
* years, |years|<10<sup>18</sup> |
| 164 |
* @param months |
| 165 |
* months, |months|<12 |
| 166 |
* @return INTERVAL YEAR TO MONTH |
| 167 |
*/ |
| 168 |
public static Interval ofYearsMonths(long years, int months) { |
| 169 |
// Interval is negative if any field is negative |
| 170 |
boolean negative = (years | months) < 0; |
| 171 |
if (negative) { |
| 172 |
// Ensure that all fields are negative or zero |
| 173 |
if (years > 0 || months > 0) { |
| 174 |
throw new IllegalArgumentException(); |
| 175 |
} |
| 176 |
// Make them positive |
| 177 |
years = -years; |
| 178 |
months = -months; |
| 179 |
// Long.MIN_VALUE and Integer.MIN_VALUE will be rejected by |
| 180 |
// constructor |
| 181 |
} |
| 182 |
return new Interval(IntervalQualifier.YEAR_TO_MONTH, negative, years, months); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Creates a new INTERVAL DAY TO HOUR. |
| 187 |
* |
| 188 |
* <p> |
| 189 |
* If both arguments are not equal to zero they should have the same sign. |
| 190 |
* </p> |
| 191 |
* |
| 192 |
* @param days |
| 193 |
* days, |days|<10<sup>18</sup> |
| 194 |
* @param hours |
| 195 |
* hours, |hours|<24 |
| 196 |
* @return INTERVAL DAY TO HOUR |
| 197 |
*/ |
| 198 |
public static Interval ofDaysHours(long days, int hours) { |
| 199 |
// Interval is negative if any field is negative |
| 200 |
boolean negative = (days | hours) < 0; |
| 201 |
if (negative) { |
| 202 |
// Ensure that all fields are negative or zero |
| 203 |
if (days > 0 || hours > 0) { |
| 204 |
throw new IllegalArgumentException(); |
| 205 |
} |
| 206 |
// Make them positive |
| 207 |
days = -days; |
| 208 |
hours = -hours; |
| 209 |
// Long.MIN_VALUE and Integer.MIN_VALUE will be rejected by |
| 210 |
// constructor |
| 211 |
} |
| 212 |
return new Interval(IntervalQualifier.DAY_TO_HOUR, negative, days, hours); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Creates a new INTERVAL DAY TO MINUTE. |
| 217 |
* |
| 218 |
* <p> |
| 219 |
* Non-zero arguments should have the same sign. |
| 220 |
* </p> |
| 221 |
* |
| 222 |
* @param days |
| 223 |
* days, |days|<10<sup>18</sup> |
| 224 |
* @param hours |
| 225 |
* hours, |hours|<24 |
| 226 |
* @param minutes |
| 227 |
* minutes, |minutes|<60 |
| 228 |
* @return INTERVAL DAY TO MINUTE |
| 229 |
*/ |
| 230 |
public static Interval ofDaysHoursMinutes(long days, int hours, int minutes) { |
| 231 |
// Interval is negative if any field is negative |
| 232 |
boolean negative = (days | hours | minutes) < 0; |
| 233 |
if (negative) { |
| 234 |
// Ensure that all fields are negative or zero |
| 235 |
if (days > 0 || hours > 0 || minutes > 0) { |
| 236 |
throw new IllegalArgumentException(); |
| 237 |
} |
| 238 |
// Make them positive |
| 239 |
days = -days; |
| 240 |
hours = -hours; |
| 241 |
minutes = -minutes; |
| 242 |
if ((hours | minutes) < 0) { |
| 243 |
// Integer.MIN_VALUE |
| 244 |
throw new IllegalArgumentException(); |
| 245 |
} |
| 246 |
// days = Long.MIN_VALUE will be rejected by constructor |
| 247 |
} |
| 248 |
// Check only minutes. |
| 249 |
// Overflow in days or hours will be detected by constructor |
| 250 |
if (minutes >= 60) { |
| 251 |
throw new IllegalArgumentException(); |
| 252 |
} |
| 253 |
return new Interval(IntervalQualifier.DAY_TO_MINUTE, negative, days, hours * 60L + minutes); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Creates a new INTERVAL DAY TO SECOND. |
| 258 |
* |
| 259 |
* <p> |
| 260 |
* Non-zero arguments should have the same sign. |
| 261 |
* </p> |
| 262 |
* |
| 263 |
* @param days |
| 264 |
* days, |days|<10<sup>18</sup> |
| 265 |
* @param hours |
| 266 |
* hours, |hours|<24 |
| 267 |
* @param minutes |
| 268 |
* minutes, |minutes|<60 |
| 269 |
* @param seconds |
| 270 |
* seconds, |seconds|<60 |
| 271 |
* @return INTERVAL DAY TO SECOND |
| 272 |
*/ |
| 273 |
public static Interval ofDaysHoursMinutesSeconds(long days, int hours, int minutes, int seconds) { |
| 274 |
return ofDaysHoursMinutesNanos(days, hours, minutes, seconds * NANOS_PER_SECOND); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Creates a new INTERVAL DAY TO SECOND. |
| 279 |
* |
| 280 |
* <p> |
| 281 |
* Non-zero arguments should have the same sign. |
| 282 |
* </p> |
| 283 |
* |
| 284 |
* @param days |
| 285 |
* days, |days|<10<sup>18</sup> |
| 286 |
* @param hours |
| 287 |
* hours, |hours|<24 |
| 288 |
* @param minutes |
| 289 |
* minutes, |minutes|<60 |
| 290 |
* @param nanos |
| 291 |
* nanoseconds, |nanos|<60,000,000,000 |
| 292 |
* @return INTERVAL DAY TO SECOND |
| 293 |
*/ |
| 294 |
public static Interval ofDaysHoursMinutesNanos(long days, int hours, int minutes, long nanos) { |
| 295 |
// Interval is negative if any field is negative |
| 296 |
boolean negative = (days | hours | minutes | nanos) < 0; |
| 297 |
if (negative) { |
| 298 |
// Ensure that all fields are negative or zero |
| 299 |
if (days > 0 || hours > 0 || minutes > 0 || nanos > 0) { |
| 300 |
throw new IllegalArgumentException(); |
| 301 |
} |
| 302 |
// Make them positive |
| 303 |
days = -days; |
| 304 |
hours = -hours; |
| 305 |
minutes = -minutes; |
| 306 |
nanos = -nanos; |
| 307 |
if ((hours | minutes | nanos) < 0) { |
| 308 |
// Integer.MIN_VALUE, Long.MIN_VALUE |
| 309 |
throw new IllegalArgumentException(); |
| 310 |
} |
| 311 |
// days = Long.MIN_VALUE will be rejected by constructor |
| 312 |
} |
| 313 |
// Check only minutes and nanoseconds. |
| 314 |
// Overflow in days or hours will be detected by constructor |
| 315 |
if (minutes >= 60 || nanos >= NANOS_PER_MINUTE) { |
| 316 |
throw new IllegalArgumentException(); |
| 317 |
} |
| 318 |
return new Interval(IntervalQualifier.DAY_TO_SECOND, negative, days, |
| 319 |
(hours * 60L + minutes) * NANOS_PER_MINUTE + nanos); |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Creates a new INTERVAL HOUR TO MINUTE. |
| 324 |
* |
| 325 |
* <p> |
| 326 |
* If both arguments are not equal to zero they should have the same sign. |
| 327 |
* </p> |
| 328 |
* |
| 329 |
* @param hours |
| 330 |
* hours, |hours|<10<sup>18</sup> |
| 331 |
* @param minutes |
| 332 |
* minutes, |minutes|<60 |
| 333 |
* @return INTERVAL HOUR TO MINUTE |
| 334 |
*/ |
| 335 |
public static Interval ofHoursMinutes(long hours, int minutes) { |
| 336 |
// Interval is negative if any field is negative |
| 337 |
boolean negative = (hours | minutes) < 0; |
| 338 |
if (negative) { |
| 339 |
// Ensure that all fields are negative or zero |
| 340 |
if (hours > 0 || minutes > 0) { |
| 341 |
throw new IllegalArgumentException(); |
| 342 |
} |
| 343 |
// Make them positive |
| 344 |
hours = -hours; |
| 345 |
minutes = -minutes; |
| 346 |
// Long.MIN_VALUE and Integer.MIN_VALUE will be rejected by |
| 347 |
// constructor |
| 348 |
} |
| 349 |
return new Interval(IntervalQualifier.HOUR_TO_MINUTE, negative, hours, minutes); |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Creates a new INTERVAL HOUR TO SECOND. |
| 354 |
* |
| 355 |
* <p> |
| 356 |
* Non-zero arguments should have the same sign. |
| 357 |
* </p> |
| 358 |
* |
| 359 |
* @param hours |
| 360 |
* hours, |hours|<10<sup>18</sup> |
| 361 |
* @param minutes |
| 362 |
* minutes, |minutes|<60 |
| 363 |
* @param seconds |
| 364 |
* seconds, |seconds|<60 |
| 365 |
* @return INTERVAL HOUR TO SECOND |
| 366 |
*/ |
| 367 |
public static Interval ofHoursMinutesSeconds(long hours, int minutes, int seconds) { |
| 368 |
return ofHoursMinutesNanos(hours, minutes, seconds * NANOS_PER_SECOND); |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Creates a new INTERVAL HOUR TO SECOND. |
| 373 |
* |
| 374 |
* <p> |
| 375 |
* Non-zero arguments should have the same sign. |
| 376 |
* </p> |
| 377 |
* |
| 378 |
* @param hours |
| 379 |
* hours, |hours|<10<sup>18</sup> |
| 380 |
* @param minutes |
| 381 |
* minutes, |minutes|<60 |
| 382 |
* @param nanos |
| 383 |
* nanoseconds, |seconds|<60,000,000,000 |
| 384 |
* @return INTERVAL HOUR TO SECOND |
| 385 |
*/ |
| 386 |
public static Interval ofHoursMinutesNanos(long hours, int minutes, long nanos) { |
| 387 |
// Interval is negative if any field is negative |
| 388 |
boolean negative = (hours | minutes | nanos) < 0; |
| 389 |
if (negative) { |
| 390 |
// Ensure that all fields are negative or zero |
| 391 |
if (hours > 0 || minutes > 0 || nanos > 0) { |
| 392 |
throw new IllegalArgumentException(); |
| 393 |
} |
| 394 |
// Make them positive |
| 395 |
hours = -hours; |
| 396 |
minutes = -minutes; |
| 397 |
nanos = -nanos; |
| 398 |
if ((minutes | nanos) < 0) { |
| 399 |
// Integer.MIN_VALUE, Long.MIN_VALUE |
| 400 |
throw new IllegalArgumentException(); |
| 401 |
} |
| 402 |
// hours = Long.MIN_VALUE will be rejected by constructor |
| 403 |
} |
| 404 |
// Check only nanoseconds. |
| 405 |
// Overflow in hours or minutes will be detected by constructor |
| 406 |
if (nanos >= NANOS_PER_MINUTE) { |
| 407 |
throw new IllegalArgumentException(); |
| 408 |
} |
| 409 |
return new Interval(IntervalQualifier.HOUR_TO_SECOND, negative, hours, minutes * NANOS_PER_MINUTE + nanos); |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Creates a new INTERVAL MINUTE TO SECOND. |
| 414 |
* |
| 415 |
* <p> |
| 416 |
* If both arguments are not equal to zero they should have the same sign. |
| 417 |
* </p> |
| 418 |
* |
| 419 |
* @param minutes |
| 420 |
* minutes, |minutes|<10<sup>18</sup> |
| 421 |
* @param seconds |
| 422 |
* seconds, |seconds|<60 |
| 423 |
* @return INTERVAL MINUTE TO SECOND |
| 424 |
*/ |
| 425 |
public static Interval ofMinutesSeconds(long minutes, int seconds) { |
| 426 |
return ofMinutesNanos(minutes, seconds * NANOS_PER_SECOND); |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Creates a new INTERVAL MINUTE TO SECOND. |
| 431 |
* |
| 432 |
* <p> |
| 433 |
* If both arguments are not equal to zero they should have the same sign. |
| 434 |
* </p> |
| 435 |
* |
| 436 |
* @param minutes |
| 437 |
* minutes, |minutes|<10<sup>18</sup> |
| 438 |
* @param nanos |
| 439 |
* nanoseconds, |nanos|<60,000,000,000 |
| 440 |
* @return INTERVAL MINUTE TO SECOND |
| 441 |
*/ |
| 442 |
public static Interval ofMinutesNanos(long minutes, long nanos) { |
| 443 |
// Interval is negative if any field is negative |
| 444 |
boolean negative = (minutes | nanos) < 0; |
| 445 |
if (negative) { |
| 446 |
// Ensure that all fields are negative or zero |
| 447 |
if (minutes > 0 || nanos > 0) { |
| 448 |
throw new IllegalArgumentException(); |
| 449 |
} |
| 450 |
// Make them positive |
| 451 |
minutes = -minutes; |
| 452 |
nanos = -nanos; |
| 453 |
// Long.MIN_VALUE will be rejected by constructor |
| 454 |
} |
| 455 |
return new Interval(IntervalQualifier.MINUTE_TO_SECOND, negative, minutes, nanos); |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* Creates a new interval. Do not use this constructor, use static methods |
| 460 |
* instead. |
| 461 |
* |
| 462 |
* @param qualifier |
| 463 |
* qualifier |
| 464 |
* @param negative |
| 465 |
* whether interval is negative |
| 466 |
* @param leading |
| 467 |
* value of leading field |
| 468 |
* @param remaining |
| 469 |
* combined value of all remaining fields |
| 470 |
*/ |
| 471 |
public Interval(IntervalQualifier qualifier, boolean negative, long leading, long remaining) { |
| 472 |
this.qualifier = qualifier; |
| 473 |
try { |
| 474 |
this.negative = IntervalUtils.validateInterval(qualifier, negative, leading, remaining); |
| 475 |
} catch (DbException e) { |
| 476 |
throw new IllegalArgumentException(); |
| 477 |
} |
| 478 |
this.leading = leading; |
| 479 |
this.remaining = remaining; |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Returns qualifier of this interval. |
| 484 |
* |
| 485 |
* @return qualifier |
| 486 |
*/ |
| 487 |
public IntervalQualifier getQualifier() { |
| 488 |
return qualifier; |
| 489 |
} |
| 490 |
|
| 491 |
/** |
| 492 |
* Returns where the interval is negative. |
| 493 |
* |
| 494 |
* @return where the interval is negative |
| 495 |
*/ |
| 496 |
public boolean isNegative() { |
| 497 |
return negative; |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* Returns value of leading field of this interval. For {@code SECOND} |
| 502 |
* intervals returns integer part of seconds. |
| 503 |
* |
| 504 |
* @return value of leading field |
| 505 |
*/ |
| 506 |
public long getLeading() { |
| 507 |
return leading; |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* Returns combined value of remaining fields of this interval. For |
| 512 |
* {@code SECOND} intervals returns nanoseconds. |
| 513 |
* |
| 514 |
* @return combined value of remaining fields |
| 515 |
*/ |
| 516 |
public long getRemaining() { |
| 517 |
return remaining; |
| 518 |
} |
| 519 |
|
| 520 |
/** |
| 521 |
* Returns years value, if any. |
| 522 |
* |
| 523 |
* @return years, or 0 |
| 524 |
*/ |
| 525 |
public long getYears() { |
| 526 |
return IntervalUtils.yearsFromInterval(qualifier, negative, leading, remaining); |
| 527 |
} |
| 528 |
|
| 529 |
/** |
| 530 |
* Returns months value, if any. |
| 531 |
* |
| 532 |
* @return months, or 0 |
| 533 |
*/ |
| 534 |
public long getMonths() { |
| 535 |
return IntervalUtils.monthsFromInterval(qualifier, negative, leading, remaining); |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Returns days value, if any. |
| 540 |
* |
| 541 |
* @return days, or 0 |
| 542 |
*/ |
| 543 |
public long getDays() { |
| 544 |
return IntervalUtils.daysFromInterval(qualifier, negative, leading, remaining); |
| 545 |
} |
| 546 |
|
| 547 |
/** |
| 548 |
* Returns hours value, if any. |
| 549 |
* |
| 550 |
* @return hours, or 0 |
| 551 |
*/ |
| 552 |
public long getHours() { |
| 553 |
return IntervalUtils.hoursFromInterval(qualifier, negative, leading, remaining); |
| 554 |
} |
| 555 |
|
| 556 |
/** |
| 557 |
* Returns minutes value, if any. |
| 558 |
* |
| 559 |
* @return minutes, or 0 |
| 560 |
*/ |
| 561 |
public long getMinutes() { |
| 562 |
return IntervalUtils.minutesFromInterval(qualifier, negative, leading, remaining); |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* Returns value of integer part of seconds, if any. |
| 567 |
* |
| 568 |
* @return seconds, or 0 |
| 569 |
*/ |
| 570 |
public long getSeconds() { |
| 571 |
if (qualifier == IntervalQualifier.SECOND) { |
| 572 |
return negative ? -leading : leading; |
| 573 |
} |
| 574 |
return getSecondsAndNanos() / NANOS_PER_SECOND; |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Returns value of fractional part of seconds (in nanoseconds), if any. |
| 579 |
* |
| 580 |
* @return nanoseconds, or 0 |
| 581 |
*/ |
| 582 |
public long getNanosOfSecond() { |
| 583 |
if (qualifier == IntervalQualifier.SECOND) { |
| 584 |
return negative ? -remaining : remaining; |
| 585 |
} |
| 586 |
return getSecondsAndNanos() % NANOS_PER_SECOND; |
| 587 |
} |
| 588 |
|
| 589 |
/** |
| 590 |
* Returns seconds value measured in nanoseconds, if any. |
| 591 |
* |
| 592 |
* <p> |
| 593 |
* This method returns a long value that cannot fit all possible values of |
| 594 |
* INTERVAL SECOND. For a very large intervals of this type use |
| 595 |
* {@link #getSeconds()} and {@link #getNanosOfSecond()} instead. This |
| 596 |
* method can be safely used for intervals of other day-time types. |
| 597 |
* </p> |
| 598 |
* |
| 599 |
* @return nanoseconds (including seconds), or 0 |
| 600 |
*/ |
| 601 |
public long getSecondsAndNanos() { |
| 602 |
return IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining); |
| 603 |
} |
| 604 |
|
| 605 |
@Override |
| 606 |
public int hashCode() { |
| 607 |
final int prime = 31; |
| 608 |
int result = 1; |
| 609 |
result = prime * result + qualifier.hashCode(); |
| 610 |
result = prime * result + (negative ? 1231 : 1237); |
| 611 |
result = prime * result + (int) (leading ^ leading >>> 32); |
| 612 |
result = prime * result + (int) (remaining ^ remaining >>> 32); |
| 613 |
return result; |
| 614 |
} |
| 615 |
|
| 616 |
@Override |
| 617 |
public boolean equals(Object obj) { |
| 618 |
if (this == obj) { |
| 619 |
return true; |
| 620 |
} |
| 621 |
if (!(obj instanceof Interval)) { |
| 622 |
return false; |
| 623 |
} |
| 624 |
Interval other = (Interval) obj; |
| 625 |
return qualifier == other.qualifier && negative == other.negative && leading == other.leading |
| 626 |
&& remaining == other.remaining; |
| 627 |
} |
| 628 |
|
| 629 |
@Override |
| 630 |
public String toString() { |
| 631 |
return IntervalUtils.appendInterval(new StringBuilder(), getQualifier(), negative, leading, remaining) |
| 632 |
.toString(); |
| 633 |
} |
| 634 |
|
| 635 |
} |