<commit_before>package com.cognifide.gradle.aem.common.utils

import com.fasterxml.jackson.core.util.DefaultIndenter
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.jayway.jsonpath.JsonPath
import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.StringUtils
import org.apache.commons.lang3.time.DurationFormatUtils
import org.apache.commons.validator.routines.UrlValidator
import org.apache.jackrabbit.util.ISO8601
import org.gradle.api.Project
import org.gradle.util.GradleVersion
import java.io.File
import java.io.InputStream
import java.math.BigInteger
import java.nio.file.Paths
import java.security.MessageDigest
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.temporal.ChronoUnit
import java.util.*
import java.util.regex.Pattern

@Suppress("MagicNumber", "TooManyFunctions")
object Formats {

    val URL_VALIDATOR = UrlValidator(arrayOf("http", "https"), UrlValidator.ALLOW_LOCAL_URLS)

    fun asPassword(value: String) = "*".repeat(value.length)

    /**
     * Trims e.g ".SP2" in "6.1.0.SP2" which is not valid Gradle version
     */
    fun asVersion(value: String): GradleVersion {
        if (value.isBlank()) {
            return VERSION_UNKNOWN
        }

        return try {
            GradleVersion.version(value.split(".").take(3).joinToString("."))
        } catch (e: IllegalArgumentException) {
            return VERSION_UNKNOWN
        }
    }

    fun versionAtLeast(actual: String, required: String) = asVersion(actual) >= asVersion(required)

    val VERSION_UNKNOWN = GradleVersion.version("0.0.0")

    fun jsonMapper(pretty: Boolean): ObjectMapper = ObjectMapper().apply {
        registerModule(KotlinModule())
        if (pretty) {
            writer(DefaultPrettyPrinter().apply {
                indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE)
            })
        }
    }

    fun asJson(input: InputStream) = JsonPath.parse(input)

    fun asJson(value: String) = JsonPath.parse(value)

    fun toJson(value: Any, pretty: Boolean = true): String {
        return jsonMapper(pretty).writeValueAsString(value) ?: ""
    }

    fun toJson(value: Map<String, Any?>, pretty: Boolean = true): String {
        return jsonMapper(pretty).writeValueAsString(value) ?: "{}"
    }

    inline fun <reified T : Any> fromJson(json: String) = fromJson(json, T::class.java)

    fun <T> fromJson(json: String, clazz: Class<T>): T = jsonMapper(false).readValue(json, clazz)

    fun fromJsonToMap(json: String): Map<String, Any?> = ObjectMapper().run {
        readValue(json, typeFactory.constructMapType(HashMap::class.java, String::class.java, Any::class.java))
    }

    fun toList(value: String?, delimiter: String = ","): List<String>? {
        if (value.isNullOrBlank()) {
            return null
        }

        val between = StringUtils.substringBetween(value, "[", "]") ?: value
        if (between.isBlank()) {
            return null
        }

        return between.split(delimiter)
    }

    fun toMap(value: String?, valueDelimiter: String = ",", keyDelimiter: String = "="): Map<String, String>? {
        return toList(value, valueDelimiter)?.map { v ->
            v.split(keyDelimiter).let { e ->
                when (e.size) {
                    2 -> e[0] to e[1]
                    else -> v to ""
                }
            }
        }?.toMap()
    }

    fun toBase64(value: String): String {<commit_msg>Preety printing fixed<commit_after>package com.cognifide.gradle.aem.common.utils

import com.fasterxml.jackson.core.util.DefaultIndenter
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.jayway.jsonpath.JsonPath
import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.StringUtils
import org.apache.commons.lang3.time.DurationFormatUtils
import org.apache.commons.validator.routines.UrlValidator
import org.apache.jackrabbit.util.ISO8601
import org.gradle.api.Project
import org.gradle.util.GradleVersion
import java.io.File
import java.io.InputStream
import java.math.BigInteger
import java.nio.file.Paths
import java.security.MessageDigest
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.temporal.ChronoUnit
import java.util.*
import java.util.regex.Pattern

@Suppress("MagicNumber", "TooManyFunctions")
object Formats {

    val URL_VALIDATOR = UrlValidator(arrayOf("http", "https"), UrlValidator.ALLOW_LOCAL_URLS)

    fun asPassword(value: String) = "*".repeat(value.length)

    /**
     * Trims e.g ".SP2" in "6.1.0.SP2" which is not valid Gradle version
     */
    fun asVersion(value: String): GradleVersion {
        if (value.isBlank()) {
            return VERSION_UNKNOWN
        }

        return try {
            GradleVersion.version(value.split(".").take(3).joinToString("."))
        } catch (e: IllegalArgumentException) {
            return VERSION_UNKNOWN
        }
    }

    fun versionAtLeast(actual: String, required: String) = asVersion(actual) >= asVersion(required)

    val VERSION_UNKNOWN = GradleVersion.version("0.0.0")

    fun jsonMapper() = ObjectMapper().apply {
        registerModule(KotlinModule())
    }

    fun jsonWriter(pretty: Boolean) = jsonMapper().run {
        when {
            pretty -> writer(DefaultPrettyPrinter().apply {
                indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE)
            })
            else -> writer()
        }
    }

    fun asJson(input: InputStream) = JsonPath.parse(input)

    fun asJson(value: String) = JsonPath.parse(value)

    fun toJson(value: Any, pretty: Boolean = true): String {
        return jsonWriter(pretty).writeValueAsString(value) ?: ""
    }

    fun toJson(value: Map<String, Any?>, pretty: Boolean = true): String {
        return jsonWriter(pretty).writeValueAsString(value) ?: "{}"
    }

    inline fun <reified T : Any> fromJson(json: String) = fromJson(json, T::class.java)

    fun <T> fromJson(json: String, clazz: Class<T>): T = jsonMapper().readValue(json, clazz)

    fun fromJsonToMap(json: String): Map<String, Any?> = jsonMapper().run {
        readValue(json, typeFactory.constructMapType(HashMap::class.java, String::class.java, Any::class.java))
    }

    fun toList(value: String?, delimiter: String = ","): List<String>? {
        if (value.isNullOrBlank()) {
            return null
        }

        val between = StringUtils.substringBetween(value, "[", "]") ?: value
        if (between.isBlank()) {
            return null
        }

        return between.split(delimiter)
    }

    fun toMap(value: String?, valueDelimiter: String = ",", keyDelimiter: String = "="): Map<String, String>? {
        return toList(value, valueDelimiter)?.map { v ->
            v.split(keyDelimiter).let { e ->
                when (e.size) {
                    2 -> e[0] to e[1]
                    else -> v to ""
                }
            }
        }?.toMap()
    }

    fun toBase64(value: String): String {
{'fraction_non_alphanumeric': 0.12434734817257488, 'fraction_numerical': 0.005496015388843089, 'mean_word_length': 3.0171081677704192, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '3834579', 'n_tokens_mistral': 2264, 'n_tokens_neox': 2177, 'n_words': 434}