Skip to content

Instantiate constants lazily so unused constants are not instantiated. #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import org.threeten.bp.zone.ZoneOffsetTransitionRule.TimeDefinition
object TzdbZoneRulesCompiler {

/** Time parser. */
private val TIME_PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
private lazy val TIME_PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
.appendValue(HOUR_OF_DAY)
.optionalStart()
.appendLiteral(':')
Expand Down
18 changes: 9 additions & 9 deletions core/shared/src/main/scala/org/threeten/bp/DayOfWeek.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,39 @@ object DayOfWeek {
/** The singleton instance for the day-of-week of Monday.
* This has the numeric value of {@code 1}.
*/
val MONDAY = new DayOfWeek("MONDAY", 0)
lazy val MONDAY = new DayOfWeek("MONDAY", 0)

/** The singleton instance for the day-of-week of Tuesday.
* This has the numeric value of {@code 2}.
*/
val TUESDAY = new DayOfWeek("TUESDAY", 1)
lazy val TUESDAY = new DayOfWeek("TUESDAY", 1)

/** The singleton instance for the day-of-week of Wednesday.
* This has the numeric value of {@code 3}.
*/
val WEDNESDAY = new DayOfWeek("WEDNESDAY", 2)
lazy val WEDNESDAY = new DayOfWeek("WEDNESDAY", 2)

/** The singleton instance for the day-of-week of Thursday.
* This has the numeric value of {@code 4}.
*/
val THURSDAY = new DayOfWeek("THURSDAY", 3)
lazy val THURSDAY = new DayOfWeek("THURSDAY", 3)

/** The singleton instance for the day-of-week of Friday.
* This has the numeric value of {@code 5}.
*/
val FRIDAY = new DayOfWeek("FRIDAY", 4)
lazy val FRIDAY = new DayOfWeek("FRIDAY", 4)

/** The singleton instance for the day-of-week of Saturday.
* This has the numeric value of {@code 6}.
*/
val SATURDAY = new DayOfWeek("SATURDAY", 5)
lazy val SATURDAY = new DayOfWeek("SATURDAY", 5)

/** The singleton instance for the day-of-week of Sunday.
* This has the numeric value of {@code 7}.
*/
val SUNDAY = new DayOfWeek("SUNDAY", 6)
lazy val SUNDAY = new DayOfWeek("SUNDAY", 6)

val values: Array[DayOfWeek] =
lazy val values: Array[DayOfWeek] =
Array(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY)
def valueOf(enum: String): DayOfWeek = values.find(_.name() == enum) match {
case Some(dayOfWeek) => dayOfWeek
Expand All @@ -118,7 +118,7 @@ object DayOfWeek {

/** Private cache of all the constants.
*/
private val ENUMS: Array[DayOfWeek] = DayOfWeek.values
private lazy val ENUMS: Array[DayOfWeek] = DayOfWeek.values

/** Obtains an instance of {@code DayOfWeek} from an {@code int} value.
*
Expand Down
6 changes: 3 additions & 3 deletions core/shared/src/main/scala/org/threeten/bp/Duration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import org.threeten.bp.temporal.UnsupportedTemporalTypeException
object Duration {

/** Constant for a duration of zero. */
val ZERO: Duration = new Duration(0, 0)
lazy val ZERO: Duration = new Duration(0, 0)

/** Constant for nanos per second. */
private val NANOS_PER_SECOND: Int = 1000000000
Expand All @@ -73,10 +73,10 @@ object Duration {
private val NANOS_PER_MILLI: Int = 1000000

/** Constant for nanos per second. */
private val BI_NANOS_PER_SECOND: BigInteger = BigInteger.valueOf(NANOS_PER_SECOND.toLong)
private lazy val BI_NANOS_PER_SECOND: BigInteger = BigInteger.valueOf(NANOS_PER_SECOND.toLong)

/** The pattern for parsing. */
private val PATTERN: Pattern = Pattern.compile(
private lazy val PATTERN: Pattern = Pattern.compile(
"([-+]?)P(?:([-+]?[0-9]+)D)?" + "(T(?:([-+]?[0-9]+)H)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)(?:[.,]([0-9]{0,9}))?S)?)?",
Pattern.CASE_INSENSITIVE
)
Expand Down
6 changes: 3 additions & 3 deletions core/shared/src/main/scala/org/threeten/bp/Instant.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import org.threeten.bp.temporal.ValueRange
object Instant {

/** Constant for the 1970-01-01T00:00:00Z epoch instant. */
val EPOCH: Instant = new Instant(0, 0)
lazy val EPOCH: Instant = new Instant(0, 0)

/** The minimum supported epoch second. */
private val MIN_SECOND: Long = -31557014167219200L
Expand All @@ -92,7 +92,7 @@ object Instant {
* The value is also chosen such that the value of the year fits in
* an {@code int}.
*/
val MIN: Instant = Instant.ofEpochSecond(MIN_SECOND, 0)
lazy val MIN: Instant = Instant.ofEpochSecond(MIN_SECOND, 0)

/** The maximum supported {@code Instant}, '1000000000-12-31T23:59:59.999999999Z'.
* This could be used by an application as a "far future" instant.
Expand All @@ -103,7 +103,7 @@ object Instant {
* The value is also chosen such that the value of the year fits in
* an {@code int}.
*/
val MAX: Instant = Instant.ofEpochSecond(MAX_SECOND, 999999999)
lazy val MAX: Instant = Instant.ofEpochSecond(MAX_SECOND, 999999999)

/** Obtains the current instant from the system clock.
*
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/org/threeten/bp/LocalDate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ object LocalDate {
/** The minimum supported {@code LocalDate}, '-999999999-01-01'.
* This could be used by an application as a "far past" date.
*/
val MIN: LocalDate = LocalDate.of(Year.MIN_VALUE, 1, 1)
lazy val MIN: LocalDate = LocalDate.of(Year.MIN_VALUE, 1, 1)

/** The maximum supported {@code LocalDate}, '+999999999-12-31'.
* This could be used by an application as a "far future" date.
*/
val MAX: LocalDate = LocalDate.of(Year.MAX_VALUE, 12, 31)
lazy val MAX: LocalDate = LocalDate.of(Year.MAX_VALUE, 12, 31)

/** The number of days in a 400 year cycle. */
private val DAYS_PER_CYCLE: Int = 146097
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ object LocalDateTime {
* This combines {@link LocalDate#MIN} and {@link LocalTime#MIN}.
* This could be used by an application as a "far past" date-time.
*/
val MIN: LocalDateTime = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN)
lazy val MIN: LocalDateTime = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN)

/** The maximum supported {@code LocalDateTime}, '+999999999-12-31T23:59:59.999999999'.
* This is the local date-time just before midnight at the end of the maximum date.
* This combines {@link LocalDate#MAX} and {@link LocalTime#MAX}.
* This could be used by an application as a "far future" date-time.
*/
val MAX: LocalDateTime = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX)
lazy val MAX: LocalDateTime = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX)

/** Obtains the current date-time from the system clock in the default time-zone.
*
Expand Down
10 changes: 5 additions & 5 deletions core/shared/src/main/scala/org/threeten/bp/LocalTime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import org.threeten.bp.temporal.ValueRange
object LocalTime {

/** Constants for the local time of each hour. */
private val HOURS: Array[LocalTime] = {
private lazy val HOURS: Array[LocalTime] = {
val hours = new Array[LocalTime](24)
var i: Int = 0
while (i < hours.length) {
Expand All @@ -77,18 +77,18 @@ object LocalTime {
/** The minimum supported {@code LocalTime}, '00:00'.
* This is the time of midnight at the start of the day.
*/
val MIN: LocalTime = HOURS(0)
lazy val MIN: LocalTime = HOURS(0)

/** The maximum supported {@code LocalTime}, '23:59:59.999999999'.
* This is the time just before midnight at the end of the day.
*/
val MAX: LocalTime = new LocalTime(23, 59, 59, 999999999)
lazy val MAX: LocalTime = new LocalTime(23, 59, 59, 999999999)

/** The time of midnight at the start of the day, '00:00'. */
val MIDNIGHT: LocalTime = HOURS(0)
lazy val MIDNIGHT: LocalTime = HOURS(0)

/** The time of noon in the middle of the day, '12:00'. */
val NOON: LocalTime = HOURS(12)
lazy val NOON: LocalTime = HOURS(12)

/** Hours per day. */
private[bp] val HOURS_PER_DAY: Int = 24
Expand Down
50 changes: 25 additions & 25 deletions core/shared/src/main/scala/org/threeten/bp/Month.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,52 +75,52 @@ object Month {
/** The singleton instance for the month of January with 31 days.
* This has the numeric value of {@code 1}.
*/
val JANUARY = new Month("JANUARY", 0)
lazy val JANUARY = new Month("JANUARY", 0)

/** The singleton instance for the month of February with 28 days, or 29 in a leap year.
* This has the numeric value of {@code 2}.
*/
val FEBRUARY = new Month("FEBRUARY", 1)
lazy val FEBRUARY = new Month("FEBRUARY", 1)

/** The singleton instance for the month of March with 31 days.
* This has the numeric value of {@code 3}.
*/
val MARCH = new Month("MARCH", 2)
lazy val MARCH = new Month("MARCH", 2)

/** The singleton instance for the month of April with 30 days.
* This has the numeric value of {@code 4}.
*/
val APRIL = new Month("APRIL", 3)
lazy val APRIL = new Month("APRIL", 3)

/** The singleton instance for the month of May with 31 days.
* This has the numeric value of {@code 5}.
*/
val MAY = new Month("MAY", 4)
lazy val MAY = new Month("MAY", 4)

/** The singleton instance for the month of June with 30 days.
* This has the numeric value of {@code 6}.
*/
val JUNE = new Month("JUNE", 5)
lazy val JUNE = new Month("JUNE", 5)

/** The singleton instance for the month of July with 31 days.
* This has the numeric value of {@code 7}.
*/
val JULY = new Month("JULY", 6)
lazy val JULY = new Month("JULY", 6)

/** The singleton instance for the month of August with 31 days.
* This has the numeric value of {@code 8}.
*/
val AUGUST = new Month("AUGUST", 7)
lazy val AUGUST = new Month("AUGUST", 7)

/** The singleton instance for the month of September with 30 days.
* This has the numeric value of {@code 9}.
*/
val SEPTEMBER = new Month("SEPTEMBER", 8)
lazy val SEPTEMBER = new Month("SEPTEMBER", 8)

/** The singleton instance for the month of October with 31 days.
* This has the numeric value of {@code 10}.
*/
val OCTOBER = new Month("OCTOBER", 9)
lazy val OCTOBER = new Month("OCTOBER", 9)

/** The singleton instance for the month of November with 30 days.
* This has the numeric value of {@code 11}.
Expand All @@ -130,28 +130,28 @@ object Month {
/** The singleton instance for the month of December with 31 days.
* This has the numeric value of {@code 12}.
*/
val DECEMBER = new Month("DECEMBER", 11)

val values: Array[Month] = Array(JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER)
lazy val DECEMBER = new Month("DECEMBER", 11)

lazy val values: Array[Month] = Array(JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER)
def valueOf(enum: String): Month = values.find(_.name() == enum) match {
case Some(month) => month
case _ => throw new IllegalArgumentException(s"Unrecognized month name: $enum")
}

/** Private cache of all the constants.
*/
private val ENUMS: Array[Month] = Month.values
private lazy val ENUMS: Array[Month] = Month.values

/** Obtains an instance of {@code Month} from an {@code int} value.
*
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/org/threeten/bp/MonthDay.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import org.threeten.bp.temporal.ValueRange
object MonthDay {

/** Parser. */
private val PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
private lazy val PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
.appendLiteral("--")
.appendValue(MONTH_OF_YEAR, 2)
.appendLiteral('-')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ object OffsetDateTime {
* This combines {@link LocalDateTime#MIN} and {@link ZoneOffset#MAX}.
* This could be used by an application as a "far past" date-time.
*/
val MIN: OffsetDateTime = LocalDateTime.MIN.atOffset(ZoneOffset.MAX)
lazy val MIN: OffsetDateTime = LocalDateTime.MIN.atOffset(ZoneOffset.MAX)

/** The maximum supported {@code OffsetDateTime}, '+999999999-12-31T23:59:59.999999999-18:00'.
* This is the local date-time just before midnight at the end of the maximum date
* in the minimum offset (larger negative offsets are later on the time-line).
* This combines {@link LocalDateTime#MAX} and {@link ZoneOffset#MIN}.
* This could be used by an application as a "far future" date-time.
*/
val MAX: OffsetDateTime = LocalDateTime.MAX.atOffset(ZoneOffset.MIN)
lazy val MAX: OffsetDateTime = LocalDateTime.MAX.atOffset(ZoneOffset.MIN)

/** Gets a comparator that compares two {@code OffsetDateTime} instances
* based solely on the instant.
Expand All @@ -91,7 +91,7 @@ object OffsetDateTime {
*/
def timeLineOrder: Comparator[OffsetDateTime] = INSTANT_COMPARATOR

private val INSTANT_COMPARATOR: Comparator[OffsetDateTime] =
private lazy val INSTANT_COMPARATOR: Comparator[OffsetDateTime] =
new Comparator[OffsetDateTime] {
override def compare(datetime1: OffsetDateTime, datetime2: OffsetDateTime): Int = {
var cmp: Int = java.lang.Long.compare(datetime1.toEpochSecond, datetime2.toEpochSecond)
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/org/threeten/bp/OffsetTime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ object OffsetTime {
* This combines {@link LocalTime#MIN} and {@link ZoneOffset#MAX}.
* This could be used by an application as a "far past" date.
*/
val MIN: OffsetTime = LocalTime.MIN.atOffset(ZoneOffset.MAX)
lazy val MIN: OffsetTime = LocalTime.MIN.atOffset(ZoneOffset.MAX)

/** The maximum supported {@code OffsetTime}, '23:59:59.999999999-18:00'.
* This is the time just before midnight at the end of the day in the minimum offset
* (larger negative offsets are later on the time-line).
* This combines {@link LocalTime#MAX} and {@link ZoneOffset#MIN}.
* This could be used by an application as a "far future" date.
*/
val MAX: OffsetTime = LocalTime.MAX.atOffset(ZoneOffset.MIN)
lazy val MAX: OffsetTime = LocalTime.MAX.atOffset(ZoneOffset.MIN)

/** Obtains the current time from the system clock in the default time-zone.
*
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/org/threeten/bp/Period.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ import org.threeten.bp.temporal.UnsupportedTemporalTypeException
object Period {

/** A constant for a period of zero. */
val ZERO: Period = new Period(0, 0, 0)
lazy val ZERO: Period = new Period(0, 0, 0)

/** The pattern for parsing. */
private val PATTERN: Pattern = Pattern.compile(
private lazy val PATTERN: Pattern = Pattern.compile(
"([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?",
Pattern.CASE_INSENSITIVE
)
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/org/threeten/bp/Year.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ import org.threeten.bp.temporal.ValueRange
object Year {

/** The minimum supported year, '-999,999,999'. */
val MIN_VALUE: Int = -999999999
lazy val MIN_VALUE: Int = -999999999

/** The maximum supported year, '+999,999,999'. */
val MAX_VALUE: Int = 999999999
lazy val MAX_VALUE: Int = 999999999

/** Parser. */
private lazy val PARSER: DateTimeFormatter =
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/org/threeten/bp/YearMonth.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ import org.threeten.bp.temporal.ValueRange
object YearMonth {

/** Parser. */
private val PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
private lazy val PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 2)
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/org/threeten/bp/ZoneId.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ object ZoneId {
* </ul><p>
* The map is unmodifiable.
*/
val SHORT_IDS: java.util.Map[String, String] = {
lazy val SHORT_IDS: java.util.Map[String, String] = {
val base = Map[String, String](
"ACT" -> "Australia/Darwin",
"AET" -> "Australia/Sydney",
Expand Down
Loading