Skip to content

Size reductions #167

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 1 commit into from
Mar 15, 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
8 changes: 4 additions & 4 deletions core/shared/src/main/scala/org/threeten/bp/Duration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ object Duration {
lazy val ZERO: Duration = new Duration(0, 0)

/** Constant for nanos per second. */
private val NANOS_PER_SECOND: Int = 1000000000
private def NANOS_PER_SECOND: Int = 1000000000

/** Constant for nanos per milli. */
private val NANOS_PER_MILLI: Int = 1000000
private def NANOS_PER_MILLI: Int = 1000000

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

/** The pattern for parsing. */
private lazy val PATTERN: Pattern = Pattern.compile(
private def 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
28 changes: 14 additions & 14 deletions core/shared/src/main/scala/org/threeten/bp/Instant.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ object Instant {
lazy val EPOCH: Instant = new Instant(0, 0)

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

/** The maximum supported epoch second. */
private val MAX_SECOND: Long = 31556889864403199L
private def MAX_SECOND: Long = 31556889864403199L

/** Constant for nanos per second. */
private val NANOS_PER_SECOND: Int = 1000000000
private def NANOS_PER_SECOND: Int = 1000000000

/** Constant for nanos per milli. */
private val NANOS_PER_MILLI: Int = 1000000
private def NANOS_PER_MILLI: Int = 1000000

/** Constant for millis per sec. */
private val MILLIS_PER_SEC = 1000
private def MILLIS_PER_SEC = 1000

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

}
plusMillis(-millisToSubtract)
}

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

private def secondsUntil(end: Instant): Long = {
var secsDiff: Long = Math.subtractExact(end.seconds, seconds)
val secsDiff: Long = Math.subtractExact(end.seconds, seconds)
val nanosDiff: Long = end.nanos.toLong - nanos.toLong
if (secsDiff > 0 && nanosDiff < 0) {
secsDiff -= 1
secsDiff - 1
} else if (secsDiff < 0 && nanosDiff > 0) {
secsDiff += 1
}
secsDiff
secsDiff + 1
} else secsDiff
}

/** Combines this instant with an offset to create an {@code OffsetDateTime}.
Expand Down
79 changes: 38 additions & 41 deletions core/shared/src/main/scala/org/threeten/bp/ZoneId.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,39 +93,38 @@ object ZoneId {
* </ul><p>
* The map is unmodifiable.
*/
lazy val SHORT_IDS: java.util.Map[String, String] = {
val base = Map[String, String](
"ACT" -> "Australia/Darwin",
"AET" -> "Australia/Sydney",
"AGT" -> "America/Argentina/Buenos_Aires",
"ART" -> "Africa/Cairo",
"AST" -> "America/Anchorage",
"BET" -> "America/Sao_Paulo",
"BST" -> "Asia/Dhaka",
"CAT" -> "Africa/Harare",
"CNT" -> "America/St_Johns",
"CST" -> "America/Chicago",
"CTT" -> "Asia/Shanghai",
"EAT" -> "Africa/Addis_Ababa",
"ECT" -> "Europe/Paris",
"IET" -> "America/Indiana/Indianapolis",
"IST" -> "Asia/Kolkata",
"JST" -> "Asia/Tokyo",
"MIT" -> "Pacific/Apia",
"NET" -> "Asia/Yerevan",
"NST" -> "Pacific/Auckland",
"PLT" -> "Asia/Karachi",
"PNT" -> "America/Phoenix",
"PRT" -> "America/Puerto_Rico",
"PST" -> "America/Los_Angeles",
"SST" -> "Pacific/Guadalcanal",
"VST" -> "Asia/Ho_Chi_Minh",
"EST" -> "-05:00",
"MST" -> "-07:00",
"HST" -> "-10:00"
)
base.asJava
}
lazy val SHORT_IDS: java.util.Map[String, String] = ShortIds.asJava

