forked from mapstruct/mapstruct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueMapping.java
More file actions
134 lines (130 loc) · 4.42 KB
/
Copy pathValueMapping.java
File metadata and controls
134 lines (130 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Configures the mapping of source constant value to target constant value.
* <p>
* Supported mappings are
* <ol>
* <li>Enumeration to Enumeration</li>
* </ol>
* <p>
* <B>Example 1:</B>
*
* <pre>
* <code>
*
* public enum OrderType { RETAIL, B2B, C2C, EXTRA, STANDARD, NORMAL }
*
* public enum ExternalOrderType { RETAIL, B2B, SPECIAL, DEFAULT }
*
* @ValueMapping(target = "SPECIAL", source = "EXTRA"),
* @ValueMapping(target = "DEFAULT", source = "STANDARD"),
* @ValueMapping(target = "DEFAULT", source = "NORMAL")
* ExternalOrderType orderTypeToExternalOrderType(OrderType orderType);
* </code>
* Mapping result:
* +---------------------+----------------------------+
* | OrderType | ExternalOrderType |
* +---------------------+----------------------------+
* | null | null |
* | OrderType.EXTRA | ExternalOrderType.SPECIAL |
* | OrderType.STANDARD | ExternalOrderType.DEFAULT |
* | OrderType.NORMAL | ExternalOrderType.DEFAULT |
* | OrderType.RETAIL | ExternalOrderType.RETAIL |
* | OrderType.B2B | ExternalOrderType.B2B |
* +---------------------+----------------------------+
* </pre>
*
* <p>
* <B>Example 2:</B>
*
* <pre>
* <code>
*
* @ValueMapping( source = MappingConstants.NULL, target = "DEFAULT" ),
* @ValueMapping( source = "STANDARD", target = MappingConstants.NULL ),
* @ValueMapping( source = MappingConstants.ANY_REMAINING, target = "SPECIAL" )
* ExternalOrderType orderTypeToExternalOrderType(OrderType orderType);
* </code>
* Mapping result:
* +---------------------+----------------------------+
* | OrderType | ExternalOrderType |
* +---------------------+----------------------------+
* | null | ExternalOrderType.DEFAULT |
* | OrderType.STANDARD | null |
* | OrderType.RETAIL | ExternalOrderType.RETAIL |
* | OrderType.B2B | ExternalOrderType.B2B |
* | OrderType.NORMAL | ExternalOrderType.SPECIAL |
* | OrderType.EXTRA | ExternalOrderType.SPECIAL |
* +---------------------+----------------------------+
* </pre>
*
* <p>
* <B>Example 3:</B>
* </p>
*
* <p></p>MapStruct will <B>WARN</B> on incomplete mappings. However, if for some reason no match is found, an
* {@link java.lang.IllegalStateException} will be thrown. This compile-time error can be avoided by
* using {@link MappingConstants#THROW_EXCEPTION} for {@link ValueMapping#target()}. It will result an
* {@link java.lang.IllegalArgumentException} at runtime.
* <pre>
* <code>
*
* @ValueMapping( source = "STANDARD", target = "DEFAULT" ),
* @ValueMapping( source = "C2C", target = MappingConstants.THROW_EXCEPTION )
* ExternalOrderType orderTypeToExternalOrderType(OrderType orderType);
* </code>
* Mapping result:
* {@link java.lang.IllegalArgumentException} with the error message:
* Unexpected enum constant: C2C
* </pre>
*
* @author Sjaak Derksen
*/
@Repeatable(ValueMappings.class)
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface ValueMapping {
/**
* The source value constant to use for this mapping.
*
* <p>
* <b>Valid values:</b>
* <ol>
* <li>enum constant name</li>
* <li>{@link MappingConstants#NULL}</li>
* <li>{@link MappingConstants#ANY_REMAINING}</li>
* <li>{@link MappingConstants#ANY_UNMAPPED}</li>
* </ol>
* <p>
* <b>NOTE:</b>When using <ANY_REMAINING>, MapStruct will perform the normal name based mapping, in which
* source is mapped to target based on enum identifier equality. Using <ANY_UNMAPPED> will not apply name
* based mapping.
*
* @return The source value.
*/
String source();
/**
* The target value constant to use for this mapping.
*
* <p>
* <b>Valid values:</b>
* <ol>
* <li>enum constant name</li>
* <li>{@link MappingConstants#NULL}</li>
* <li>{@link MappingConstants#THROW_EXCEPTION}</li>
* </ol>
*
* @return The target value.
*/
String target();
}