View Javadoc
1   package org.nuiton.validator.bean.simple;
2   /*
3    * #%L
4    * Nuiton Validator
5    * %%
6    * Copyright (C) 2013 - 2014 Code Lutin, Tony Chemit
7    * %%
8    * This program is free software: you can redistribute it and/or modify
9    * it under the terms of the GNU Lesser General Public License as 
10   * published by the Free Software Foundation, either version 3 of the 
11   * License, or (at your option) any later version.
12   * 
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Lesser Public License for more details.
17   * 
18   * You should have received a copy of the GNU General Lesser Public 
19   * License along with this program.  If not, see
20   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
21   * #L%
22   */
23  
24  import org.nuiton.validator.NuitonValidatorScope;
25  
26  import java.util.EnumMap;
27  import java.util.EnumSet;
28  import java.util.List;
29  
30  /**
31   * Useful methods arond {@link SimpleBeanValidator}.
32   *
33   * @author Tony Chemit - chemit@codelutin.com
34   * @since 2.5.4
35   */
36  public class SimpleBeanValidators {
37  
38      protected SimpleBeanValidators() {
39          // no constructor on helper class
40      }
41  
42      public static EnumSet<NuitonValidatorScope> getScopes(
43              List<SimpleBeanValidatorMessage<?>> messages) {
44          EnumSet<NuitonValidatorScope> result =
45                  EnumSet.noneOf(NuitonValidatorScope.class);
46          for (SimpleBeanValidatorMessage<?> m : messages) {
47              result.add(m.getScope());
48          }
49          return result;
50      }
51  
52      public static EnumMap<NuitonValidatorScope, Integer> getScopesCount(
53              List<SimpleBeanValidatorMessage<?>> messages) {
54          EnumMap<NuitonValidatorScope, Integer> result =
55                  new EnumMap<NuitonValidatorScope, Integer>(NuitonValidatorScope.class);
56          for (NuitonValidatorScope s : NuitonValidatorScope.values()) {
57              result.put(s, 0);
58          }
59          for (SimpleBeanValidatorMessage<?> m : messages) {
60  
61              NuitonValidatorScope scope = m.getScope();
62  
63              result.put(scope, result.get(scope) + 1);
64          }
65  
66          for (NuitonValidatorScope s : NuitonValidatorScope.values()) {
67              if (result.get(s) == 0) {
68                  result.remove(s);
69              }
70          }
71          return result;
72      }
73  }