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   * Portions Copyright 2018 Wren Security
16   */
17  
18  package org.forgerock.api.models;
19  
20  import static org.forgerock.api.util.ValidationUtil.*;
21  import static org.forgerock.util.Reject.*;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Objects;
26  import java.util.Set;
27  
28  import com.fasterxml.jackson.annotation.JsonAnySetter;
29  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
30  
31  import com.fasterxml.jackson.annotation.JsonIgnore;
32  import com.fasterxml.jackson.annotation.JsonValue;
33  
34  /**
35   * Class that represents API descriptor {@link Schema} definitions.
36   */
37  @JsonDeserialize(builder = Definitions.Builder.class)
38  public final class Definitions {
39  
40      private final Map<String, Schema> definitions;
41  
42      private Definitions(Builder builder) {
43          this.definitions = builder.definitions;
44      }
45  
46      /**
47       * Gets a {@code Map} of schema-names to {@link Schema}s. This method is currently only used for JSON serialization.
48       *
49       * @return {@code Map} of schema-names to {@link Schema}s.
50       */
51      @JsonValue
52      protected Map<String, Schema> getDefinitions() {
53          return definitions;
54      }
55  
56      /**
57       * Gets the {@link Schema} for a given Schema-name.
58       *
59       * @param name Schema name
60       * @return {@link Schema} or {@code null} if does-not-exist.
61       */
62      @JsonIgnore
63      public Schema get(String name) {
64          return definitions.get(name);
65      }
66  
67      /**
68       * Returns all {@link Schema} names.
69       *
70       * @return All {@link Schema} names.
71       */
72      @JsonIgnore
73      public Set<String> getNames() {
74          return definitions.keySet();
75      }
76  
77      /**
78       * Create a new Builder for Definitions.
79       *
80       * @return Builder
81       */
82      public static Builder definitions() {
83          return new Builder();
84      }
85  
86      @Override
87      public boolean equals(Object o) {
88          if (this == o) {
89              return true;
90          }
91          if (o == null || getClass() != o.getClass()) {
92              return false;
93          }
94          Definitions that = (Definitions) o;
95          return Objects.equals(definitions, that.definitions);
96      }
97  
98      @Override
99      public int hashCode() {
100         return Objects.hash(definitions);
101     }
102 
103     /**
104      * Builder to help construct the Definitions.
105      */
106     public static final class Builder {
107 
108         private final Map<String, Schema> definitions = new HashMap<>();
109 
110         /**
111          * Private default constructor.
112          */
113         private Builder() {
114         }
115 
116         /**
117          * Adds a {@link Schema}.
118          *
119          * @param name Schema name
120          * @param schema {@link Schema}
121          * @return Builder
122          */
123         @JsonAnySetter
124         public Builder put(String name, Schema schema) {
125             if (isEmpty(name) || containsWhitespace(name)) {
126                 throw new IllegalArgumentException(
127                     "Schema name is required, must not be blank, and must not contain " +
128                     "whitespace; given: '" + name + "'");
129             }
130             if (definitions.containsKey(name) && !definitions.get(name).equals(schema)) {
131                 throw new IllegalStateException(
132                     "Schema name already exists but Schema objects are not equal; " +
133                     "given: '" + name + "'");
134             }
135 
136             definitions.put(name, checkNotNull(schema));
137             return this;
138         }
139 
140         /**
141          * Builds the Definitions instance.
142          *
143          * @return Definitions instance
144          */
145         public Definitions build() {
146             return new Definitions(this);
147         }
148     }
149 
150 }