All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.slimgears.apt.data.EnumInfo Maven / Gradle / Ivy

There is a newer version: 0.7.58
Show newest version
package com.slimgears.apt.data;

import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.slimgears.apt.util.ElementUtils;

import javax.lang.model.element.TypeElement;
import java.util.Arrays;

@AutoValue
public abstract class EnumInfo implements HasType {
    public abstract ImmutableList constants();

    public static Builder builder() {
        return new AutoValue_EnumInfo.Builder();
    }

    @SuppressWarnings("unchecked")
    public static EnumInfo of(Class cls) {
        Preconditions.checkArgument(cls.isEnum());
        Builder builder = builder().type(cls);
        Arrays.asList(((Class)cls).getEnumConstants()).forEach(builder::enumValue);
        return builder.build();
    }

    public static EnumInfo of(TypeElement typeElement) {
        Preconditions.checkArgument(ElementUtils.isEnum(typeElement));
        Builder builder = builder().type(typeElement);
        typeElement.getEnclosedElements()
                .stream()
                .filter(ElementUtils::isEnumConstant)
                .map(Object::toString)
                .forEach(builder::enumValue);
        return builder.build();
    }

    @AutoValue.Builder
    public interface Builder extends InfoBuilder, HasType.Builder {
        ImmutableList.Builder constantsBuilder();

        default Builder enumValue(EnumValueInfo value) {
            constantsBuilder().add(value);
            return this;
        }

        default Builder enumValue(String value) {
            return enumValue(EnumValueInfo.of(value));
        }

        default Builder enumValue(Enum value) {
            return enumValue(EnumValueInfo.of(value));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy