1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.api.models;
18
19 import static org.forgerock.api.util.ValidationUtil.isEmpty;
20
21 import java.util.Arrays;
22 import java.util.Objects;
23
24 import com.fasterxml.jackson.annotation.JsonProperty;
25 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
26 import org.forgerock.api.ApiValidationException;
27 import org.forgerock.api.enums.PatchOperation;
28
29
30
31
32 @JsonDeserialize(builder = Patch.Builder.class)
33 public final class Patch extends Operation {
34
35 private final PatchOperation[] operations;
36
37
38
39
40
41
42 private Patch(Builder builder) {
43 super(builder);
44 this.operations = builder.operations;
45
46 if (isEmpty(operations)) {
47 throw new ApiValidationException("operations required");
48 }
49 }
50
51
52
53
54
55
56 public PatchOperation[] getOperations() {
57 return operations;
58 }
59
60 @Override
61 public boolean equals(Object o) {
62 if (this == o) {
63 return true;
64 }
65 if (o == null || getClass() != o.getClass()) {
66 return false;
67 }
68 if (!super.equals(o)) {
69 return false;
70 }
71 Patch patch = (Patch) o;
72 return Arrays.equals(operations, patch.operations);
73 }
74
75 @Override
76 public int hashCode() {
77 return Objects.hash(super.hashCode(), operations);
78 }
79
80
81
82
83
84
85 public static final Builder patch() {
86 return new Builder();
87 }
88
89
90
91
92
93
94 @Override
95 protected void allocateToResource(Resource.Builder resourceBuilder) {
96 resourceBuilder.patch(this);
97 }
98
99
100
101
102
103
104
105
106 public static Patch fromAnnotation(org.forgerock.api.annotations.Patch patch, ApiDescription descriptor,
107 Class<?> relativeType) {
108 return patch()
109 .detailsFromAnnotation(patch.operationDescription(), descriptor, relativeType)
110 .operations(patch.operations())
111 .build();
112 }
113
114
115
116
117 public static final class Builder extends Operation.Builder<Builder> {
118
119 private PatchOperation[] operations;
120
121 private Builder() {
122 super();
123 }
124
125
126
127
128
129
130
131 @JsonProperty("operations")
132 public Builder operations(PatchOperation... operations) {
133 this.operations = operations;
134 return this;
135 }
136
137 @Override
138 protected Builder self() {
139 return this;
140 }
141
142
143
144
145
146
147 public Patch build() {
148 return new Patch(this);
149 }
150 }
151
152 }