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.metadata.aggregated;
8
9 import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;
10
11 import java.util.Collections;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.Set;
15
16 import javax.validation.metadata.GroupConversionDescriptor;
17
18 import org.hibernate.validator.internal.metadata.descriptor.GroupConversionDescriptorImpl;
19 import org.hibernate.validator.internal.util.CollectionHelper;
20 import org.hibernate.validator.internal.util.stereotypes.Immutable;
21
22 /**
23  * Provides group conversion functionality to {@link org.hibernate.validator.cfg.context.Cascadable}s.
24  *
25  * @author Gunnar Morling
26  */

27 public class GroupConversionHelper {
28
29     static final GroupConversionHelper EMPTY = new GroupConversionHelper( Collections.emptyMap() );
30
31     @Immutable
32     private final Map<Class<?>, Class<?>> groupConversions;
33
34     private GroupConversionHelper(Map<Class<?>, Class<?>> groupConversions) {
35         this.groupConversions = CollectionHelper.toImmutableMap( groupConversions );
36     }
37
38     public static GroupConversionHelper of(Map<Class<?>, Class<?>> groupConversions) {
39         if ( groupConversions.isEmpty() ) {
40             return GroupConversionHelper.EMPTY;
41         }
42         else {
43             return new GroupConversionHelper( groupConversions );
44         }
45     }
46
47     /**
48      * Converts the given validation group as per the group conversion
49      * configuration for this property (as e.g. specified via
50      * {@code @ConvertGroup}.
51      *
52      * @param from The group to convert.
53      *
54      * @return The converted group. Will be the original group itself in case no
55      *         conversion is to be performed.
56      */

57     public Class<?> convertGroup(Class<?> from) {
58         Class<?> to = groupConversions.get( from );
59         return to != null ? to : from;
60     }
61
62     /**
63      * Returns a set with {@link GroupConversionDescriptor}s representing the
64      * underlying group conversions.
65      *
66      * @return A set with group conversion descriptors. May be empty, but never
67      *         {@code null}.
68      */

69     public Set<GroupConversionDescriptor> asDescriptors() {
70         Set<GroupConversionDescriptor> descriptors = newHashSet( groupConversions.size() );
71
72         for ( Entry<Class<?>, Class<?>> conversion : groupConversions.entrySet() ) {
73             descriptors.add(
74                     new GroupConversionDescriptorImpl(
75                             conversion.getKey(),
76                             conversion.getValue()
77                     )
78             );
79         }
80
81         return CollectionHelper.toImmutableSet( descriptors );
82     }
83
84     boolean isEmpty() {
85         return groupConversions.isEmpty();
86     }
87
88     @Override
89     public String toString() {
90         StringBuilder sb = new StringBuilder();
91         sb.append( getClass().getSimpleName() );
92         sb.append( " [" );
93         sb.append( "groupConversions=" ).append( groupConversions );
94         sb.append( "]" );
95         return sb.toString();
96     }
97 }
98