1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
48
49
50
51 @JsonValue
52 protected Map<String, Schema> getDefinitions() {
53 return definitions;
54 }
55
56
57
58
59
60
61
62 @JsonIgnore
63 public Schema get(String name) {
64 return definitions.get(name);
65 }
66
67
68
69
70
71
72 @JsonIgnore
73 public Set<String> getNames() {
74 return definitions.keySet();
75 }
76
77
78
79
80
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
105
106 public static final class Builder {
107
108 private final Map<String, Schema> definitions = new HashMap<>();
109
110
111
112
113 private Builder() {
114 }
115
116
117
118
119
120
121
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
142
143
144
145 public Definitions build() {
146 return new Definitions(this);
147 }
148 }
149
150 }