5
5
/**
6
6
* Helper class used for storing and accessing per-call attributes.
7
7
* Storage is two-layered: at higher precedence, we have actual per-call
8
+ * ({@code ObjectMapper.readValue} or {@code ObjectMapper.writeValue})
8
9
* attributes; and at lower precedence, default attributes that may be
9
10
* defined for Object readers and writers.
10
11
*<p>
15
16
* sharing, by creating new copies instead of modifying state.
16
17
* This allows sharing of default values without per-call copying, but
17
18
* requires two-level lookup on access.
19
+ *<p>
20
+ * To set default attributes, use {@link #withSharedAttributes(Map)}
21
+ * or {@link #withSharedAttribute(Object, Object)}, starting with
22
+ * "empty" instance (see {@link #getEmpty()}). For example:
23
+ *<pre>
24
+ * ContextAttributes attrs = ContextAttributes.getEmpty()
25
+ * .withSharedAttribute("foo", "bar")
26
+ * .withSharedAttribute("attr2", "value2");
27
+ *</pre>
18
28
*
19
29
* @since 2.3
20
30
*/
21
31
public abstract class ContextAttributes
22
32
{
33
+ /**
34
+ * Accessor for an empty instance of {@link ContextAttributes}: usable
35
+ * as-is, or as a starting point for building up a set of default attributes.
36
+ */
23
37
public static ContextAttributes getEmpty () {
24
38
return Impl .getEmpty ();
25
39
}
@@ -30,8 +44,27 @@ public static ContextAttributes getEmpty() {
30
44
/**********************************************************
31
45
*/
32
46
47
+ /**
48
+ * Fluent factory method for creating a new instance with an additional
49
+ * shared attribute.
50
+ *
51
+ * @param key Name of the attribute to add
52
+ * @param value Value of the attribute to add; may be null, in which case
53
+ * attribute will be removed if it already exists.
54
+ *
55
+ * @return New instance of {@link ContextAttributes} that has specified change.
56
+ */
33
57
public abstract ContextAttributes withSharedAttribute (Object key , Object value );
34
58
59
+ /**
60
+ * Fluent factory method for creating a new instance with specified set of
61
+ * shared attributes.
62
+ * Any shared attributes that already exist will be replaced
63
+ *
64
+ * @param attributes Map of shared attributes to add, replacing any existing ones.
65
+ *
66
+ * @return New instance of {@link ContextAttributes} that has specified shared attributes.
67
+ */
35
68
public abstract ContextAttributes withSharedAttributes (Map <?,?> attributes );
36
69
37
70
public abstract ContextAttributes withoutSharedAttribute (Object key );
@@ -115,7 +148,7 @@ public ContextAttributes withSharedAttribute(Object key, Object value)
115
148
Map <Object ,Object > m ;
116
149
// need to cover one special case, since EMPTY uses Immutable map:
117
150
if (this == EMPTY ) {
118
- m = new HashMap <Object , Object >(8 );
151
+ m = new HashMap <>(8 );
119
152
} else {
120
153
m = _copy (_shared );
121
154
}
0 commit comments