@@ -44,23 +44,64 @@ public class JSONPointer {
4444 public static class Builder {
4545
4646 private final List <String > refTokens = new ArrayList <String >();
47+
48+ /**
49+ * Creates a {@code JSONPointer} instance using the tokens previously set using the
50+ * {@link #append(String)} method calls.
51+ */
52+ public JSONPointer build () {
53+ return new JSONPointer (refTokens );
54+ }
4755
56+ /**
57+ * Adds an arbitary token to the list of reference tokens. It can be any non-null value.
58+ *
59+ * Unlike in the case of JSON string or URI fragment representation of JSON pointers, the
60+ * argument of this method MUST NOT be escaped. If you want to query the property called
61+ * {@code "a~b"} then you should simply pass the {@code "a~b"} string as-is, there is no
62+ * need to escape it as {@code "a~0b"}.
63+ *
64+ * @param token the new token to be appended to the list
65+ * @return {@code this}
66+ * @throws NullPointerException if {@code token} is null
67+ */
4868 public Builder append (String token ) {
69+ if (token == null ) {
70+ throw new NullPointerException ("token cannot be null" );
71+ }
4972 refTokens .add (token );
5073 return this ;
5174 }
5275
53- public JSONPointer build () {
54- return new JSONPointer (refTokens );
55- }
56-
76+ /**
77+ * Adds an integer to the reference token list. Although not necessarily, mostly this token will
78+ * denote an array index.
79+ *
80+ * @param arrayIndex the array index to be added to the token list
81+ * @return {@code this}
82+ */
5783 public Builder append (int arrayIndex ) {
5884 refTokens .add (String .valueOf (arrayIndex ));
5985 return this ;
6086 }
6187
6288 }
6389
90+ /**
91+ * Static factory method for {@link Builder}. Example usage:
92+ *
93+ * <pre><code>
94+ * JSONPointer pointer = JSONPointer.builder()
95+ * .append("obj")
96+ * .append("other~key").append("another/key")
97+ * .append("\"")
98+ * .append(0)
99+ * .build();
100+ * </code></pre>
101+ *
102+ * @return a builder instance which can be used to construct a {@code JSONPointer} instance by chained
103+ * {@link Builder#append(String)} calls.
104+ */
64105 public static Builder builder () {
65106 return new Builder ();
66107 }
0 commit comments