private lazy val ShortIds: Map[String, String] = Map[String, String](
"ACT" -> "Australia/Darwin",
"AET" -> "Australia/Sydney",
"AGT" -> "America/Argentina/Buenos_Aires",
"ART" -> "Africa/Cairo",
"AST" -> "America/Anchorage",
"BET" -> "America/Sao_Paulo",
"BST" -> "Asia/Dhaka",
"CAT" -> "Africa/Harare",
"CNT" -> "America/St_Johns",
"CST" -> "America/Chicago",
"CTT" -> "Asia/Shanghai",
"EAT" -> "Africa/Addis_Ababa",
"ECT" -> "Europe/Paris",
"IET" -> "America/Indiana/Indianapolis",
"IST" -> "Asia/Kolkata",
"JST" -> "Asia/Tokyo",
"MIT" -> "Pacific/Apia",
"NET" -> "Asia/Yerevan",
"NST" -> "Pacific/Auckland",
"PLT" -> "Asia/Karachi",
"PNT" -> "America/Phoenix",
"PRT" -> "America/Puerto_Rico",
"PST" -> "America/Los_Angeles",
"SST" -> "Pacific/Guadalcanal",
"VST" -> "Asia/Ho_Chi_Minh",
"EST" -> "-05:00",
"MST" -> "-07:00",
"HST" -> "-10:00"
)

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

/** Gets the set of available zone IDs.
*
Expand Down Expand Up @@ -168,13 +167,11 @@ object ZoneId {
* @throws DateTimeException if the zone ID has an invalid format
* @throws ZoneRulesException if the zone ID is a region ID that cannot be found
*/
def of(zoneId: String, aliasMap: java.util.Map[String, String]): ZoneId = {
Objects.requireNonNull(zoneId, "zoneId")
Objects.requireNonNull(aliasMap, "aliasMap")
var id: String = aliasMap.get(zoneId)
id = if (id != null) id else zoneId
of(id)
}
def of(zoneId: String, aliasMap: java.util.Map[String, String]): ZoneId =
of2(zoneId, aliasMap.asScala.toMap)

private def of2(zoneId: String, aliasMap: Map[String, String]): ZoneId =
of(aliasMap.get(zoneId).getOrElse(zoneId))

