1 /*
2  * Hibernate Validator, declare and validate application constraints
3  *
4  * License: Apache License, Version 2.0
5  * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6  */

7 package org.hibernate.validator.internal.engine.groups;
8
9 import java.util.Collections;
10 import java.util.Iterator;
11 import java.util.List;
12
13 import javax.validation.GroupDefinitionException;
14
15 /**
16  * Interface defining the methods needed to execute groups and sequences in the right order.
17  *
18  * @author Hardy Ferentschik
19  * @author Guillaume Smet
20  */

21 public interface ValidationOrder {
22
23     Iterator<Group> getGroupIterator();
24
25     Iterator<Sequence> getSequenceIterator();
26
27     /**
28      * Asserts that the default group sequence of the validated bean can be expanded into the sequences which needs to
29      * be validated.
30      *
31      * @param defaultGroupSequence the default group sequence of the bean currently validated
32      *
33      * @throws GroupDefinitionException in case {@code defaultGroupSequence} cannot be expanded into one of the group sequences
34      * which need to be validated
35      */

36     void assertDefaultGroupSequenceIsExpandable(List<Class<?>> defaultGroupSequence)
37             throws GroupDefinitionException;
38
39     /**
40      * A {@link org.hibernate.validator.internal.engine.groups.ValidationOrder} which contains a single group, {@code Default}.
41      */

42     ValidationOrder DEFAULT_GROUP = new DefaultGroupValidationOrder();
43
44     /**
45      * A {@link org.hibernate.validator.internal.engine.groups.ValidationOrder} which contains a single sequence which
46      * in turn contains a single group, {@code Default}.
47      */

48     ValidationOrder DEFAULT_SEQUENCE = new DefaultSequenceValidationOrder();
49
50     class DefaultSequenceValidationOrder implements ValidationOrder {
51
52         private final List<Sequence> defaultSequences;
53
54         private DefaultSequenceValidationOrder() {
55             defaultSequences = Collections.singletonList( Sequence.DEFAULT );
56         }
57
58         @Override
59         public Iterator<Group> getGroupIterator() {
60             return Collections.<Group>emptyIterator();
61         }
62
63         @Override
64         public Iterator<Sequence> getSequenceIterator() {
65             return defaultSequences.iterator();
66         }
67
68         @Override
69         public void assertDefaultGroupSequenceIsExpandable(List<Class<?>> defaultGroupSequence) throws GroupDefinitionException {
70         }
71     }
72
73     class DefaultGroupValidationOrder implements ValidationOrder {
74
75         private final List<Group> defaultGroups;
76
77         private DefaultGroupValidationOrder() {
78             defaultGroups = Collections.singletonList( Group.DEFAULT_GROUP );
79         }
80
81         @Override
82         public Iterator<Group> getGroupIterator() {
83             return defaultGroups.iterator();
84         }
85
86         @Override
87         public Iterator<Sequence> getSequenceIterator() {
88             return Collections.<Sequence>emptyIterator();
89         }
90
91         @Override
92         public void assertDefaultGroupSequenceIsExpandable(List<Class<?>> defaultGroupSequence) throws GroupDefinitionException {
93         }
94     }
95 }
96