Skip to content

Commit fde94d9

Browse files
authored
Merge pull request #131 from exoego/lazy-constants
Instantiate constants lazily so unused constants are not instantiated.
2 parents 9ad5406 + ae0924d commit fde94d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+288
-281
lines changed

core/jvm/src/main/scala/org/threeten/bp/zone/TzdbZoneRulesCompiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import org.threeten.bp.zone.ZoneOffsetTransitionRule.TimeDefinition
5555
object TzdbZoneRulesCompiler {
5656

5757
/** Time parser. */
58-
private val TIME_PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
58+
private lazy val TIME_PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
5959
.appendValue(HOUR_OF_DAY)
6060
.optionalStart()
6161
.appendLiteral(':')

core/shared/src/main/scala/org/threeten/bp/DayOfWeek.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,39 +76,39 @@ object DayOfWeek {
7676
/** The singleton instance for the day-of-week of Monday.
7777
* This has the numeric value of {@code 1}.
7878
*/
79-
val MONDAY = new DayOfWeek("MONDAY", 0)
79+
lazy val MONDAY = new DayOfWeek("MONDAY", 0)
8080

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

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

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

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

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

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

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

119119
/** Private cache of all the constants.
120120
*/
121-
private val ENUMS: Array[DayOfWeek] = DayOfWeek.values
121+
private lazy val ENUMS: Array[DayOfWeek] = DayOfWeek.values
122122

123123
/** Obtains an instance of {@code DayOfWeek} from an {@code int} value.
124124
*

core/shared/src/main/scala/org/threeten/bp/Duration.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import org.threeten.bp.temporal.UnsupportedTemporalTypeException
6464
object Duration {
6565

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

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

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

7878
/** The pattern for parsing. */
79-
private val PATTERN: Pattern = Pattern.compile(
79+
private lazy val PATTERN: Pattern = Pattern.compile(
8080
"([-+]?)P(?:([-+]?[0-9]+)D)?" + "(T(?:([-+]?[0-9]+)H)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)(?:[.,]([0-9]{0,9}))?S)?)?",
8181
Pattern.CASE_INSENSITIVE
8282
)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ import org.threeten.bp.temporal.ValueRange
6666
object Instant {
6767

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

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

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

108108
/** Obtains the current instant from the system clock.
109109
*

core/shared/src/main/scala/org/threeten/bp/LocalDate.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ object LocalDate {
7777
/** The minimum supported {@code LocalDate}, '-999999999-01-01'.
7878
* This could be used by an application as a "far past" date.
7979
*/
80-
val MIN: LocalDate = LocalDate.of(Year.MIN_VALUE, 1, 1)
80+
lazy val MIN: LocalDate = LocalDate.of(Year.MIN_VALUE, 1, 1)
8181

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

8787
/** The number of days in a 400 year cycle. */
8888
private val DAYS_PER_CYCLE: Int = 146097

core/shared/src/main/scala/org/threeten/bp/LocalDateTime.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ object LocalDateTime {
7373
* This combines {@link LocalDate#MIN} and {@link LocalTime#MIN}.
7474
* This could be used by an application as a "far past" date-time.
7575
*/
76-
val MIN: LocalDateTime = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN)
76+
lazy val MIN: LocalDateTime = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN)
7777

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

8585
/** Obtains the current date-time from the system clock in the default time-zone.
8686
*

core/shared/src/main/scala/org/threeten/bp/LocalTime.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import org.threeten.bp.temporal.ValueRange
6464
object LocalTime {
6565

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

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

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

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

9393
/** Hours per day. */
9494
private[bp] val HOURS_PER_DAY: Int = 24

core/shared/src/main/scala/org/threeten/bp/Month.scala

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,52 +75,52 @@ object Month {
7575
/** The singleton instance for the month of January with 31 days.
7676
* This has the numeric value of {@code 1}.
7777
*/
78-
val JANUARY = new Month("JANUARY", 0)
78+
lazy val JANUARY = new Month("JANUARY", 0)
7979

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

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

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

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

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

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

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

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

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

125125
/** The singleton instance for the month of November with 30 days.
126126
* This has the numeric value of {@code 11}.
@@ -130,28 +130,28 @@ object Month {
130130
/** The singleton instance for the month of December with 31 days.
131131
* This has the numeric value of {@code 12}.
132132
*/
133-
val DECEMBER = new Month("DECEMBER", 11)
134-
135-
val values: Array[Month] = Array(JANUARY,
136-
FEBRUARY,
137-
MARCH,
138-
APRIL,
139-
MAY,
140-
JUNE,
141-
JULY,
142-
AUGUST,
143-
SEPTEMBER,
144-
OCTOBER,
145-
NOVEMBER,
146-
DECEMBER)
133+
lazy val DECEMBER = new Month("DECEMBER", 11)
134+
135+
lazy val values: Array[Month] = Array(JANUARY,
136+
FEBRUARY,
137+
MARCH,
138+
APRIL,
139+
MAY,
140+
JUNE,
141+
JULY,
142+
AUGUST,
143+
SEPTEMBER,
144+
OCTOBER,
145+
NOVEMBER,
146+
DECEMBER)
147147
def valueOf(enum: String): Month = values.find(_.name() == enum) match {
148148
case Some(month) => month
149149
case _ => throw new IllegalArgumentException(s"Unrecognized month name: $enum")
150150
}
151151

152152
/** Private cache of all the constants.
153153
*/
154-
private val ENUMS: Array[Month] = Month.values
154+
private lazy val ENUMS: Array[Month] = Month.values
155155

156156
/** Obtains an instance of {@code Month} from an {@code int} value.
157157
*

core/shared/src/main/scala/org/threeten/bp/MonthDay.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import org.threeten.bp.temporal.ValueRange
5959
object MonthDay {
6060

6161
/** Parser. */
62-
private val PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
62+
private lazy val PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
6363
.appendLiteral("--")
6464
.appendValue(MONTH_OF_YEAR, 2)
6565
.appendLiteral('-')

core/shared/src/main/scala/org/threeten/bp/OffsetDateTime.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ object OffsetDateTime {
6868
* This combines {@link LocalDateTime#MIN} and {@link ZoneOffset#MAX}.
6969
* This could be used by an application as a "far past" date-time.
7070
*/
71-
val MIN: OffsetDateTime = LocalDateTime.MIN.atOffset(ZoneOffset.MAX)
71+
lazy val MIN: OffsetDateTime = LocalDateTime.MIN.atOffset(ZoneOffset.MAX)
7272

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

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

94-
private val INSTANT_COMPARATOR: Comparator[OffsetDateTime] =
94+
private lazy val INSTANT_COMPARATOR: Comparator[OffsetDateTime] =
9595
new Comparator[OffsetDateTime] {
9696
override def compare(datetime1: OffsetDateTime, datetime2: OffsetDateTime): Int = {
9797
var cmp: Int = java.lang.Long.compare(datetime1.toEpochSecond, datetime2.toEpochSecond)

core/shared/src/main/scala/org/threeten/bp/OffsetTime.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ object OffsetTime {
7070
* This combines {@link LocalTime#MIN} and {@link ZoneOffset#MAX}.
7171
* This could be used by an application as a "far past" date.
7272
*/
73-
val MIN: OffsetTime = LocalTime.MIN.atOffset(ZoneOffset.MAX)
73+
lazy val MIN: OffsetTime = LocalTime.MIN.atOffset(ZoneOffset.MAX)
7474

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

8383
/** Obtains the current time from the system clock in the default time-zone.
8484
*

core/shared/src/main/scala/org/threeten/bp/Period.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ import org.threeten.bp.temporal.UnsupportedTemporalTypeException
5252
object Period {
5353

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

5757
/** The pattern for parsing. */
58-
private val PATTERN: Pattern = Pattern.compile(
58+
private lazy val PATTERN: Pattern = Pattern.compile(
5959
"([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?",
6060
Pattern.CASE_INSENSITIVE
6161
)

core/shared/src/main/scala/org/threeten/bp/Year.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ import org.threeten.bp.temporal.ValueRange
6969
object Year {
7070

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

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

7777
/** Parser. */
7878
private lazy val PARSER: DateTimeFormatter =

core/shared/src/main/scala/org/threeten/bp/YearMonth.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ import org.threeten.bp.temporal.ValueRange
9090
object YearMonth {
9191

9292
/** Parser. */
93-
private val PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
93+
private lazy val PARSER: DateTimeFormatter = new DateTimeFormatterBuilder()
9494
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
9595
.appendLiteral('-')
9696
.appendValue(MONTH_OF_YEAR, 2)

core/shared/src/main/scala/org/threeten/bp/ZoneId.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ object ZoneId {
9393
* </ul><p>
9494
* The map is unmodifiable.
9595
*/
96-
val SHORT_IDS: java.util.Map[String, String] = {
96+
lazy val SHORT_IDS: java.util.Map[String, String] = {
9797
val base = Map[String, String](
9898
"ACT" -> "Australia/Darwin",
9999
"AET" -> "Australia/Sydney",

0 commit comments

Comments
 (0)