001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2022, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.joran.sanity;
015
016import ch.qos.logback.core.model.Model;
017
018import java.util.ArrayList;
019import java.util.List;
020
021/**
022 * Interface for sanity checking Models.
023 * @since 1.3.2/1.4.2
024 * @author ceki
025 */
026public interface SanityChecker {
027
028    public void check(Model model);
029
030    default void deepFindAllModelsOfType(Class<? extends Model> modelClass, List<Model> modelList, Model model) {
031        if (modelClass.isInstance(model)) {
032            modelList.add(model);
033        }
034
035        for (Model m : model.getSubModels()) {
036            deepFindAllModelsOfType(modelClass, modelList, m);
037        }
038    }
039
040    default List<Pair<Model, Model>> deepFindNestedSubModelsOfType(Class<? extends Model> modelClass, List<? extends Model> parentList) {
041
042        List<Pair<Model, Model>> nestingPairs = new ArrayList<>();
043
044        for (Model parent : parentList) {
045            List<Model> nestedElements = new ArrayList<>();
046            parent.getSubModels().stream().forEach(m -> deepFindAllModelsOfType(modelClass, nestedElements, m));
047            nestedElements.forEach(n -> nestingPairs.add(new Pair(parent, n)));
048        }
049        return nestingPairs;
050    }
051}