<commit_before>/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.kotlin.dsl

import org.gradle.api.InvalidUserCodeException
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.plugins.ExtensionContainer
import org.gradle.api.plugins.ExtraPropertiesExtension

import org.gradle.internal.Cast.uncheckedCast

import kotlin.reflect.KProperty


/**
 * The extra properties extension in this object's extension container.
 *
 * @see [ExtensionContainer.getExtraProperties]
 */
val ExtensionAware.extra: ExtraPropertiesExtension
    get() = extensions.extraProperties


operator fun ExtraPropertiesExtension.provideDelegate(any: Any?, property: KProperty<*>) =
    if (property.returnType.isMarkedNullable) NullableExtraPropertyDelegate(this, property.name)
    else NonNullExtraPropertyDelegate(this, property.name)


/**
 * Enables typed access to extra properties.
 */
interface ExtraPropertyDelegate {
    operator fun <T> getValue(any: Any?, property: KProperty<*>): T
    operator fun <T> setValue(receiver: Any?, property: KProperty<*>, value: T)
}


private
class NonNullExtraPropertyDelegate(
    private val extra: ExtraPropertiesExtension,
    private val name: String
) : ExtraPropertyDelegate {

    override fun <T> getValue(any: Any?, property: KProperty<*>): T {
        val isFound = extra.has(name)
        val foundValue = if (isFound) extra.get(name) else null
        return if (isFound && foundValue != null) uncheckedCast(foundValue)
        else throw InvalidUserCodeException("Cannot get non-null extra property '$name' as it ${if (isFound) "is null" else "does not exist"}")
    }

    override fun <T> setValue(receiver: Any?, property: KProperty<*>, value: T) =
        extra.set(property.name, value)
}


private
class NullableExtraPropertyDelegate(
    private val extra: ExtraPropertiesExtension,
    private val name: String
) : ExtraPropertyDelegate {

    override fun <T> getValue(any: Any?, property: KProperty<*>): T =
        uncheckedCast(if (extra.has(name)) extra.get(name) else null)

    override fun <T> setValue(receiver: Any?, property: KProperty<*>, value: T) =
        extra.set(property.name, value)
}


/**
 * Returns a property delegate provider that will initialize the extra property to the value provided<commit_msg>Rename receiver parameter for consistency<commit_after>/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.kotlin.dsl

import org.gradle.api.InvalidUserCodeException
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.plugins.ExtensionContainer
import org.gradle.api.plugins.ExtraPropertiesExtension

import org.gradle.internal.Cast.uncheckedCast

import kotlin.reflect.KProperty


/**
 * The extra properties extension in this object's extension container.
 *
 * @see [ExtensionContainer.getExtraProperties]
 */
val ExtensionAware.extra: ExtraPropertiesExtension
    get() = extensions.extraProperties


operator fun ExtraPropertiesExtension.provideDelegate(receiver: Any?, property: KProperty<*>) =
    if (property.returnType.isMarkedNullable) NullableExtraPropertyDelegate(this, property.name)
    else NonNullExtraPropertyDelegate(this, property.name)


/**
 * Enables typed access to extra properties.
 */
interface ExtraPropertyDelegate {
    operator fun <T> getValue(receiver: Any?, property: KProperty<*>): T
    operator fun <T> setValue(receiver: Any?, property: KProperty<*>, value: T)
}


private
class NonNullExtraPropertyDelegate(
    private val extra: ExtraPropertiesExtension,
    private val name: String
) : ExtraPropertyDelegate {

    override fun <T> getValue(receiver: Any?, property: KProperty<*>): T {
        val isFound = extra.has(name)
        val foundValue = if (isFound) extra.get(name) else null
        return if (isFound && foundValue != null) uncheckedCast(foundValue)
        else throw InvalidUserCodeException("Cannot get non-null extra property '$name' as it ${if (isFound) "is null" else "does not exist"}")
    }

    override fun <T> setValue(receiver: Any?, property: KProperty<*>, value: T) =
        extra.set(property.name, value)
}


private
class NullableExtraPropertyDelegate(
    private val extra: ExtraPropertiesExtension,
    private val name: String
) : ExtraPropertyDelegate {

    override fun <T> getValue(receiver: Any?, property: KProperty<*>): T =
        uncheckedCast(if (extra.has(name)) extra.get(name) else null)

    override fun <T> setValue(receiver: Any?, property: KProperty<*>, value: T) =
        extra.set(property.name, value)
}


/**
 * Returns a property delegate provider that will initialize the extra property to the value provided
{'fraction_non_alphanumeric': 0.09556786703601108, 'fraction_numerical': 0.002770083102493075, 'mean_word_length': 4.373953488372093, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4232643', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1529, 'n_words': 543}