Repository: square/okhttp
File: okhttp/src/jvmMain/kotlin/okhttp3/Cookie.kt (Kotlin)

Code before:
    private fun domain(domain: String, hostOnly: Boolean) = apply {
      val canonicalDomain = domain.toCanonicalHost()
        ?: throw IllegalArgumentException("unexpected domain: $domain")
      this.domain = canonicalDomain
      this.hostOnly = hostOnly
    }

    fun path(path: String) = apply {
      require(path.startsWith("/")) { "path must start with '/'" }
      this.path = path
    }

    fun secure() = apply {
      this.secure = true
    }

    fun httpOnly() = apply {
      this.httpOnly = true
    }

    fun samesite(sameSite: String) = apply {
      val validSameSites = listOf("lax", "strict", "none")
      require(validSameSites.contains(sameSite.lowercase())) { "SameSite must be one of: ${validSameSites.joinToString(", ")}" }

      if (sameSite.lowercase() == "none") {
        require(secure) { "Secure attribute is required for this SameSite policy" }
      }

      this.sameSite = sameSite
    }

    fun build(): Cookie {
      return Cookie(
        name ?: throw NullPointerException("builder.name == null"),
        value ?: throw NullPointerException("builder.value == null"),
        expiresAt,
        domain ?: throw NullPointerException("builder.domain == null"),
        path,
        secure,
        httpOnly,
        persistent,
        hostOnly,
        sameSite
      )
    }
  }

  @Suppress("NAME_SHADOWING")
  companion object {
    private val YEAR_PATTERN = Pattern.compile("(\\d{2,4})[^\\d]*")
    private val MONTH_PATTERN =

Review comment (suggestion):
We need to defer this check until the `build()` call is made,  because the user might set these values in an order different from what we expect.



Code after:

    /**
     * Set the domain pattern for this cookie. The cookie will match [domain] and all of its
     * subdomains.
     */
    fun domain(domain: String): Builder = domain(domain, false)

    /**
     * Set the host-only domain for this cookie. The cookie will match [domain] but none of
     * its subdomains.
     */
    fun hostOnlyDomain(domain: String): Builder = domain(domain, true)

    private fun domain(domain: String, hostOnly: Boolean) = apply {
      val canonicalDomain = domain.toCanonicalHost()
        ?: throw IllegalArgumentException("unexpected domain: $domain")
      this.domain = canonicalDomain
      this.hostOnly = hostOnly
    }

    fun path(path: String) = apply {
      require(path.startsWith("/")) { "path must start with '/'" }
      this.path = path
    }

    fun secure() = apply {
      this.secure = true
    }

    fun httpOnly() = apply {
      this.httpOnly = true
    }

    fun sameSite(sameSite: String) = apply {
      require(sameSite.trim() == sameSite) { "sameSite is not trimmed" }
      this.sameSite = sameSite
    }

    fun build(): Cookie {

      return Cookie(
        name ?: throw NullPointerException("builder.name == null"),
        value ?: throw NullPointerException("builder.value == null"),
        expiresAt,
        domain ?: throw NullPointerException("builder.domain == null"),
        path,
        secure,
        httpOnly,
        persistent,
        hostOnly,
        sameSite