Package org.forgerock.json.resource
Class PatchOperation
java.lang.Object
org.forgerock.json.resource.PatchOperation
An individual patch operation which is to be performed against a field within
a resource. This class defines four core types of operation. The core
operations are defined below and their behavior depends on the type of the
field being targeted by the operation:
- an object (Java
Map) or primitive (JavaString,Boolean, orNumber): these are considered to be single-valued fields - an array (Java
List): these are considered to be multi-valued fields exhibiting either:- list semantics - an ordered collection of potentially non-unique values, or
- set semantics - a collection of unique values whose ordering is implementation defined.
add- ensures that the targeted field contains the provided value(s). Missing parent fields will be created as needed. If the targeted field is already present and it is single-valued (i.e. not an array) then the existing value will be replaced. If the targeted field is already present and it is multi-valued (i.e. an array) then the behavior depends on whether the field is a list or a set:- list - the provided array of values will be appended to the existing list of values,
- set - the provided array of values will be merged with the existing set of values and duplicates removed.
remove- ensures that the targeted field does not contain the provided value(s) if present. If no values are provided with the remove operation then the entire field will be removed if it is present. If the remove operation targets a single-valued field and a patch value is provided then it must match the existing value for it to be removed, otherwise the field is left unchanged. If the remove operation targets a multi-valued field then the behavior depends on whether the field is a list or a set:- list - the provided array of values will be removed from the existing list of values. Each value in the remove operation will result in at most one value being removed from the existing list. In other words, if the existing list contains a pair of duplicate values and both of them need to be removed, then the values must be include twice in the remove operation,
- set - the provided array of values will be removed from the existing set of values.
replace- removes any existing value(s) of the targeted field and replaces them with the provided value(s). A replace operation is semantically equivalent to aremovefollowed by anadd, except that indexed updates are not permitted regardless of whether or not the field is a list.increment- increments or decrements the targeted numerical field value(s) by the specified amount. If the amount is negative then the value(s) are decremented. It is an error to attempt to increment a field which does not contain a number or an array of numbers. It is also an error if the patch value is not a single value.
NOTE: this class does not define how field values will be matched, nor does it define whether a resource supports indexed based modifications, nor whether fields are single or multi-valued. Instead these matters are the responsibility of the resource provider and, in particular, the JSON schema being enforced for the targeted resource.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe name of the field which contains the target field in the JSON representation.static final StringThe name of the source field for copy and move operations.static final StringThe name of the field which contains the type of patch operation in the JSON representation.static final StringThe name of the field which contains the operation value in the JSON representation.static final StringThe identifier used for "add" operations.static final StringThe identifier used for "copy" operations.static final StringThe identifier used for "increment" operations.static final StringThe identifier used for "move" operations.static final StringThe identifier used for "remove" operations.static final StringThe identifier used for "replace" operations.static final StringThe identifier used for "transform" operations. -
Method Summary
Modifier and TypeMethodDescriptionstatic PatchOperationCreates a new "add" patch operation which will add the provided value(s) to the specified field.static PatchOperationadd(JsonPointer field, Object value) Creates a new "add" patch operation which will add the provided value(s) to the specified field.static PatchOperationCreates a new "copy" patch operation which will copy the value found at `from` to `path`.static PatchOperationcopy(JsonPointer from, JsonPointer field) Creates a new "copy" patch operation which will copy the value found at `from` to `path`.static PatchOperationcopyOf(PatchOperation operation) Returns a deep copy of the provided patch operation.getField()Returns the field targeted by the patch operation.getFrom()Returns the source field for move and copy operations.Returns the type of patch operation to be performed.getValue()Returns the value for the patch operation.static PatchOperationCreates a new "increment" patch operation which will increment the value(s) of the specified field by the amount provided.static PatchOperationincrement(JsonPointer field, Number amount) Creates a new "increment" patch operation which will increment the value(s) of the specified field by the amount provided.booleanisAdd()Returnstrueif this is an "add" patch operation.booleanisCopy()Returnstrueif this is a "copy" patch operation.booleanReturnstrueif this is an "increment" patch operation.booleanisMove()Returnstrueif this is a "move" patch operation.booleanisRemove()Returnstrueif this is an "remove" patch operation.booleanReturnstrueif this is an "replace" patch operation.booleanReturnstrueif this is a "transform" patch operation.static PatchOperationCreates a new "move" patch operation which will move the value found at `from` to `path`.static PatchOperationmove(JsonPointer from, JsonPointer field) Creates a new "move" patch operation which will move the value found at `from` to `path`.static PatchOperationCreates a new patch operation having the specified operation type, field, and value(s).static PatchOperationoperation(String operation, JsonPointer field, Object value) Creates a new patch operation having the specified operation type, field, and value(s).static PatchOperationCreates a new "remove" patch operation which will remove the specified field.static PatchOperationCreates a new "remove" patch operation which will remove the provided value(s) from the specified field.static PatchOperationremove(JsonPointer field) Creates a new "remove" patch operation which will remove the specified field.static PatchOperationremove(JsonPointer field, Object value) Creates a new "remove" patch operation which will remove the provided value(s) from the specified field.static PatchOperationCreates a new "replace" patch operation which will replace the value(s) of the specified field with the provided value(s).static PatchOperationreplace(JsonPointer field, Object value) Creates a new "replace" patch operation which will replace the value(s) of the specified field with the provided value(s).Returns a JSON value representation of this patch operation.toString()static PatchOperationCreates a new "transform" patch operation which sets the value at field based on a transformation.static PatchOperationtransform(JsonPointer field, Object transform) Creates a new "transform" patch operation which sets the value at field based on a transformation.static PatchOperationParses the provided JSON content as a patch operation.static List<PatchOperation>valueOfList(JsonValue json) Parses the provided JSON content as a list of patch operations.
-
Field Details
-
FIELD_FIELD
The name of the field which contains the target field in the JSON representation.- See Also:
-
FIELD_FROM
The name of the source field for copy and move operations.- See Also:
-
FIELD_OPERATION
The name of the field which contains the type of patch operation in the JSON representation.- See Also:
-
FIELD_VALUE
The name of the field which contains the operation value in the JSON representation.- See Also:
-
OPERATION_ADD
The identifier used for "add" operations.- See Also:
-
OPERATION_INCREMENT
The identifier used for "increment" operations.- See Also:
-
OPERATION_REMOVE
The identifier used for "remove" operations.- See Also:
-
OPERATION_REPLACE
The identifier used for "replace" operations.- See Also:
-
OPERATION_MOVE
The identifier used for "move" operations.- See Also:
-
OPERATION_COPY
The identifier used for "copy" operations.- See Also:
-
OPERATION_TRANSFORM
The identifier used for "transform" operations. This is similar to an "add" or "replace" but the value may be treated as something other than a raw object.- See Also:
-
-
Method Details
-
add
Creates a new "add" patch operation which will add the provided value(s) to the specified field.- Parameters:
field- The field to be added.value- The new value(s) to be added, which may be aJsonValueor a JSON object, such as aString,Map, etc.- Returns:
- The new patch operation.
- Throws:
NullPointerException- If the value isnull.
-
add
Creates a new "add" patch operation which will add the provided value(s) to the specified field.- Parameters:
field- The field to be added.value- The new value(s) to be added, which may be aJsonValueor a JSON object, such as aString,Map, etc.- Returns:
- The new patch operation.
- Throws:
NullPointerException- If the value isnull.
-
increment
Creates a new "increment" patch operation which will increment the value(s) of the specified field by the amount provided.- Parameters:
field- The field to be incremented.amount- The amount to be added or removed (if negative) from the field's value(s).- Returns:
- The new patch operation.
- Throws:
NullPointerException- If the amount isnull.
-
increment
Creates a new "increment" patch operation which will increment the value(s) of the specified field by the amount provided.- Parameters:
field- The field to be incremented.amount- The amount to be added or removed (if negative) from the field's value(s).- Returns:
- The new patch operation.
- Throws:
NullPointerException- If the amount isnull.
-
remove
Creates a new "remove" patch operation which will remove the specified field.- Parameters:
field- The field to be removed.- Returns:
- The new patch operation.
-
remove
Creates a new "remove" patch operation which will remove the provided value(s) from the specified field.- Parameters:
field- The field to be removed.value- The value(s) to be removed, which may be aJsonValueor a JSON object, such as aString,Map, etc.- Returns:
- The new patch operation.
-
remove
Creates a new "remove" patch operation which will remove the specified field.- Parameters:
field- The field to be removed.- Returns:
- The new patch operation.
-
remove
Creates a new "remove" patch operation which will remove the provided value(s) from the specified field.- Parameters:
field- The field to be removed.value- The value(s) to be removed, which may be aJsonValueor a JSON object, such as aString,Map, etc.- Returns:
- The new patch operation.
-
replace
Creates a new "replace" patch operation which will replace the value(s) of the specified field with the provided value(s).- Parameters:
field- The field to be replaced.value- The new value(s) for the field, which may be aJsonValueor a JSON object, such as aString,Map, etc.- Returns:
- The new patch operation.
-
replace
Creates a new "replace" patch operation which will replace the value(s) of the specified field with the provided value(s).- Parameters:
field- The field to be replaced.value- The new value(s) for the field, which may be aJsonValueor a JSON object, such as aString,Map, etc.- Returns:
- The new patch operation.
-
move
Creates a new "move" patch operation which will move the value found at `from` to `path`.- Parameters:
from- The field to be moved.field- The destination path for the moved value- Returns:
- The new patch operation.
- Throws:
NullPointerException- If the from or path isnull.
-
move
Creates a new "move" patch operation which will move the value found at `from` to `path`.- Parameters:
from- The field to be moved.field- The destination path for the moved value- Returns:
- The new patch operation.
- Throws:
NullPointerException- If the from or path isnull.
-
copy
Creates a new "copy" patch operation which will copy the value found at `from` to `path`.- Parameters:
from- The field to be copied.field- The destination path for the copied value- Returns:
- The new patch operation.
- Throws:
NullPointerException- If the from or path isnull.
-
copy
Creates a new "copy" patch operation which will copy the value found at `from` to `path`.- Parameters:
from- The field to be copied.field- The destination path for the copied value- Returns:
- The new patch operation.
- Throws:
NullPointerException- If the from or path isnull.
-
transform
Creates a new "transform" patch operation which sets the value at field based on a transformation.- Parameters:
field- The field to be set.transform- The transform to be used to set the field value.- Returns:
- The new patch operation.
- Throws:
NullPointerException- If the transform isnull.
-
transform
Creates a new "transform" patch operation which sets the value at field based on a transformation.- Parameters:
field- The field to be set.transform- The transform to be used to set the field value.- Returns:
- The new patch operation.
- Throws:
NullPointerException- If the transform isnull.
-
operation
Creates a new patch operation having the specified operation type, field, and value(s).- Parameters:
operation- The type of patch operation to be performed.field- The field targeted by the patch operation.value- The possiblynullvalue for the patch operation, which may be aJsonValueor a JSON object, such as aString,Map, etc.- Returns:
- The new patch operation.
-
operation
Creates a new patch operation having the specified operation type, field, and value(s).- Parameters:
operation- The type of patch operation to be performed.field- The field targeted by the patch operation.value- The possiblynullvalue for the patch operation, which may be aJsonValueor a JSON object, such as aString,Map, etc.- Returns:
- The new patch operation.
-
copyOf
Returns a deep copy of the provided patch operation. This method may be used in cases where the immutability of the underlying JSON value cannot be guaranteed.- Parameters:
operation- The patch operation to be defensively copied.- Returns:
- A deep copy of the provided patch operation.
-
valueOf
Parses the provided JSON content as a patch operation.- Parameters:
json- The patch operation to be parsed.- Returns:
- The parsed patch operation.
- Throws:
BadRequestException- If the JSON value is not a JSON patch operation.
-
valueOfList
Parses the provided JSON content as a list of patch operations.- Parameters:
json- The list of patch operations to be parsed.- Returns:
- The list of parsed patch operations.
- Throws:
BadRequestException- If the JSON value is not a list of JSON patch operations.
-
getField
Returns the field targeted by the patch operation.- Returns:
- The field targeted by the patch operation.
-
getFrom
Returns the source field for move and copy operations.- Returns:
- The source field for move and copy operations.
-
getOperation
Returns the type of patch operation to be performed.- Returns:
- The type of patch operation to be performed.
-
getValue
Returns the value for the patch operation. The return value may be a JSON value whose value isnull.- Returns:
- The nullable value for the patch operation.
-
isAdd
public boolean isAdd()Returnstrueif this is an "add" patch operation.- Returns:
trueif this is an "add" patch operation.
-
isIncrement
public boolean isIncrement()Returnstrueif this is an "increment" patch operation.- Returns:
trueif this is an "increment" patch operation.
-
isRemove
public boolean isRemove()Returnstrueif this is an "remove" patch operation.- Returns:
trueif this is an "remove" patch operation.
-
isReplace
public boolean isReplace()Returnstrueif this is an "replace" patch operation.- Returns:
trueif this is an "replace" patch operation.
-
isMove
public boolean isMove()Returnstrueif this is a "move" patch operation.- Returns:
trueif this is a "move" patch operation.
-
isCopy
public boolean isCopy()Returnstrueif this is a "copy" patch operation.- Returns:
trueif this is a "copy" patch operation.
-
isTransform
public boolean isTransform()Returnstrueif this is a "transform" patch operation.- Returns:
trueif this is a "transform" patch operation.
-
toJsonValue
Returns a JSON value representation of this patch operation.- Returns:
- A JSON value representation of this patch operation.
-
toString
-