55/**
66 * Helper class used for storing and accessing per-call attributes.
77 * Storage is two-layered: at higher precedence, we have actual per-call
8+ * ({@code ObjectMapper.readValue} or {@code ObjectMapper.writeValue})
89 * attributes; and at lower precedence, default attributes that may be
910 * defined for Object readers and writers.
1011 *<p>
1516 * sharing, by creating new copies instead of modifying state.
1617 * This allows sharing of default values without per-call copying, but
1718 * 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>
1828 *
1929 * @since 2.3
2030 */
2131public abstract class ContextAttributes
2232{
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+ */
2337 public static ContextAttributes getEmpty () {
2438 return Impl .getEmpty ();
2539 }
@@ -30,8 +44,27 @@ public static ContextAttributes getEmpty() {
3044 /**********************************************************
3145 */
3246
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+ */
3357 public abstract ContextAttributes withSharedAttribute (Object key , Object value );
3458
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+ */
3568 public abstract ContextAttributes withSharedAttributes (Map <?,?> attributes );
3669
3770 public abstract ContextAttributes withoutSharedAttribute (Object key );
@@ -115,7 +148,7 @@ public ContextAttributes withSharedAttribute(Object key, Object value)
115148 Map <Object ,Object > m ;
116149 // need to cover one special case, since EMPTY uses Immutable map:
117150 if (this == EMPTY ) {
118- m = new HashMap <Object , Object >(8 );
151+ m = new HashMap <>(8 );
119152 } else {
120153 m = _copy (_shared );
121154 }
0 commit comments