<commit_before>/*
 * Copyright 2020 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.internal.deprecation;

import com.google.common.base.Joiner;

import javax.annotation.CheckReturnValue;
import java.util.List;

import static org.gradle.internal.deprecation.Messages.pleaseUseThisMethodInstead;
import static org.gradle.internal.deprecation.Messages.thisIsScheduledToBeRemoved;
import static org.gradle.internal.deprecation.Messages.thisWillBecomeAnError;

@CheckReturnValue
public class DeprecationMessageBuilder<T extends DeprecationMessageBuilder<T>> {

    private String summary;
    private String removalDetails;
    private String context;
    private String advice;
    private Documentation documentation = Documentation.NO_DOCUMENTATION;
    private DeprecatedFeatureUsage.Type usageType = DeprecatedFeatureUsage.Type.USER_CODE_DIRECT;

    DeprecationMessageBuilder() {
    }

    public T withContext(String context) {
        this.context = context;
        return (T) this;
    }

    public T withAdvice(String advice) {
        this.advice = advice;
        return (T) this;
    }

    public WithDocumentation undocumented() {
        return new WithDocumentation(this);
    }

    public WithDocumentation withUserManual(String documentationId) {
        this.documentation = Documentation.userManual(documentationId);
        return new WithDocumentation(this);
    }

    public WithDocumentation withUserManual(String documentationId, String section) {
        this.documentation = Documentation.userManual(documentationId, section);
        return new WithDocumentation(this);
    }

    public WithDocumentation withDslReference(Class<?> targetClass, String property) {
        this.documentation = Documentation.dslReference(targetClass, property);
        return new WithDocumentation(this);
    }

    public WithDocumentation withUpgradeGuideSection(int majorVersion, String upgradeGuideSection) {
        this.documentation = Documentation.upgradeGuide(majorVersion, upgradeGuideSection);
        return new WithDocumentation(this);
    }

    void setIndirectUsage() {
        this.usageType = DeprecatedFeatureUsage.Type.USER_CODE_INDIRECT;
    }

    void setBuildInvocationUsage() {
        this.usageType = DeprecatedFeatureUsage.Type.BUILD_INVOCATION;
    }

    void setSummary(String summary) {
        this.summary = summary;
    }

    void setAdvice(String advice) {
        this.advice = advice;
    }

    void setRemovalDetails(String removalDetails) {
        this.removalDetails = removalDetails;
    }

    void setDocumentation(Documentation documentation) {
        this.documentation = documentation;
    }

    DeprecationMessage build() {
        return new DeprecationMessage(summary, removalDetails, advice, context, documentation, usageType);
    }

    public static class WithDocumentation {
        private final DeprecationMessageBuilder<?> builder;

        public WithDocumentation(DeprecationMessageBuilder<?> builder) {
            this.builder = builder;
        }

        public void nagUser() {
            DeprecationLogger.nagUserWith(builder, WithDocumentation.class);
        }
    }

    public static abstract class WithReplacement<T, SELF extends WithReplacement<T, SELF>> extends DeprecationMessageBuilder<SELF> {
        private final String subject;
        private T replacement;

        WithReplacement(String subject) {
            this.subject = subject;
        }

        public SELF replaceWith(T replacement) {
            this.replacement = replacement;
            return (SELF) this;
        }

        String formatSubject() {
            return subject;
        }

        abstract String formatSummary(String subject);
<commit_msg>Suppress unchecked cast warnings in DeprecationMessageBuilder<commit_after>/*
 * Copyright 2020 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.internal.deprecation;

import com.google.common.base.Joiner;

import javax.annotation.CheckReturnValue;
import java.util.List;

import static org.gradle.internal.deprecation.Messages.pleaseUseThisMethodInstead;
import static org.gradle.internal.deprecation.Messages.thisIsScheduledToBeRemoved;
import static org.gradle.internal.deprecation.Messages.thisWillBecomeAnError;

@CheckReturnValue
public class DeprecationMessageBuilder<T extends DeprecationMessageBuilder<T>> {

    private String summary;
    private String removalDetails;
    private String context;
    private String advice;
    private Documentation documentation = Documentation.NO_DOCUMENTATION;
    private DeprecatedFeatureUsage.Type usageType = DeprecatedFeatureUsage.Type.USER_CODE_DIRECT;

    DeprecationMessageBuilder() {
    }

    @SuppressWarnings("unchecked")
    public T withContext(String context) {
        this.context = context;
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public T withAdvice(String advice) {
        this.advice = advice;
        return (T) this;
    }

    public WithDocumentation undocumented() {
        return new WithDocumentation(this);
    }

    public WithDocumentation withUserManual(String documentationId) {
        this.documentation = Documentation.userManual(documentationId);
        return new WithDocumentation(this);
    }

    public WithDocumentation withUserManual(String documentationId, String section) {
        this.documentation = Documentation.userManual(documentationId, section);
        return new WithDocumentation(this);
    }

    public WithDocumentation withDslReference(Class<?> targetClass, String property) {
        this.documentation = Documentation.dslReference(targetClass, property);
        return new WithDocumentation(this);
    }

    public WithDocumentation withUpgradeGuideSection(int majorVersion, String upgradeGuideSection) {
        this.documentation = Documentation.upgradeGuide(majorVersion, upgradeGuideSection);
        return new WithDocumentation(this);
    }

    void setIndirectUsage() {
        this.usageType = DeprecatedFeatureUsage.Type.USER_CODE_INDIRECT;
    }

    void setBuildInvocationUsage() {
        this.usageType = DeprecatedFeatureUsage.Type.BUILD_INVOCATION;
    }

    void setSummary(String summary) {
        this.summary = summary;
    }

    void setAdvice(String advice) {
        this.advice = advice;
    }

    void setRemovalDetails(String removalDetails) {
        this.removalDetails = removalDetails;
    }

    void setDocumentation(Documentation documentation) {
        this.documentation = documentation;
    }

    DeprecationMessage build() {
        return new DeprecationMessage(summary, removalDetails, advice, context, documentation, usageType);
    }

    public static class WithDocumentation {
        private final DeprecationMessageBuilder<?> builder;

        public WithDocumentation(DeprecationMessageBuilder<?> builder) {
            this.builder = builder;
        }

        public void nagUser() {
            DeprecationLogger.nagUserWith(builder, WithDocumentation.class);
        }
    }

    public static abstract class WithReplacement<T, SELF extends WithReplacement<T, SELF>> extends DeprecationMessageBuilder<SELF> {
        private final String subject;
        private T replacement;

        WithReplacement(String subject) {
            this.subject = subject;
        }

        @SuppressWarnings("unchecked")
        public SELF replaceWith(T replacement) {
            this.replacement = replacement;
            return (SELF) this;
        }

        String formatSubject() {
            return subject;
        }

        abstract String formatSummary(String subject);

{'fraction_non_alphanumeric': 0.07223812249570692, 'fraction_numerical': 0.0018317115054378936, 'mean_word_length': 3.370185092546273, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '7155805', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2182, 'n_words': 596}