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 import com.fasterxml.jackson.annotation.JsonAnySetter;
23 import com.fasterxml.jackson.annotation.JsonIgnore;
24 import com.fasterxml.jackson.annotation.JsonValue;
25 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
26
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.Set;
30 import java.util.TreeMap;
31
32
33
34
35 @JsonDeserialize(builder = Services.Builder.class)
36 public final class Services {
37
38 private final Map<String, Resource> services;
39
40 private Services(Builder builder) {
41 this.services = builder.services;
42 }
43
44
45
46
47
48
49
50 @JsonValue
51 protected Map<String, Resource> getServices() {
52 return services;
53 }
54
55
56
57
58
59
60
61 @JsonIgnore
62 public Resource get(String name) {
63 return services.get(name);
64 }
65
66
67
68
69
70
71 @JsonIgnore
72 public Set<String> getNames() {
73 return services.keySet();
74 }
75
76
77
78
79
80
81
82 public static Builder services() {
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 Services services1 = (Services) o;
95 return Objects.equals(services, services1.services);
96 }
97
98 @Override
99 public int hashCode() {
100 return Objects.hash(services);
101 }
102
103
104
105
106 public static final class Builder {
107
108 private final Map<String, Resource> services = new TreeMap<>();
109
110
111
112
113 private Builder() {
114 }
115
116
117
118
119
120
121
122
123 @JsonAnySetter
124 public Builder put(String name, Resource resource) {
125 if (isEmpty(name) || containsWhitespace(name)) {
126 throw new IllegalArgumentException(
127 "Resource name is required, must not be blank, and must not contain " +
128 "whitespace; given: '" + name + "'");
129 }
130 if (services.containsKey(name) && !services.get(name).equals(resource)) {
131 throw new IllegalStateException(
132 "Resource name already exists but Resource objects are not equal; " +
133 "given: '" + name + "'");
134 }
135
136 services.put(name, checkNotNull(resource));
137 return this;
138 }
139
140
141
142
143
144
145 public Services build() {
146 return new Services(this);
147 }
148 }
149
150
151 }