/** Obtains an instance of {@code ZoneId} from an ID ensuring that the
* ID is valid and available for use.
Expand Down
13 changes: 9 additions & 4 deletions core/shared/src/main/scala/org/threeten/bp/ZoneOffset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,29 +256,34 @@ object ZoneOffset {
* @throws DateTimeException if the offset is not in the required range
*/
private def validate(hours: Int, minutes: Int, seconds: Int): Unit = {
def msg(v: String) =
s"Zone offset $v not in valid range: abs(value) ${Math.abs(minutes)} is not in the range 0 to 59"

def msg2(v: String) =
s"Zone offset minutes and seconds must be $v because hours is $v"
if (hours < -18 || hours > 18)
throw new DateTimeException(
s"Zone offset hours not in valid range: value $hours is not in the range -18 to 18"
)
if (hours > 0) {
if (minutes < 0 || seconds < 0)
throw new DateTimeException(
"Zone offset minutes and seconds must be positive because hours is positive"
msg2("positive")
)
} else if (hours < 0) {
if (minutes > 0 || seconds > 0)
throw new DateTimeException(
"Zone offset minutes and seconds must be negative because hours is negative"
msg2("negative")
)
} else if ((minutes > 0 && seconds < 0) || (minutes < 0 && seconds > 0))
throw new DateTimeException("Zone offset minutes and seconds must have the same sign")
if (Math.abs(minutes) > 59)
throw new DateTimeException(
s"Zone offset minutes not in valid range: abs(value) ${Math.abs(minutes)} is not in the range 0 to 59"
msg("minutes")
)
if (Math.abs(seconds) > 59)
throw new DateTimeException(
s"Zone offset seconds not in valid range: abs(value) ${Math.abs(seconds)} is not in the range 0 to 59"
msg("seconds")
)
if (Math.abs(hours) == 18 && (Math.abs(minutes) > 0 || Math.abs(seconds) > 0))
throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ final class DateTimeBuilder() extends TemporalAccessor with Cloneable {
* @return the extracted value, not null
* @throws DateTimeException if an error occurs
*/
def build[R](`type`: TemporalQuery[R]): R = `type`.queryFrom(this)
def build[R](tpe: TemporalQuery[R]): R = tpe.queryFrom(this)

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

override def toString: String = {
val buf: StringBuilder = new StringBuilder(128)
buf.append("DateTimeBuilder[")
if (fieldValues.size > 0)
buf.append("fields=").append(fieldValues)
buf.append(", ").append(chrono)
buf.append(", ").append(zone)
buf.append(", ").append(date)
buf.append(", ").append(time)
buf.append(']')
buf.toString
val fields =
if (fieldValues.size > 0)
s"fields=$fieldValues, "
else ""
s"DateTimeBuilder[$fields $chrono, $zone, $date, $time]"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,9 @@ object DateTimeFormatter {
def parseObject(text: String, pos: ParsePosition): AnyRef = {
Objects.requireNonNull(text, "text")
var unresolved: TTBPDateTimeParseContext#Parsed = null
try unresolved = formatter.parseUnresolved0(text, pos)
catch {
try {
unresolved = formatter.parseUnresolved0(text, pos)
} catch {
case _: IndexOutOfBoundsException =>
if (pos.getErrorIndex < 0)
pos.setErrorIndex(0)
Expand Down Expand Up @@ -1597,14 +1598,14 @@ final class DateTimeFormatter private[format] (
Objects.requireNonNull(text, "text")
Objects.requireNonNull(position, "position")
val context: TTBPDateTimeParseContext = new TTBPDateTimeParseContext(this)
var pos: Int = position.getIndex
pos = printerParser.parse(context, text, pos)
val pos = printerParser.parse(context, text, position.getIndex())
if (pos < 0) {
position.setErrorIndex(~pos)
return null
null
} else {
position.setIndex(pos)
context.toParsed
}
position.setIndex(pos)
context.toParsed
}

/** Returns the formatter as a composite printer parser.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ final class DateTimeFormatterBuilder private (
appendInternal(
new TTBPDateTimeFormatterBuilder.TextPrinterParser(field,
textStyle,
TTBPDateTimeTextProvider.getInstance)
TTBPDateTimeTextProvider.Provider)
)
this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,11 @@
package org.threeten.bp.format.internal

import java.util.Locale
import java.util.concurrent.atomic.AtomicReference
import org.threeten.bp.temporal.TemporalField
import org.threeten.bp.format.TextStyle

private[format] object TTBPDateTimeTextProvider {
lazy val MUTABLE_PROVIDER: AtomicReference[TTBPDateTimeTextProvider] =
new AtomicReference[TTBPDateTimeTextProvider]()

/** Gets the provider.
*
* @return the provider, not null
*/
private[format] def getInstance: TTBPDateTimeTextProvider =
ProviderSingleton.PROVIDER

/**
* Sets the provider to use.
* <p>
* This can only be invoked before {@link DateTimeTextProvider} class is used for formatting/parsing.
* Invoking this method at a later point will throw an exception.
*
* @param provider the provider to use, not null
* @throws IllegalStateException if provider is already set
*/
def setInitializer(provider: TTBPDateTimeTextProvider): Unit =
if (!MUTABLE_PROVIDER.compareAndSet(null, provider)) {
throw new IllegalStateException(
"Provider was already set, possibly with a default during initialization"
)
}

//-----------------------------------------------------------------------
// use JVM class initializtion to lock the singleton without additional synchronization
object ProviderSingleton {
lazy val PROVIDER: TTBPDateTimeTextProvider = initialize()
// initialize the provider
def initialize(): TTBPDateTimeTextProvider = {
// Set the default initializer if none has been provided yet
MUTABLE_PROVIDER.compareAndSet(null, new TTBPSimpleDateTimeTextProvider())
return MUTABLE_PROVIDER.get();
}
}
lazy val Provider: TTBPDateTimeTextProvider = new TTBPSimpleDateTimeTextProvider()
}

/** The Service Provider Interface (SPI) to be implemented by classes providing
Expand Down
Loading