MATSIM
BeanValidationConfigConsistencyChecker.java
Go to the documentation of this file.
1 /*
2  * *********************************************************************** *
3  * project: org.matsim.*
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2019 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** *
19  */
20 
21 package org.matsim.core.config.consistency;
22 
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28 
29 import jakarta.validation.ConstraintViolation;
30 import jakarta.validation.ConstraintViolationException;
31 import jakarta.validation.Validation;
32 import jakarta.validation.Validator;
33 
34 import org.matsim.core.config.Config;
36 
41  private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
42 
43  @Override
44  public void checkConsistency(Config config) {
45  Set<ConstraintViolation<ConfigGroup>> violations = new HashSet<>();
46  List<String> messages = new ArrayList<>();
47 
48  for (ConfigGroup group : config.getModules().values()) {
49  Set<ConstraintViolation<ConfigGroup>> groupViolations = validator.validate(group);
50  violations.addAll(groupViolations);
51  for (ConstraintViolation<ConfigGroup> v : groupViolations) {
52  messages.add((messages.size() + 1)
53  + ") "
54  + group.getClass().getName()
55  + "(name="
56  + group.getName()
57  + ")."
58  + v.getPropertyPath()
59  + ": "
60  + v.getMessage());
61  }
62  }
63 
64  if (!violations.isEmpty()) {
65  String message = messages.size() + " error(s) found in the config:\n" + messages.stream()
66  .collect(Collectors.joining("\n"));
67  throw new ConstraintViolationException(message, violations);
68  }
69  }
70 }
final TreeMap< String, ConfigGroup > getModules()
Definition: Config.java:288