1
1
package org .everit .json .schema ;
2
2
3
- import static java .util .Collections .unmodifiableMap ;
3
+ import org .everit .json .schema .internal .JSONPrinter ;
4
+ import org .json .JSONWriter ;
4
5
5
6
import java .io .StringWriter ;
6
7
import java .util .HashMap ;
8
+ import java .util .List ;
7
9
import java .util .Map ;
8
10
import java .util .Objects ;
9
11
10
- import org .everit .json .schema .internal .JSONPrinter ;
11
- import org .json .JSONWriter ;
12
+ import static java .util .Collections .unmodifiableMap ;
12
13
13
14
/**
14
15
* Superclass of all other schema validator classes of this package.
15
16
*/
16
17
public abstract class Schema {
17
18
18
- /**
19
- * Abstract builder class for the builder classes of {@code Schema} subclasses. This builder is
20
- * used to load the generic properties of all types of schemas like {@code title} or
21
- * {@code description}.
22
- *
23
- * @param <S>
24
- * the type of the schema being built by the builder subclass.
25
- */
26
- public abstract static class Builder <S extends Schema > {
27
-
28
- private String title ;
29
-
30
- private String description ;
31
-
32
- private String id ;
33
-
34
- private SchemaLocation schemaLocation ;
35
-
36
- private Object defaultValue ;
37
-
38
- private Boolean nullable = null ;
39
-
40
- private Boolean readOnly = null ;
41
-
42
- private Boolean writeOnly = null ;
43
-
44
- public Map <String , Object > unprocessedProperties = new HashMap <>(0 );
45
-
46
- public Builder <S > title (String title ) {
47
- this .title = title ;
48
- return this ;
49
- }
50
-
51
- public Builder <S > description (String description ) {
52
- this .description = description ;
53
- return this ;
54
- }
55
-
56
- public Builder <S > id (String id ) {
57
- this .id = id ;
58
- return this ;
59
- }
60
-
61
- /**
62
- * @deprecated Use {@link #schemaLocation(SchemaLocation)} instead.
63
- */
64
- @ Deprecated
65
- public Builder <S > schemaLocation (String schemaLocation ) {
66
- return schemaLocation (SchemaLocation .parseURI (schemaLocation ));
67
- }
68
-
69
- public Builder <S > schemaLocation (SchemaLocation location ) {
70
- this .schemaLocation = location ;
71
- return this ;
72
- }
73
-
74
- public Builder <S > defaultValue (Object defaultValue ) {
75
- this .defaultValue = defaultValue ;
76
- return this ;
77
- }
78
-
79
- public Builder <S > nullable (Boolean nullable ) {
80
- this .nullable = nullable ;
81
- return this ;
82
- }
83
-
84
- public Builder <S > readOnly (Boolean readOnly ) {
85
- this .readOnly = readOnly ;
86
- return this ;
87
- }
88
-
89
- public Builder <S > writeOnly (Boolean writeOnly ) {
90
- this .writeOnly = writeOnly ;
91
- return this ;
92
- }
93
-
94
- public Builder <S > unprocessedProperties (Map <String , Object > unprocessedProperties ) {
95
- this .unprocessedProperties = unprocessedProperties ;
96
- return this ;
97
- }
98
-
99
- public abstract S build ();
100
-
101
- }
19
+ private final String examples ;
102
20
103
21
private final String title ;
104
22
@@ -113,14 +31,6 @@ public Builder<S> unprocessedProperties(Map<String, Object> unprocessedPropertie
113
31
114
32
private final Object defaultValue ;
115
33
116
- private final Boolean nullable ;
117
-
118
- private final Boolean readOnly ;
119
-
120
- private final Boolean writeOnly ;
121
-
122
- private final Map <String , Object > unprocessedProperties ;
123
-
124
34
/**
125
35
* Constructor.
126
36
*
@@ -134,12 +44,43 @@ protected Schema(Builder<?> builder) {
134
44
this .schemaLocation = builder .schemaLocation == null ? null : builder .schemaLocation .toString ();
135
45
this .location = builder .schemaLocation ;
136
46
this .defaultValue = builder .defaultValue ;
47
+ this .examples = builder .examples ;
137
48
this .nullable = builder .nullable ;
138
49
this .readOnly = builder .readOnly ;
139
50
this .writeOnly = builder .writeOnly ;
140
51
this .unprocessedProperties = new HashMap <>(builder .unprocessedProperties );
141
52
}
142
53
54
+ private final Boolean nullable ;
55
+
56
+ private final Boolean readOnly ;
57
+
58
+ private final Boolean writeOnly ;
59
+
60
+ private final Map <String , Object > unprocessedProperties ;
61
+
62
+ @ Override
63
+ public boolean equals (Object o ) {
64
+ if (this == o ) {
65
+ return true ;
66
+ }
67
+ if (o instanceof Schema ) {
68
+ Schema schema = (Schema ) o ;
69
+ return schema .canEqual (this ) &&
70
+ Objects .equals (title , schema .title ) &&
71
+ Objects .equals (defaultValue , schema .defaultValue ) &&
72
+ Objects .equals (examples , schema .examples ) &&
73
+ Objects .equals (description , schema .description ) &&
74
+ Objects .equals (id , schema .id ) &&
75
+ Objects .equals (nullable , schema .nullable ) &&
76
+ Objects .equals (readOnly , schema .readOnly ) &&
77
+ Objects .equals (writeOnly , schema .writeOnly ) &&
78
+ Objects .equals (unprocessedProperties , schema .unprocessedProperties );
79
+ } else {
80
+ return false ;
81
+ }
82
+ }
83
+
143
84
/**
144
85
* Performs the schema validation.
145
86
*
@@ -199,29 +140,13 @@ public boolean definesProperty(String field) {
199
140
}
200
141
201
142
@ Override
202
- public boolean equals (Object o ) {
203
- if (this == o ) {
204
- return true ;
205
- }
206
- if (o instanceof Schema ) {
207
- Schema schema = (Schema ) o ;
208
- return schema .canEqual (this ) &&
209
- Objects .equals (title , schema .title ) &&
210
- Objects .equals (defaultValue , schema .defaultValue ) &&
211
- Objects .equals (description , schema .description ) &&
212
- Objects .equals (id , schema .id ) &&
213
- Objects .equals (nullable , schema .nullable ) &&
214
- Objects .equals (readOnly , schema .readOnly ) &&
215
- Objects .equals (writeOnly , schema .writeOnly ) &&
216
- Objects .equals (unprocessedProperties , schema .unprocessedProperties );
217
- } else {
218
- return false ;
219
- }
143
+ public int hashCode () {
144
+ return Objects .hash (title , description , id , defaultValue , examples , nullable , readOnly , writeOnly ,
145
+ unprocessedProperties );
220
146
}
221
147
222
- @ Override
223
- public int hashCode () {
224
- return Objects .hash (title , description , id , defaultValue , nullable , readOnly , writeOnly , unprocessedProperties );
148
+ public String getExamples () {
149
+ return this .examples ;
225
150
}
226
151
227
152
public String getTitle () {
@@ -248,6 +173,103 @@ public Object getDefaultValue() {
248
173
return this .defaultValue ;
249
174
}
250
175
176
+ /**
177
+ * Abstract builder class for the builder classes of {@code Schema} subclasses. This builder is
178
+ * used to load the generic properties of all types of schemas like {@code title} or
179
+ * {@code description}.
180
+ *
181
+ * @param <S>
182
+ * the type of the schema being built by the builder subclass.
183
+ */
184
+ public abstract static class Builder <S extends Schema > {
185
+
186
+ private String title ;
187
+
188
+ private String description ;
189
+
190
+ private String id ;
191
+
192
+ private SchemaLocation schemaLocation ;
193
+
194
+ private Object defaultValue ;
195
+
196
+ private String examples ;
197
+
198
+ private Boolean nullable = null ;
199
+
200
+ private Boolean readOnly = null ;
201
+
202
+ private Boolean writeOnly = null ;
203
+
204
+ public Map <String , Object > unprocessedProperties = new HashMap <>(0 );
205
+
206
+ public Builder <S > title (String title ) {
207
+ this .title = title ;
208
+ return this ;
209
+ }
210
+
211
+ public Builder <S > description (String description ) {
212
+ this .description = description ;
213
+ return this ;
214
+ }
215
+
216
+ public Builder <S > id (String id ) {
217
+ this .id = id ;
218
+ return this ;
219
+ }
220
+
221
+ /**
222
+ * @deprecated Use {@link #schemaLocation(SchemaLocation)} instead.
223
+ */
224
+ @ Deprecated
225
+ public Builder <S > schemaLocation (String schemaLocation ) {
226
+ return schemaLocation (SchemaLocation .parseURI (schemaLocation ));
227
+ }
228
+
229
+ public Builder <S > schemaLocation (SchemaLocation location ) {
230
+ this .schemaLocation = location ;
231
+ return this ;
232
+ }
233
+
234
+ public Builder <S > defaultValue (Object defaultValue ) {
235
+ this .defaultValue = defaultValue ;
236
+ return this ;
237
+ }
238
+
239
+ public Builder <S > examples (List <String > examples ) {
240
+ StringBuilder exampleBuilder = new StringBuilder ("[" );
241
+ for (String example : examples ) {
242
+ exampleBuilder .append (example );
243
+ }
244
+ exampleBuilder .append ("]" );
245
+ this .examples = examples .toString ();
246
+ return this ;
247
+ }
248
+
249
+ public Builder <S > nullable (Boolean nullable ) {
250
+ this .nullable = nullable ;
251
+ return this ;
252
+ }
253
+
254
+ public Builder <S > readOnly (Boolean readOnly ) {
255
+ this .readOnly = readOnly ;
256
+ return this ;
257
+ }
258
+
259
+ public Builder <S > writeOnly (Boolean writeOnly ) {
260
+ this .writeOnly = writeOnly ;
261
+ return this ;
262
+ }
263
+
264
+ public Builder <S > unprocessedProperties (Map <String , Object > unprocessedProperties ) {
265
+ this .unprocessedProperties = unprocessedProperties ;
266
+ return this ;
267
+ }
268
+
269
+ public abstract S build ();
270
+
271
+ }
272
+
251
273
public boolean hasDefaultValue () {
252
274
return this .defaultValue != null ;
253
275
}
0 commit comments