<commit_before>package com.benoitquenaudon.tvfoot.red.app.domain.match

import android.os.Parcelable
import com.benoitquenaudon.tvfoot.red.util.PreConditions.checkNotNull
import com.benoitquenaudon.tvfoot.red.app.data.entity.Broadcaster
import com.benoitquenaudon.tvfoot.red.app.data.entity.Competition
import com.benoitquenaudon.tvfoot.red.app.data.entity.Match
import com.benoitquenaudon.tvfoot.red.app.data.entity.Team
import com.benoitquenaudon.tvfoot.red.app.domain.matches.displayable.BroadcasterRowDisplayable
import com.benoitquenaudon.tvfoot.red.app.domain.matches.displayable.MatchesItemDisplayable
import com.benoitquenaudon.tvfoot.red.util.stripAccents
import kotlinx.android.parcel.Parcelize
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import java.util.TimeZone
import java.util.concurrent.TimeUnit

@Parcelize
data class MatchDisplayable(
    val headerKey: String,
    val startAt: Long,
    val startTime: String,
    val broadcasters: List<BroadcasterRowDisplayable>,
    val headline: String,
    val competition: String,
    val matchDay: String,
    val live: Boolean,
    val startTimeInText: String,
    val homeTeamLogoPath: String,
    val awayTeamLogoPath: String,
    val location: String?,
    val matchId: String
) : Parcelable, MatchesItemDisplayable {

  override fun isSameAs(newItem: MatchesItemDisplayable): Boolean {
    return newItem is MatchDisplayable && this.matchId == newItem.matchId
  }

  companion object {
    private val shortDateFormat = object : ThreadLocal<DateFormat>() {
      override fun initialValue(): DateFormat {
        val format = SimpleDateFormat("HH:mm", Locale.getDefault())
        format.timeZone = TimeZone.getDefault()
        return format
      }
    }
    private val mediumDateFormat = object : ThreadLocal<DateFormat>() {
      override fun initialValue(): DateFormat {
        val format = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        format.timeZone = TimeZone.getDefault()
        return format
      }
    }
    private val fullTextDateFormat = object : ThreadLocal<DateFormat>() {
      override fun initialValue(): DateFormat {
        val format = SimpleDateFormat("EEEE dd MMMM yyyy HH'h'mm", Locale.getDefault())
        format.timeZone = TimeZone.getDefault()
        return format
      }
    }

    fun fromMatch(match: Match): MatchDisplayable {
      return MatchDisplayable( //
          parseHeaderKey(match.startAt), //
          match.startAt.time, //
          parseStartTime(match.startAt), //
          parseBroadcasters(match.broadcasters), //
          parseHeadLine(match.homeTeam, match.awayTeam, match.label), //
          parseCompetition(match.competition), //
          parseMatchDay(match.label, match.matchDay), //
          isMatchLive(match.startAt), //
          parseStartTimeInText(match.startAt), //
          parseTeamLogoPath(match.homeTeam), //
          parseTeamLogoPath(match.awayTeam), //
          parseLocation(match)!!, match.id)
    }

    private fun parseHeaderKey(startAt: Date): String {
      return mediumDateFormat.get().format(startAt)
    }

    private fun parseStartTime(startAt: Date): String {
      return shortDateFormat.get().format(startAt)
    }

    private fun parseBroadcasters(
        broadcasters: List<Broadcaster>?
    ): List<BroadcasterRowDisplayable> =
        broadcasters?.map { BroadcasterRowDisplayable(it.name, it.code) }
            ?: emptyList()

    private fun parseHeadLine(homeTeam: Team, awayTeam: Team, matchLabel: String?): String =
        if (homeTeam.isEmpty || awayTeam.isEmpty) {
          matchLabel.toString()
        } else {
          homeTeam.name.toString().toUpperCase() + " - " + awayTeam.name.toString().toUpperCase()
        }

    private fun parseCompetition(competition: Competition): String = competition.name

    private fun parseMatchDay(matchLabel: String?, matchDay: String?): String =
        if (matchLabel != null && !matchLabel.isEmpty()) {
          matchLabel<commit_msg>Not NPE check the location<commit_after>package com.benoitquenaudon.tvfoot.red.app.domain.match

import android.os.Parcelable
import com.benoitquenaudon.tvfoot.red.app.data.entity.Broadcaster
import com.benoitquenaudon.tvfoot.red.app.data.entity.Competition
import com.benoitquenaudon.tvfoot.red.app.data.entity.Match
import com.benoitquenaudon.tvfoot.red.app.data.entity.Team
import com.benoitquenaudon.tvfoot.red.app.domain.matches.displayable.BroadcasterRowDisplayable
import com.benoitquenaudon.tvfoot.red.app.domain.matches.displayable.MatchesItemDisplayable
import com.benoitquenaudon.tvfoot.red.util.PreConditions.checkNotNull
import com.benoitquenaudon.tvfoot.red.util.stripAccents
import kotlinx.android.parcel.Parcelize
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import java.util.TimeZone
import java.util.concurrent.TimeUnit

@Parcelize
data class MatchDisplayable(
    val headerKey: String,
    val startAt: Long,
    val startTime: String,
    val broadcasters: List<BroadcasterRowDisplayable>,
    val headline: String,
    val competition: String,
    val matchDay: String,
    val live: Boolean,
    val startTimeInText: String,
    val homeTeamLogoPath: String,
    val awayTeamLogoPath: String,
    val location: String?,
    val matchId: String
) : Parcelable, MatchesItemDisplayable {

  override fun isSameAs(newItem: MatchesItemDisplayable): Boolean {
    return newItem is MatchDisplayable && this.matchId == newItem.matchId
  }

  companion object {
    private val shortDateFormat = object : ThreadLocal<DateFormat>() {
      override fun initialValue(): DateFormat {
        val format = SimpleDateFormat("HH:mm", Locale.getDefault())
        format.timeZone = TimeZone.getDefault()
        return format
      }
    }
    private val mediumDateFormat = object : ThreadLocal<DateFormat>() {
      override fun initialValue(): DateFormat {
        val format = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        format.timeZone = TimeZone.getDefault()
        return format
      }
    }
    private val fullTextDateFormat = object : ThreadLocal<DateFormat>() {
      override fun initialValue(): DateFormat {
        val format = SimpleDateFormat("EEEE dd MMMM yyyy HH'h'mm", Locale.getDefault())
        format.timeZone = TimeZone.getDefault()
        return format
      }
    }

    fun fromMatch(match: Match): MatchDisplayable {
      return MatchDisplayable(
          parseHeaderKey(match.startAt),
          match.startAt.time,
          parseStartTime(match.startAt),
          parseBroadcasters(match.broadcasters),
          parseHeadLine(match.homeTeam, match.awayTeam, match.label),
          parseCompetition(match.competition),
          parseMatchDay(match.label, match.matchDay),
          isMatchLive(match.startAt),
          parseStartTimeInText(match.startAt),
          parseTeamLogoPath(match.homeTeam),
          parseTeamLogoPath(match.awayTeam),
          parseLocation(match),
          match.id)
    }

    private fun parseHeaderKey(startAt: Date): String {
      return mediumDateFormat.get().format(startAt)
    }

    private fun parseStartTime(startAt: Date): String {
      return shortDateFormat.get().format(startAt)
    }

    private fun parseBroadcasters(
        broadcasters: List<Broadcaster>?
    ): List<BroadcasterRowDisplayable> =
        broadcasters?.map { BroadcasterRowDisplayable(it.name, it.code) }
            ?: emptyList()

    private fun parseHeadLine(homeTeam: Team, awayTeam: Team, matchLabel: String?): String =
        if (homeTeam.isEmpty || awayTeam.isEmpty) {
          matchLabel.toString()
        } else {
          homeTeam.name.toString().toUpperCase() + " - " + awayTeam.name.toString().toUpperCase()
        }

    private fun parseCompetition(competition: Competition): String = competition.name

    private fun parseMatchDay(matchLabel: String?, matchDay: String?): String =
        if (matchLabel != null && !matchLabel.isEmpty()) {
          matchLabel
{'fraction_non_alphanumeric': 0.09993846153846153, 'fraction_numerical': 0.0, 'mean_word_length': 3.4235166031573216, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1894925', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2331, 'n_words': 461}