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 aremove
followed 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
Modifier and TypeFieldDescriptionstatic final String
The name of the field which contains the target field in the JSON representation.static final String
The name of the source field for copy and move operations.static final String
The name of the field which contains the type of patch operation in the JSON representation.static final String
The name of the field which contains the operation value in the JSON representation.static final String
The identifier used for "add" operations.static final String
The identifier used for "copy" operations.static final String
The identifier used for "increment" operations.static final String
The identifier used for "move" operations.static final String
The identifier used for "remove" operations.static final String
The identifier used for "replace" operations.static final String
The identifier used for "transform" operations. -
Method Summary
Modifier and TypeMethodDescriptionstatic PatchOperation
Creates a new "add" patch operation which will add the provided value(s) to the specified field.static PatchOperation
add
(JsonPointer field, Object value) Creates a new "add" patch operation which will add the provided value(s) to the specified field.static PatchOperation
Creates a new "copy" patch operation which will copy the value found at `from` to `path`.static PatchOperation
copy
(JsonPointer from, JsonPointer field) Creates a new "copy" patch operation which will copy the value found at `from` to `path`.static PatchOperation
copyOf
(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 PatchOperation
Creates a new "increment" patch operation which will increment the value(s) of the specified field by the amount provided.static PatchOperation
increment
(JsonPointer field, Number amount) Creates a new "increment" patch operation which will increment the value(s) of the specified field by the amount provided.boolean
isAdd()
Returnstrue
if this is an "add" patch operation.boolean
isCopy()
Returnstrue
if this is a "copy" patch operation.boolean
Returnstrue
if this is an "increment" patch operation.boolean
isMove()
Returnstrue
if this is a "move" patch operation.boolean
isRemove()
Returnstrue
if this is an "remove" patch operation.boolean
Returnstrue
if this is an "replace" patch operation.boolean
Returnstrue
if this is a "transform" patch operation.static PatchOperation
Creates a new "move" patch operation which will move the value found at `from` to `path`.static PatchOperation
move
(JsonPointer from, JsonPointer field) Creates a new "move" patch operation which will move the value found at `from` to `path`.static PatchOperation
Creates a new patch operation having the specified operation type, field, and value(s).static PatchOperation
operation
(String operation, JsonPointer field, Object value) Creates a new patch operation having the specified operation type, field, and value(s).static PatchOperation
Creates a new "remove" patch operation which will remove the specified field.static PatchOperation
Creates a new "remove" patch operation which will remove the provided value(s) from the specified field.static PatchOperation
remove
(JsonPointer field) Creates a new "remove" patch operation which will remove the specified field.static PatchOperation
remove
(JsonPointer field, Object value) Creates a new "remove" patch operation which will remove the provided value(s) from the specified field.static PatchOperation
Creates a new "replace" patch operation which will replace the value(s) of the specified field with the provided value(s).static PatchOperation
replace
(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 PatchOperation
Creates a new "transform" patch operation which sets the value at field based on a transformation.static PatchOperation
transform
(JsonPointer field, Object transform) Creates a new "transform" patch operation which sets the value at field based on a transformation.static PatchOperation
Parses 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 aJsonValue
or 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 aJsonValue
or 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 aJsonValue
or 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 aJsonValue
or 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 aJsonValue
or 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 aJsonValue
or 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 possiblynull
value for the patch operation, which may be aJsonValue
or 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 possiblynull
value for the patch operation, which may be aJsonValue
or 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()Returnstrue
if this is an "add" patch operation.- Returns:
true
if this is an "add" patch operation.
-
isIncrement
public boolean isIncrement()Returnstrue
if this is an "increment" patch operation.- Returns:
true
if this is an "increment" patch operation.
-
isRemove
public boolean isRemove()Returnstrue
if this is an "remove" patch operation.- Returns:
true
if this is an "remove" patch operation.
-
isReplace
public boolean isReplace()Returnstrue
if this is an "replace" patch operation.- Returns:
true
if this is an "replace" patch operation.
-
isMove
public boolean isMove()Returnstrue
if this is a "move" patch operation.- Returns:
true
if this is a "move" patch operation.
-
isCopy
public boolean isCopy()Returnstrue
if this is a "copy" patch operation.- Returns:
true
if this is a "copy" patch operation.
-
isTransform
public boolean isTransform()Returnstrue
if this is a "transform" patch operation.- Returns:
true
if 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
-