1 package com.fasterxml.jackson.annotation;
2
3 /**
4 * Optional Boolean value ("nullean"). Needed just because Java annotations
5 * can not take 'null' as a value (even as default), so there is no
6 * way to distinguish between explicit `true` and `false`, and lack of
7 * choice (related: annotations are limited to primitives, so
8 * {@link java.lang.Boolean} not allowed as solution).
9 *<p>
10 * Note: although use of `true` and `false` would be more convenient, they
11 * can not be chosen since they are Java keyword and compiler won't allow
12 * the choice. And since enum naming convention suggests all-upper-case,
13 * that is what is done here.
14 *
15 * @since 2.6
16 */
17 public enum OptBoolean
18 {
19 /**
20 * Value that indicates that the annotation property is explicitly defined to
21 * be enabled, or true.
22 */
23 TRUE,
24
25 /**
26 * Value that indicates that the annotation property is explicitly defined to
27 * be disabled, or false.
28 */
29 FALSE,
30
31 /**
32 * Value that indicates that the annotation property does NOT have an explicit
33 * definition of enabled/disabled (or true/false); instead, a higher-level
34 * configuration value is used; or lacking higher-level global setting,
35 * default.
36 */
37 DEFAULT;
38
39 public Boolean asBoolean() {
40 if (this == DEFAULT) return null;
41 return (this == TRUE) ? Boolean.TRUE : Boolean.FALSE;
42 }
43
44 public boolean asPrimitive() {
45 return (this == TRUE);
46 }
47
48 public static OptBoolean fromBoolean(Boolean b) {
49 if (b == null) {
50 return DEFAULT;
51 }
52 return b.booleanValue() ? TRUE : FALSE;
53 }
54
55 public static boolean equals(Boolean b1, Boolean b2) {
56 if (b1 == null) {
57 return (b2 == null);
58 }
59 return b1.equals(b2);
60 }
61 }
62