View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2016 ForgeRock AS.
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   * Class that represents the Patch operation type in API descriptor.
31   */
32  @JsonDeserialize(builder = Patch.Builder.class)
33  public final class Patch extends Operation {
34  
35      private final PatchOperation[] operations;
36  
37      /**
38       * Protected contstructor of the Patch operation.
39       *
40       * @param builder Patch Builder
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       * Getter for supported Patch operations.
53       *
54       * @return Supported Patch operations
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       * Creates a new builder for Patch.
82       *
83       * @return New builder instance
84       */
85      public static final Builder patch() {
86          return new Builder();
87      }
88  
89      /**
90       * Allocates the Patch operation type to the given Resource Builder.
91       *
92       * @param resourceBuilder - Resource Builder to add the operation
93       */
94      @Override
95      protected void allocateToResource(Resource.Builder resourceBuilder) {
96          resourceBuilder.patch(this);
97      }
98  
99      /**
100      * Builds a Patch object from the data stored in the annotation.
101      * @param patch Patch annotation that holds the data
102      * @param descriptor The root descriptor to add definitions to.
103      * @param relativeType The type relative to which schema resources should be resolved.
104      * @return Patch instance
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      * Builder to help construct the Patch.
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          * Setter for supported Patch-operations.
127          *
128          * @param operations Supported Patch-operations
129          * @return Builder
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          * Builds the Patch instance.
144          *
145          * @return Patch instance
146          */
147         public Patch build() {
148             return new Patch(this);
149         }
150     }
151 
152 }