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

step.core.yaml.YamlModelUtils Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (C) 2020, exense GmbH
 *
 * This file is part of STEP
 *
 * STEP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * STEP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with STEP.  If not, see .
 ******************************************************************************/
package step.core.yaml;

import step.core.scanner.CachedAnnotationScanner;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class YamlModelUtils {

    public static  List> scanYamlModels(Class applicableClass) {
        return CachedAnnotationScanner.getClassesWithAnnotation(YamlModel.LOCATION, YamlModel.class, Thread.currentThread().getContextClassLoader())
                .stream()
                .filter(applicableClass::isAssignableFrom)
                .map(c -> (Class) c)
                .sorted(Comparator.comparing(Class::getSimpleName))
                .collect(Collectors.toList());
    }

    public static  List> scanNamedYamlModels(Class applicableClass) {
        return scanYamlModels(applicableClass).stream().filter(c -> c.getAnnotation(YamlModel.class).named()).collect(Collectors.toList());
    }

    public static String getEntityNameByClass(Class yamlModelClass) {
        boolean annotationPresent = yamlModelClass.isAnnotationPresent(YamlModel.class);
        String nameFromAnnotation = null;
        if (annotationPresent) {
            if(!yamlModelClass.getAnnotation(YamlModel.class).named()){
                return null;
            }
            nameFromAnnotation = yamlModelClass.getAnnotation(YamlModel.class).name();
        }

        if (nameFromAnnotation == null || nameFromAnnotation.isEmpty()) {
            return yamlModelClass.getSimpleName();
        } else {
            return nameFromAnnotation;
        }
    }

    public static  Class getClassByEntityName(String entityName, Collection> candidates) {
        for (Class candidate : candidates) {
            if (Objects.equals(entityName, getEntityNameByClass(candidate))) {
                return candidate;
            }
        }
        return null;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy