Skip to content

Commit 630d279

Browse files
committed
RIF Add null checks where appropriate
1 parent e696f2b commit 630d279

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

shared/src/main/scala/org/threeten/bp/Instant.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,10 @@ final class Instant private(private val seconds: Long, private val nanos: Int) e
447447
* @throws DateTimeException if a value for the field cannot be obtained
448448
* @throws ArithmeticException if numeric overflow occurs
449449
*/
450-
def getLong(field: TemporalField): Long =
450+
def getLong(field: TemporalField): Long = {
451+
if (field == null) {
452+
throw new NullPointerException()
453+
}
451454
if (field.isInstanceOf[ChronoField])
452455
field.asInstanceOf[ChronoField] match {
453456
case NANO_OF_SECOND => nanos
@@ -458,6 +461,7 @@ final class Instant private(private val seconds: Long, private val nanos: Int) e
458461
}
459462
else
460463
field.getFrom(this)
464+
}
461465

462466
/** Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
463467
*
@@ -765,13 +769,17 @@ final class Instant private(private val seconds: Long, private val nanos: Int) e
765769
* @throws DateTimeException if unable to query (defined by the query)
766770
* @throws ArithmeticException if numeric overflow occurs (defined by the query)
767771
*/
768-
override def query[R >: Null](query: TemporalQuery[R]): R =
772+
override def query[R >: Null](query: TemporalQuery[R]): R = {
773+
if (query == null) {
774+
throw new NullPointerException()
775+
}
769776
if (query eq TemporalQueries.precision)
770777
NANOS.asInstanceOf[R]
771778
else if ((query eq TemporalQueries.localDate) || (query eq TemporalQueries.localTime) || (query eq TemporalQueries.chronology) || (query eq TemporalQueries.zoneId) || (query eq TemporalQueries.zone) || (query eq TemporalQueries.offset))
772779
null
773780
else
774781
query.queryFrom(this)
782+
}
775783

776784
/** Adjusts the specified temporal object to have this instant.
777785
*
@@ -943,6 +951,9 @@ final class Instant private(private val seconds: Long, private val nanos: Int) e
943951
* @throws NullPointerException if otherInstant is null
944952
*/
945953
def compare(otherInstant: Instant): Int = {
954+
if (otherInstant == null) {
955+
throw new NullPointerException("null object in comparison")
956+
}
946957
val cmp: Int = java.lang.Long.compare(seconds, otherInstant.seconds)
947958
if (cmp != 0) cmp
948959
else nanos - otherInstant.nanos

shared/src/main/scala/org/threeten/bp/temporal/TemporalAccessor.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,16 @@ trait TemporalAccessor {
114114
* @return the range of valid values for the field, not null
115115
* @throws DateTimeException if the range for the field cannot be obtained
116116
*/
117-
def range(field: TemporalField): ValueRange =
117+
def range(field: TemporalField): ValueRange = {
118+
if (field == null) {
119+
throw new NullPointerException()
120+
}
118121
if (field.isInstanceOf[ChronoField])
119122
if (isSupported(field)) field.range
120123
else throw new UnsupportedTemporalTypeException(s"Unsupported field: $field")
121124
else
122125
field.rangeRefinedBy(this)
126+
}
123127

124128
/** Gets the value of the specified field as an {@code int}.
125129
*

0 commit comments

Comments
 (0)