1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.api.models;
18
19 import static org.forgerock.api.util.ValidationUtil.*;
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Objects;
24 import java.util.Set;
25
26 import com.fasterxml.jackson.annotation.JsonAnySetter;
27 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
28 import org.forgerock.api.util.PathUtil;
29 import org.forgerock.util.Reject;
30
31 import com.fasterxml.jackson.annotation.JsonIgnore;
32 import com.fasterxml.jackson.annotation.JsonValue;
33
34
35
36
37 @JsonDeserialize(builder = SubResources.Builder.class)
38 public final class SubResources {
39 private final Map<String, Resource> subResources;
40
41 private SubResources(Builder builder) {
42 this.subResources = builder.subResources;
43 }
44
45
46
47
48
49
50 @JsonValue
51 public Map<String, Resource> getSubResources() {
52 return subResources;
53 }
54
55
56
57
58
59
60
61 @JsonIgnore
62 public Resource get(String name) {
63 return subResources.get(name);
64 }
65
66
67
68
69
70
71 @JsonIgnore
72 public Set<String> getNames() {
73 return subResources.keySet();
74 }
75
76 @Override
77 public boolean equals(Object o) {
78 if (this == o) {
79 return true;
80 }
81 if (o == null || getClass() != o.getClass()) {
82 return false;
83 }
84 SubResources that = (SubResources) o;
85 return Objects.equals(subResources, that.subResources);
86 }
87
88 @Override
89 public int hashCode() {
90 return Objects.hash(subResources);
91 }
92
93
94
95
96
97
98 public static Builder subresources() {
99 return new Builder();
100 }
101
102
103
104
105 public static final class Builder {
106
107 private final Map<String, Resource> subResources = new HashMap<>();
108
109
110
111
112 private Builder() {
113 }
114
115
116
117
118
119
120
121
122 @JsonAnySetter
123 public Builder put(String path, Resource resource) {
124 if (path == null || containsWhitespace(path)) {
125 throw new IllegalArgumentException("path required and may not contain whitespace");
126 }
127 if (!path.isEmpty()) {
128
129 path = PathUtil.buildPath(path);
130 }
131 if (subResources.containsKey(path)) {
132 throw new IllegalStateException("path not unique");
133 }
134 subResources.put(path, Reject.checkNotNull(resource));
135 return this;
136 }
137
138
139
140
141
142
143 public SubResources build() {
144 return new SubResources(this);
145 }
146 }
147
148 }