View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.joran.sanity;
15  
16  import ch.qos.logback.core.model.Model;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  /**
22   * Interface for sanity checking Models.
23   * @since 1.3.2/1.4.2
24   * @author ceki
25   */
26  public interface SanityChecker {
27  
28      public void check(Model model);
29  
30      default void deepFindAllModelsOfType(Class<? extends Model> modelClass, List<Model> modelList, Model model) {
31          if (modelClass.isInstance(model)) {
32              modelList.add(model);
33          }
34  
35          for (Model m : model.getSubModels()) {
36              deepFindAllModelsOfType(modelClass, modelList, m);
37          }
38      }
39  
40      default List<Pair<Model, Model>> deepFindNestedSubModelsOfType(Class<? extends Model> modelClass, List<? extends Model> parentList) {
41  
42          List<Pair<Model, Model>> nestingPairs = new ArrayList<>();
43  
44          for (Model parent : parentList) {
45              List<Model> nestedElements = new ArrayList<>();
46              parent.getSubModels().stream().forEach(m -> deepFindAllModelsOfType(modelClass, nestedElements, m));
47              nestedElements.forEach(n -> nestingPairs.add(new Pair(parent, n)));
48          }
49          return nestingPairs;
50      }
51  }