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.containsWhitespace;
20  import static org.forgerock.api.util.ValidationUtil.isEmpty;
21  
22  import java.util.Objects;
23  
24  import org.forgerock.api.ApiValidationException;
25  
26  import com.fasterxml.jackson.annotation.JsonCreator;
27  import com.fasterxml.jackson.annotation.JsonValue;
28  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
29  
30  /**
31   * Class that represents the Reference type in API descriptor.
32   */
33  @JsonDeserialize(builder = Reference.Builder.class)
34  public final class Reference {
35  
36      private final String value;
37  
38      private Reference(Builder builder) {
39          this.value = builder.value;
40  
41          if (isEmpty(value)) {
42              throw new ApiValidationException("Reference-value is required");
43          }
44          if (containsWhitespace(value)) {
45              throw new ApiValidationException("Reference-value may not contain whitespace");
46          }
47      }
48  
49      /**
50       * Getter of the JSON reference.
51       *
52       * @return value
53       */
54      @JsonValue
55      public String getValue() {
56          return value;
57      }
58  
59      @Override
60      public boolean equals(Object o) {
61          if (this == o) {
62              return true;
63          }
64          if (o == null || getClass() != o.getClass()) {
65              return false;
66          }
67          Reference reference = (Reference) o;
68          return Objects.equals(value, reference.value);
69      }
70  
71      @Override
72      public int hashCode() {
73          return Objects.hash(value);
74      }
75  
76      /**
77       * Create a new Builder for Reference.
78       *
79       * @return Builder
80       */
81      public static Builder reference() {
82          return new Builder();
83      }
84  
85      /**
86       * Builder to help construct the Reference.
87       */
88      public static final class Builder {
89  
90          private String value;
91  
92          private Builder() {
93          }
94  
95          @JsonCreator
96          private Builder(String value) {
97              this.value = value;
98          }
99  
100         /**
101          * Setter for Reference-value.
102          *
103          * @param value Reference-value
104          * @return Builder
105          */
106         @JsonCreator
107         public Builder value(String value) {
108             this.value = value;
109             return this;
110         }
111 
112         /**
113          * Builds the Reference instance.
114          *
115          * @return Reference instance
116          */
117         public Reference build() {
118             return new Reference(this);
119         }
120     }
121 
122 }