Skip to content

Commit 293f2da

Browse files
authored
Merge pull request #167 from cquiroz/reductions
Size reductions
2 parents dee687b + 01515be commit 293f2da

18 files changed

+149
-185
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ object Duration {
6767
lazy val ZERO: Duration = new Duration(0, 0)
6868

6969
/** Constant for nanos per second. */
70-
private val NANOS_PER_SECOND: Int = 1000000000
70+
private def NANOS_PER_SECOND: Int = 1000000000
7171

7272
/** Constant for nanos per milli. */
73-
private val NANOS_PER_MILLI: Int = 1000000
73+
private def NANOS_PER_MILLI: Int = 1000000
7474

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

7878
/** The pattern for parsing. */
79-
private lazy val PATTERN: Pattern = Pattern.compile(
79+
private def 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: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ object Instant {
6969
lazy val EPOCH: Instant = new Instant(0, 0)
7070

7171
/** The minimum supported epoch second. */
72-
private val MIN_SECOND: Long = -31557014167219200L
72+
private def MIN_SECOND: Long = -31557014167219200L
7373

7474
/** The maximum supported epoch second. */
75-
private val MAX_SECOND: Long = 31556889864403199L
75+
private def MAX_SECOND: Long = 31556889864403199L
7676

7777
/** Constant for nanos per second. */
78-
private val NANOS_PER_SECOND: Int = 1000000000
78+
private def NANOS_PER_SECOND: Int = 1000000000
7979

8080
/** Constant for nanos per milli. */
81-
private val NANOS_PER_MILLI: Int = 1000000
81+
private def NANOS_PER_MILLI: Int = 1000000
8282

8383
/** Constant for millis per sec. */
84-
private val MILLIS_PER_SEC = 1000
84+
private def MILLIS_PER_SEC = 1000
8585

8686
/** The minimum supported {@code Instant}, '-1000000000-01-01T00:00Z'.
8787
* This could be used by an application as a "far past" instant.
@@ -753,12 +753,13 @@ final class Instant private (private val seconds: Long, private val nanos: Int)
753753
* @throws DateTimeException if the result exceeds the maximum or minimum instant
754754
* @throws ArithmeticException if numeric overflow occurs
755755
*/
756-
def minusMillis(millisToSubtract: Long): Instant = {
756+
def minusMillis(millisToSubtract: Long): Instant =
757757
if (millisToSubtract == Long.MinValue) {
758-
return plusMillis(Long.MaxValue).plusMillis(1)
758+
plusMillis(Long.MaxValue).plusMillis(1)
759+
} else {
760+
plusMillis(-millisToSubtract)
761+
759762
}
760-
plusMillis(-millisToSubtract)
761-
}
762763

763764
/** Returns a copy of this instant with the specified duration in nanoseconds subtracted.
764765
*
@@ -904,14 +905,13 @@ final class Instant private (private val seconds: Long, private val nanos: Int)
904905
}
905906

906907
private def secondsUntil(end: Instant): Long = {
907-
var secsDiff: Long = Math.subtractExact(end.seconds, seconds)
908+
val secsDiff: Long = Math.subtractExact(end.seconds, seconds)
908909
val nanosDiff: Long = end.nanos.toLong - nanos.toLong
909910
if (secsDiff > 0 && nanosDiff < 0) {
910-
secsDiff -= 1
911+
secsDiff - 1
911912
} else if (secsDiff < 0 && nanosDiff > 0) {
912-
secsDiff += 1
913-
}
914-
secsDiff
913+
secsDiff + 1
914+
} else secsDiff
915915
}
916916

917917
/** Combines this instant with an offset to create an {@code OffsetDateTime}.

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

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -93,39 +93,38 @@ object ZoneId {
9393
* </ul><p>
9494
* The map is unmodifiable.
9595
*/
96-
lazy val SHORT_IDS: java.util.Map[String, String] = {
97-
val base = Map[String, String](
98-
"ACT" -> "Australia/Darwin",
99-
"AET" -> "Australia/Sydney",
100-
"AGT" -> "America/Argentina/Buenos_Aires",
101-
"ART" -> "Africa/Cairo",
102-
"AST" -> "America/Anchorage",
103-
"BET" -> "America/Sao_Paulo",
104-
"BST" -> "Asia/Dhaka",
105-
"CAT" -> "Africa/Harare",
106-
"CNT" -> "America/St_Johns",
107-
"CST" -> "America/Chicago",
108-
"CTT" -> "Asia/Shanghai",
109-
"EAT" -> "Africa/Addis_Ababa",
110-
"ECT" -> "Europe/Paris",
111-
"IET" -> "America/Indiana/Indianapolis",
112-
"IST" -> "Asia/Kolkata",
113-
"JST" -> "Asia/Tokyo",
114-
"MIT" -> "Pacific/Apia",
115-
"NET" -> "Asia/Yerevan",
116-
"NST" -> "Pacific/Auckland",
117-
"PLT" -> "Asia/Karachi",
118-
"PNT" -> "America/Phoenix",
119-
"PRT" -> "America/Puerto_Rico",
120-
"PST" -> "America/Los_Angeles",
121-
"SST" -> "Pacific/Guadalcanal",
122-
"VST" -> "Asia/Ho_Chi_Minh",
123-
"EST" -> "-05:00",
124-
"MST" -> "-07:00",
125-
"HST" -> "-10:00"
126-
)
127-
base.asJava
128-
}
96+
lazy val SHORT_IDS: java.util.Map[String, String] = ShortIds.asJava
97+
98+
private lazy val ShortIds: Map[String, String] = Map[String, String](
99+
"ACT" -> "Australia/Darwin",
100+
"AET" -> "Australia/Sydney",
101+
"AGT" -> "America/Argentina/Buenos_Aires",
102+
"ART" -> "Africa/Cairo",
103+
"AST" -> "America/Anchorage",
104+
"BET" -> "America/Sao_Paulo",
105+
"BST" -> "Asia/Dhaka",
106+
"CAT" -> "Africa/Harare",
107+
"CNT" -> "America/St_Johns",
108+
"CST" -> "America/Chicago",
109+
"CTT" -> "Asia/Shanghai",
110+
"EAT" -> "Africa/Addis_Ababa",
111+
"ECT" -> "Europe/Paris",
112+
"IET" -> "America/Indiana/Indianapolis",
113+
"IST" -> "Asia/Kolkata",
114+
"JST" -> "Asia/Tokyo",
115+
"MIT" -> "Pacific/Apia",
116+
"NET" -> "Asia/Yerevan",
117+
"NST" -> "Pacific/Auckland",
118+
"PLT" -> "Asia/Karachi",
119+
"PNT" -> "America/Phoenix",
120+
"PRT" -> "America/Puerto_Rico",
121+
"PST" -> "America/Los_Angeles",
122+
"SST" -> "Pacific/Guadalcanal",
123+
"VST" -> "Asia/Ho_Chi_Minh",
124+
"EST" -> "-05:00",
125+
"MST" -> "-07:00",
126+
"HST" -> "-10:00"
127+
)
129128

130129
/** Gets the system default time-zone.
131130
*
@@ -137,7 +136,7 @@ object ZoneId {
137136
* @throws DateTimeException if the converted zone ID has an invalid format
138137
* @throws ZoneRulesException if the converted zone region ID cannot be found
139138
*/
140-
def systemDefault: ZoneId = ZoneId.of(TimeZone.getDefault.getID, SHORT_IDS)
139+
def systemDefault: ZoneId = of2(TimeZone.getDefault.getID, ShortIds)
141140

142141
/** Gets the set of available zone IDs.
143142
*
@@ -168,13 +167,11 @@ object ZoneId {
168167
* @throws DateTimeException if the zone ID has an invalid format
169168
* @throws ZoneRulesException if the zone ID is a region ID that cannot be found
170169
*/
171-
def of(zoneId: String, aliasMap: java.util.Map[String, String]): ZoneId = {
172-
Objects.requireNonNull(zoneId, "zoneId")
173-
Objects.requireNonNull(aliasMap, "aliasMap")
174-
var id: String = aliasMap.get(zoneId)
175-
id = if (id != null) id else zoneId
176-
of(id)
177-
}
170+
def of(zoneId: String, aliasMap: java.util.Map[String, String]): ZoneId =
171+
of2(zoneId, aliasMap.asScala.toMap)
172+
173+
private def of2(zoneId: String, aliasMap: Map[String, String]): ZoneId =
174+
of(aliasMap.get(zoneId).getOrElse(zoneId))
178175

179176
/** Obtains an instance of {@code ZoneId} from an ID ensuring that the
180177
* ID is valid and available for use.

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,29 +256,34 @@ object ZoneOffset {
256256
* @throws DateTimeException if the offset is not in the required range
257257
*/
258258
private def validate(hours: Int, minutes: Int, seconds: Int): Unit = {
259+
def msg(v: String) =
260+
s"Zone offset $v not in valid range: abs(value) ${Math.abs(minutes)} is not in the range 0 to 59"
261+
262+
def msg2(v: String) =
263+
s"Zone offset minutes and seconds must be $v because hours is $v"
259264
if (hours < -18 || hours > 18)
260265
throw new DateTimeException(
261266
s"Zone offset hours not in valid range: value $hours is not in the range -18 to 18"
262267
)
263268
if (hours > 0) {
264269
if (minutes < 0 || seconds < 0)
265270
throw new DateTimeException(
266-
"Zone offset minutes and seconds must be positive because hours is positive"
271+
msg2("positive")
267272
)
268273
} else if (hours < 0) {
269274
if (minutes > 0 || seconds > 0)
270275
throw new DateTimeException(
271-
"Zone offset minutes and seconds must be negative because hours is negative"
276+
msg2("negative")
272277
)
273278
} else if ((minutes > 0 && seconds < 0) || (minutes < 0 && seconds > 0))
274279
throw new DateTimeException("Zone offset minutes and seconds must have the same sign")
275280
if (Math.abs(minutes) > 59)
276281
throw new DateTimeException(
277-
s"Zone offset minutes not in valid range: abs(value) ${Math.abs(minutes)} is not in the range 0 to 59"
282+
msg("minutes")
278283
)
279284
if (Math.abs(seconds) > 59)
280285
throw new DateTimeException(
281-
s"Zone offset seconds not in valid range: abs(value) ${Math.abs(seconds)} is not in the range 0 to 59"
286+
msg("seconds")
282287
)
283288
if (Math.abs(hours) == 18 && (Math.abs(minutes) > 0 || Math.abs(seconds) > 0))
284289
throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00")

core/shared/src/main/scala/org/threeten/bp/format/DateTimeBuilder.scala

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ final class DateTimeBuilder() extends TemporalAccessor with Cloneable {
552552
* @return the extracted value, not null
553553
* @throws DateTimeException if an error occurs
554554
*/
555-
def build[R](`type`: TemporalQuery[R]): R = `type`.queryFrom(this)
555+
def build[R](tpe: TemporalQuery[R]): R = tpe.queryFrom(this)
556556

557557
def isSupported(field: TemporalField): Boolean =
558558
if (field == null)
@@ -591,15 +591,10 @@ final class DateTimeBuilder() extends TemporalAccessor with Cloneable {
591591
query.queryFrom(this)
592592

593593
override def toString: String = {
594-
val buf: StringBuilder = new StringBuilder(128)
595-
buf.append("DateTimeBuilder[")
596-
if (fieldValues.size > 0)
597-
buf.append("fields=").append(fieldValues)
598-
buf.append(", ").append(chrono)
599-
buf.append(", ").append(zone)
600-
buf.append(", ").append(date)
601-
buf.append(", ").append(time)
602-
buf.append(']')
603-
buf.toString
594+
val fields =
595+
if (fieldValues.size > 0)
596+
s"fields=$fieldValues, "
597+
else ""
598+
s"DateTimeBuilder[$fields $chrono, $zone, $date, $time]"
604599
}
605600
}

core/shared/src/main/scala/org/threeten/bp/format/DateTimeFormatter.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,9 @@ object DateTimeFormatter {
910910
def parseObject(text: String, pos: ParsePosition): AnyRef = {
911911
Objects.requireNonNull(text, "text")
912912
var unresolved: TTBPDateTimeParseContext#Parsed = null
913-
try unresolved = formatter.parseUnresolved0(text, pos)
914-
catch {
913+
try {
914+
unresolved = formatter.parseUnresolved0(text, pos)
915+
} catch {
915916
case _: IndexOutOfBoundsException =>
916917
if (pos.getErrorIndex < 0)
917918
pos.setErrorIndex(0)
@@ -1597,14 +1598,14 @@ final class DateTimeFormatter private[format] (
15971598
Objects.requireNonNull(text, "text")
15981599
Objects.requireNonNull(position, "position")
15991600
val context: TTBPDateTimeParseContext = new TTBPDateTimeParseContext(this)
1600-
var pos: Int = position.getIndex
1601-
pos = printerParser.parse(context, text, pos)
1601+
val pos = printerParser.parse(context, text, position.getIndex())
16021602
if (pos < 0) {
16031603
position.setErrorIndex(~pos)
1604-
return null
1604+
null
1605+
} else {
1606+
position.setIndex(pos)
1607+
context.toParsed
16051608
}
1606-
position.setIndex(pos)
1607-
context.toParsed
16081609
}
16091610

16101611
/** Returns the formatter as a composite printer parser.

core/shared/src/main/scala/org/threeten/bp/format/DateTimeFormatterBuilder.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ final class DateTimeFormatterBuilder private (
673673
appendInternal(
674674
new TTBPDateTimeFormatterBuilder.TextPrinterParser(field,
675675
textStyle,
676-
TTBPDateTimeTextProvider.getInstance)
676+
TTBPDateTimeTextProvider.Provider)
677677
)
678678
this
679679
}

core/shared/src/main/scala/org/threeten/bp/format/internal/TTBPDateTimeTextProvider.scala

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,48 +32,11 @@
3232
package org.threeten.bp.format.internal
3333

3434
import java.util.Locale
35-
import java.util.concurrent.atomic.AtomicReference
3635
import org.threeten.bp.temporal.TemporalField
3736
import org.threeten.bp.format.TextStyle
3837

3938
private[format] object TTBPDateTimeTextProvider {
40-
lazy val MUTABLE_PROVIDER: AtomicReference[TTBPDateTimeTextProvider] =
41-
new AtomicReference[TTBPDateTimeTextProvider]()
42-
43-
/** Gets the provider.
44-
*
45-
* @return the provider, not null
46-
*/
47-
private[format] def getInstance: TTBPDateTimeTextProvider =
48-
ProviderSingleton.PROVIDER
49-
50-
/**
51-
* Sets the provider to use.
52-
* <p>
53-
* This can only be invoked before {@link DateTimeTextProvider} class is used for formatting/parsing.
54-
* Invoking this method at a later point will throw an exception.
55-
*
56-
* @param provider the provider to use, not null
57-
* @throws IllegalStateException if provider is already set
58-
*/
59-
def setInitializer(provider: TTBPDateTimeTextProvider): Unit =
60-
if (!MUTABLE_PROVIDER.compareAndSet(null, provider)) {
61-
throw new IllegalStateException(
62-
"Provider was already set, possibly with a default during initialization"
63-
)
64-
}
65-
66-
//-----------------------------------------------------------------------
67-
// use JVM class initializtion to lock the singleton without additional synchronization
68-
object ProviderSingleton {
69-
lazy val PROVIDER: TTBPDateTimeTextProvider = initialize()
70-
// initialize the provider
71-
def initialize(): TTBPDateTimeTextProvider = {
72-
// Set the default initializer if none has been provided yet
73-
MUTABLE_PROVIDER.compareAndSet(null, new TTBPSimpleDateTimeTextProvider())
74-
return MUTABLE_PROVIDER.get();
75-
}
76-
}
39+
lazy val Provider: TTBPDateTimeTextProvider = new TTBPSimpleDateTimeTextProvider()
7740
}
7841

7942
/** The Service Provider Interface (SPI) to be implemented by classes providing

0 commit comments

Comments
 (0)