Skip to content

Commit 28e28c6

Browse files
committed
Improve javadocs wrt #2237
1 parent 5f1bb5a commit 28e28c6

File tree

1 file changed

+85
-8
lines changed

1 file changed

+85
-8
lines changed

src/main/java/com/fasterxml/jackson/databind/JsonNode.java

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -683,45 +683,122 @@ public boolean asBoolean(boolean defaultValue) {
683683
*/
684684

685685
/**
686+
* Method that may be called to verify that {@code this} node is NOT so-called
687+
* "missing node": that is, one for which {@link #isMissingNode()} returns {@code true}.
688+
* If not missing node, {@code this} is returned to allow chaining; otherwise
689+
* {@link IllegalArgumentException} is thrown.
690+
*
691+
* @return {@code this} node to allow chaining
692+
*
693+
* @throws IllegalArgumentException if this node is "missing node"
694+
*
686695
* @since 2.10
687696
*/
688-
public <T extends JsonNode> T require() {
697+
public <T extends JsonNode> T require() throws IllegalArgumentException {
689698
return _this();
690699
}
691700

692701
/**
702+
* Method that may be called to verify that {@code this} node is neither so-called
703+
* "missing node" (that is, one for which {@link #isMissingNode()} returns {@code true})
704+
* nor "null node" (one for which {@link #isNull()} returns {@code true}).
705+
* If non-null non-missing node, {@code this} is returned to allow chaining; otherwise
706+
* {@link IllegalArgumentException} is thrown.
707+
*
708+
* @return {@code this} node to allow chaining
709+
*
710+
* @throws IllegalArgumentException if this node is either "missing node" or "null node"
711+
*
693712
* @since 2.10
694713
*/
695-
public <T extends JsonNode> T requireNonNull() {
714+
public <T extends JsonNode> T requireNonNull() throws IllegalArgumentException {
696715
return _this();
697716
}
698717

699718
/**
719+
* Method is functionally equivalent to
720+
*{@code
721+
* path(fieldName).required()
722+
*}
723+
* and can be used to check that this node is an {@code ObjectNode} (that is, represents
724+
* JSON Object value) and has value for specified property with key {@code fieldName}
725+
* (but note that value may be explicit JSON null value).
726+
* If this node is Object Node and has value for specified property, {@code this} is returned
727+
* to allow chaining; otherwise {@link IllegalArgumentException} is thrown.
728+
*
729+
* @return {@code this} node to allow chaining
730+
*
731+
* @throws IllegalArgumentException if this node is not an Object node or if it does not
732+
* have value for specified property
733+
*
700734
* @since 2.10
701735
*/
702-
public JsonNode required(String fieldName) {
736+
public JsonNode required(String fieldName) throws IllegalArgumentException {
703737
return _reportRequiredViolation("Node of type `%s` has no fields", getClass().getName());
704738
}
705739

706740
/**
741+
* Method is functionally equivalent to
742+
*{@code
743+
* path(index).required()
744+
*}
745+
* and can be used to check that this node is an {@code ArrayNode} (that is, represents
746+
* JSON Array value) and has value for specified {@code index}
747+
* (but note that value may be explicit JSON null value).
748+
* If this node is Array Node and has value for specified index, {@code this} is returned
749+
* to allow chaining; otherwise {@link IllegalArgumentException} is thrown.
750+
*
751+
* @return {@code this} node to allow chaining
752+
*
753+
* @throws IllegalArgumentException if this node is not an Array node or if it does not
754+
* have value for specified index
755+
*
707756
* @since 2.10
708757
*/
709-
public JsonNode required(int index) {
758+
public JsonNode required(int index) throws IllegalArgumentException {
710759
return _reportRequiredViolation("Node of type `%s` has no indexed values", getClass().getName());
711760
}
712761

713762
/**
763+
* Method is functionally equivalent to
764+
*{@code
765+
* at(pathExpr).required()
766+
*}
767+
* and can be used to check that there is an actual value node at specified {@link JsonPointer}
768+
* starting from {@code this} node
769+
* (but note that value may be explicit JSON null value).
770+
* If such value node exists {@code this} is returned
771+
* to allow chaining; otherwise {@link IllegalArgumentException} is thrown.
772+
*
773+
* @return {@code this} node to allow chaining
774+
*
775+
* @throws IllegalArgumentException if no value node exists at given {@code JSON Pointer} path
776+
*
714777
* @since 2.10
715778
*/
716-
public JsonNode requiredAt(String pathExpr) {
779+
public JsonNode requiredAt(String pathExpr) throws IllegalArgumentException {
717780
return requiredAt(JsonPointer.compile(pathExpr));
718781
}
719782

720783
/**
784+
* Method is functionally equivalent to
785+
*{@code
786+
* at(path).required()
787+
*}
788+
* and can be used to check that there is an actual value node at specified {@link JsonPointer}
789+
* starting from {@code this} node
790+
* (but note that value may be explicit JSON null value).
791+
* If such value node exists {@code this} is returned
792+
* to allow chaining; otherwise {@link IllegalArgumentException} is thrown.
793+
*
794+
* @return {@code this} node to allow chaining
795+
*
796+
* @throws IllegalArgumentException if no value node exists at given {@code JSON Pointer} path
797+
*
721798
* @since 2.10
722799
*/
723-
public final JsonNode requiredAt(final JsonPointer pathExpr) {
724-
JsonPointer currentExpr = pathExpr;
800+
public final JsonNode requiredAt(final JsonPointer path) throws IllegalArgumentException {
801+
JsonPointer currentExpr = path;
725802
JsonNode curr = this;
726803

727804
// Note: copied from `at()`
@@ -732,7 +809,7 @@ public final JsonNode requiredAt(final JsonPointer pathExpr) {
732809
curr = curr._at(currentExpr);
733810
if (curr == null) {
734811
_reportRequiredViolation("No node at '%s' (unmatched part: '%s')",
735-
pathExpr, currentExpr);
812+
path, currentExpr);
736813
}
737814
currentExpr = currentExpr.tail();
738815
}

0 commit comments

Comments
 (0)