<commit_before>package org.hildan.livedoc.core.readers.annotation;

import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

import org.hildan.livedoc.core.annotations.types.ApiType;
import org.hildan.livedoc.core.model.doc.types.ApiPropertyDoc;
import org.hildan.livedoc.core.model.doc.types.ApiTypeDoc;
import org.hildan.livedoc.core.scanners.properties.Property;
import org.hildan.livedoc.core.scanners.properties.PropertyScanner;
import org.hildan.livedoc.core.scanners.templates.TemplateProvider;
import org.hildan.livedoc.core.scanners.types.references.TypeReferenceProvider;
import org.hildan.livedoc.core.util.BeanUtils;

import static org.hildan.livedoc.core.readers.annotation.ApiDocReader.nullifyIfEmpty;

public class ApiTypeDocReader {

    private final PropertyScanner propertyScanner;

    public ApiTypeDocReader(PropertyScanner propertyScanner) {
        this.propertyScanner = propertyScanner;
    }

    public ApiTypeDoc read(Class<?> clazz, TypeReferenceProvider typeReferenceProvider,
            TemplateProvider templateProvider) {
        ApiTypeDoc apiTypeDoc = new ApiTypeDoc(clazz);
        apiTypeDoc.setName(clazz.getSimpleName());
        apiTypeDoc.setSupportedVersions(ApiVersionDocReader.read(clazz));
        apiTypeDoc.setShow(Modifier.isAbstract(clazz.getModifiers()));
        apiTypeDoc.setStage(ApiStageReader.read(clazz));

        ApiType apiType = clazz.getAnnotation(ApiType.class);
        if (apiType != null) {
            apiTypeDoc.setName(BeanUtils.maybeOverridden(nullifyIfEmpty(apiType.name()), clazz.getSimpleName()));
            apiTypeDoc.setDescription(nullifyIfEmpty(apiType.description()));
            apiTypeDoc.setGroup(apiType.group());
            apiTypeDoc.setShow(apiType.show());
        }

        if (clazz.isEnum()) {
            apiTypeDoc.setAllowedValues(BeanUtils.enumConstantsToStringArray(clazz.getEnumConstants()));
        }

        apiTypeDoc.setFields(getFieldDocs(clazz, apiTypeDoc, typeReferenceProvider));
        apiTypeDoc.setTemplate(templateProvider.getTemplate(clazz));

        return apiTypeDoc;
    }

    private Set<ApiPropertyDoc> getFieldDocs(Class<?> clazz, ApiTypeDoc apiTypeDoc,
            TypeReferenceProvider typeReferenceProvider) {
        if (clazz.isEnum()) {
            return Collections.emptySet();
        }
        Set<ApiPropertyDoc> fieldDocs = new TreeSet<>();
        for (Property property : propertyScanner.getProperties(clazz)) {
            fieldDocs.add(ApiPropertyDocReader.read(property, apiTypeDoc, typeReferenceProvider));
        }
        return fieldDocs;
    }
}<commit_msg>Add support for javadoc type description<commit_after>package org.hildan.livedoc.core.readers.annotation;

import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

import org.hildan.livedoc.core.annotations.types.ApiType;
import org.hildan.livedoc.core.model.doc.types.ApiPropertyDoc;
import org.hildan.livedoc.core.model.doc.types.ApiTypeDoc;
import org.hildan.livedoc.core.readers.javadoc.JavadocHelper;
import org.hildan.livedoc.core.scanners.properties.Property;
import org.hildan.livedoc.core.scanners.properties.PropertyScanner;
import org.hildan.livedoc.core.scanners.templates.TemplateProvider;
import org.hildan.livedoc.core.scanners.types.references.TypeReferenceProvider;
import org.hildan.livedoc.core.util.BeanUtils;

import static org.hildan.livedoc.core.readers.annotation.ApiDocReader.nullifyIfEmpty;

public class ApiTypeDocReader {

    private final PropertyScanner propertyScanner;

    public ApiTypeDocReader(PropertyScanner propertyScanner) {
        this.propertyScanner = propertyScanner;
    }

    public ApiTypeDoc read(Class<?> clazz, TypeReferenceProvider typeReferenceProvider,
            TemplateProvider templateProvider) {
        String javadoc = JavadocHelper.getJavadocDescription(clazz).orElse(null);
        ApiTypeDoc apiTypeDoc = new ApiTypeDoc(clazz);
        apiTypeDoc.setName(clazz.getSimpleName());
        apiTypeDoc.setDescription(javadoc);
        apiTypeDoc.setSupportedVersions(ApiVersionDocReader.read(clazz));
        apiTypeDoc.setShow(Modifier.isAbstract(clazz.getModifiers()));
        apiTypeDoc.setStage(ApiStageReader.read(clazz));

        ApiType apiType = clazz.getAnnotation(ApiType.class);
        if (apiType != null) {
            apiTypeDoc.setName(BeanUtils.maybeOverridden(nullifyIfEmpty(apiType.name()), clazz.getSimpleName()));
            apiTypeDoc.setDescription(BeanUtils.maybeOverridden(nullifyIfEmpty(apiType.description()), javadoc));
            apiTypeDoc.setGroup(apiType.group());
            apiTypeDoc.setShow(apiType.show());
        }

        if (clazz.isEnum()) {
            apiTypeDoc.setAllowedValues(BeanUtils.enumConstantsToStringArray(clazz.getEnumConstants()));
        }

        apiTypeDoc.setFields(getFieldDocs(clazz, apiTypeDoc, typeReferenceProvider));
        apiTypeDoc.setTemplate(templateProvider.getTemplate(clazz));

        return apiTypeDoc;
    }

    private Set<ApiPropertyDoc> getFieldDocs(Class<?> clazz, ApiTypeDoc apiTypeDoc,
            TypeReferenceProvider typeReferenceProvider) {
        if (clazz.isEnum()) {
            return Collections.emptySet();
        }
        Set<ApiPropertyDoc> fieldDocs = new TreeSet<>();
        for (Property property : propertyScanner.getProperties(clazz)) {
            fieldDocs.add(ApiPropertyDocReader.read(property, apiTypeDoc, typeReferenceProvider));
        }
        return fieldDocs;
    }
}
{'fraction_non_alphanumeric': 0.102384794692487, 'fraction_numerical': 0.0, 'mean_word_length': 4.016187050359712, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '424142', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1639, 'n_words': 216}