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

io.micronaut.starter.feature.Features Maven / Gradle / Ivy

There is a newer version: 4.7.0
Show newest version
/*
 * Copyright 2017-2022 original 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
 *
 * https://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 io.micronaut.starter.feature;

import io.micronaut.starter.application.generator.GeneratorContext;
import io.micronaut.starter.feature.lang.LanguageFeature;
import io.micronaut.starter.feature.test.TestFeature;
import io.micronaut.starter.options.BuildTool;
import io.micronaut.starter.options.JdkVersion;
import io.micronaut.starter.options.Options;
import io.micronaut.starter.util.VersionInfo;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class Features extends ArrayList {

    private final Set featureList;
    private final BuildTool buildTool;
    private final GeneratorContext context;
    private ApplicationFeature applicationFeature;
    private LanguageFeature languageFeature;
    private TestFeature testFeature;
    private final JdkVersion javaVersion;

    public Features(GeneratorContext context, Set featureList, Options options) {
        super(featureList.stream().map(Feature::getName).collect(Collectors.toList()));
        this.featureList = featureList;
        this.context = context;
        for (Feature feature: featureList) {
            if (applicationFeature == null && feature instanceof ApplicationFeature applicationFeature1) {
                applicationFeature = applicationFeature1;
            }
            if (languageFeature == null && feature instanceof LanguageFeature languageFeature1) {
                languageFeature = languageFeature1;
            }
            if (testFeature == null && feature instanceof TestFeature testFeature1) {
                testFeature = testFeature1;
            }
        }
        this.javaVersion = options.getJavaVersion();
        this.buildTool = options.getBuildTool();
    }

    public boolean hasFeature(Class clazz) {
        return getFeatures().stream().anyMatch(clazz::isInstance);
    }

    public boolean hasMultiProjectFeature() {
        return getFeatures().stream().anyMatch(MultiProjectFeature.class::isInstance);
    }

    public BuildTool build() {
        return buildTool;
    }

    public ApplicationFeature application() {
        return applicationFeature;
    }

    public LanguageFeature language() {
        return languageFeature;
    }

    public TestFeature testFramework() {
        return testFeature;
    }

    public Set getFeatures() {
        return featureList;
    }

    public JdkVersion javaVersion() {
        return javaVersion;
    }

    /**
     * @return The main class
     */
    public Optional mainClass() {
        ApplicationFeature application = application();
        if (application != null && context != null) {
            return Optional.ofNullable(application.mainClassName(context));
        }
        return Optional.empty();
    }

    public String getTargetJdk(int max) {
        return VersionInfo.toJdkVersion(Math.min(javaVersion.majorVersion(), max));
    }

    public String getTargetJdk() {
        return VersionInfo.toJdkVersion(javaVersion.majorVersion());
    }

    public boolean isFeaturePresent(Class feature) {
        Objects.requireNonNull(feature, "The feature class cannot be null");
        return getFeatures().stream()
                .map(Feature::getClass)
                .anyMatch(feature::isAssignableFrom);
    }

    public  Optional getFeature(Class feature) {
        Objects.requireNonNull(feature, "The feature class cannot be null");
        for (Feature f : featureList) {
            if (feature.isInstance(f)) {
                return Optional.of((T) f);
            }
        }
        return Optional.empty();
    }

    public  T getRequiredFeature(Class feature) {
        Objects.requireNonNull(feature, "The feature class cannot be null");
        for (Feature f : featureList) {
            if (feature.isInstance(f)) {
                return (T) f;
            }
        }
        throw new IllegalStateException("The required feature type %s does not exist".formatted(feature.getName()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy