• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 
17 package software.amazon.awssdk.core.document;
18 
19 import java.io.Serializable;
20 import java.math.BigDecimal;
21 import java.math.BigInteger;
22 import java.text.ParseException;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.function.Consumer;
26 import software.amazon.awssdk.annotations.Immutable;
27 import software.amazon.awssdk.annotations.SdkPublicApi;
28 import software.amazon.awssdk.core.SdkNumber;
29 import software.amazon.awssdk.core.document.internal.BooleanDocument;
30 import software.amazon.awssdk.core.document.internal.ListDocument;
31 import software.amazon.awssdk.core.document.internal.MapDocument;
32 import software.amazon.awssdk.core.document.internal.NullDocument;
33 import software.amazon.awssdk.core.document.internal.NumberDocument;
34 import software.amazon.awssdk.core.document.internal.StringDocument;
35 
36 /**
37  * Interface for Document Types.
38  * Document types are used to carry open content that is Data with no fixed schema, data that can't be modeled using rigid types,
39  * or data that has a schema that evolves outside the purview of a service
40  * without requiring techniques like embedding JSON inside JSON strings.
41  * Document type value is serialized using the same format as its surroundings and requires no additional encoding or escaping.
42  * This interface specifies all the methods to access a Document, also provides constructor methods for
43  * instantiating Document.
44  *
45  */
46 @SdkPublicApi
47 @Immutable
48 public interface Document extends Serializable {
49 
50 
51     /**
52      * Create {@link Document} from a string, using the provided String.
53      * @param string String value.
54      * @return Implementation of Document that stores a String.
55      */
fromString(String string)56     static Document fromString(String string) {
57         return new StringDocument(string);
58     }
59 
60     /**
61      * Create {@link Document} from a boolean.
62      * @param booleanValue Boolean value.
63      * @return Implementation of Document that stores a Boolean.
64      */
fromBoolean(boolean booleanValue)65     static Document fromBoolean(boolean booleanValue) {
66         return new BooleanDocument(booleanValue);
67     }
68 
69     /**
70      * Create {@link Document} from a {@link SdkNumber}.
71      * @param number {@link SdkNumber} sdkNumber with the given precision type.
72      * @return Implementation of Document that stores a {@link SdkNumber}.
73      */
fromNumber(SdkNumber number)74     static Document fromNumber(SdkNumber number) {
75         return new NumberDocument(number);
76     }
77 
78 
79     /**
80      * Create {@link Document} from a int.
81      * @param number int type number.
82      * @return Implementation of Document that stores a {@link SdkNumber} constructed {@link SdkNumber#fromInteger(int)}.
83      */
fromNumber(int number)84     static Document fromNumber(int number) {
85         return new NumberDocument(SdkNumber.fromInteger(number));
86     }
87 
88     /**
89      * Create {@link Document} from a long.
90      * @param number long type number.
91      * @return Implementation of Document that stores a {@link SdkNumber} constructed {@link SdkNumber#longValue()}.
92      */
fromNumber(long number)93     static Document fromNumber(long number) {
94         return new NumberDocument(SdkNumber.fromLong(number));
95     }
96 
97     /**
98      * Create {@link Document} from a float.
99      * @param number float type number.
100      * @return Implementation of Document that stores a {@link SdkNumber} constructed {@link SdkNumber#floatValue()}
101      */
fromNumber(float number)102     static Document fromNumber(float number) {
103         return new NumberDocument(SdkNumber.fromFloat(number));
104     }
105 
106     /**
107      * Create {@link Document} from a double.
108      * @param number double type number.
109      * @return Implementation of Document that stores a {@link SdkNumber} constructed {@link SdkNumber#fromDouble(double)}
110      */
fromNumber(double number)111     static Document fromNumber(double number) {
112         return new NumberDocument(SdkNumber.fromDouble(number));
113     }
114 
115     /**
116      * Create {@link Document} from a BigDecimal.
117      * @param number BigDecimal type number.
118      * @return Implementation of Document that stores a {@link SdkNumber} constructed {@link SdkNumber#fromBigDecimal(BigDecimal)}
119      */
fromNumber(BigDecimal number)120     static Document fromNumber(BigDecimal number) {
121         return new NumberDocument(SdkNumber.fromBigDecimal(number));
122     }
123 
124     /**
125      * Create {@link Document} from a BigInteger.
126      * @param number BigInteger type number.
127      * @return Implementation of Document that stores a {@link SdkNumber} constructed {@link SdkNumber#fromBigInteger(BigInteger)}
128      */
fromNumber(BigInteger number)129     static Document fromNumber(BigInteger number) {
130         return new NumberDocument(SdkNumber.fromBigInteger(number));
131     }
132 
133 
134     /**
135      * Create {@link Document} from a String.
136      * @param number String representation of a number.
137      * @return Implementation of Document that stores a {@link SdkNumber} constructed {@link SdkNumber#fromString(String)}
138      * @throws ParseException Throws ParseException when the inputString is not of Number format.
139      */
fromNumber(String number)140     static Document fromNumber(String number) {
141         return new NumberDocument(SdkNumber.fromString(number));
142     }
143 
144 
145     /**
146      * Creates a Document from a Map of Documents.
147      * @param documentMap Map with Keys of Type Strinb and Value of Document type.
148      * @return Implementation of Document that stores a Map with String Keys and Document Values.
149      */
fromMap(Map<String, Document> documentMap)150     static Document fromMap(Map<String, Document> documentMap) {
151         return new MapDocument(documentMap);
152     }
153 
154     /**
155      * Create a {@link ListBuilder} for generating a {@link Document} by directly allowing user add Documents.
156      * @param documentList List of Documents.
157      * @return Implementation of Document that stores a Lists of Documents.
158      */
fromList(List<Document> documentList)159     static Document fromList(List<Document> documentList) {
160         return new ListDocument(documentList);
161     }
162 
163     /**
164      * Create a {@link MapBuilder} for generating a {@link Document} by directly allowing user to put String Keys
165      * and Document Values in the builder methods.
166      * @return Builder to Construct Document with Map of Documents.
167      */
mapBuilder()168     static MapBuilder mapBuilder() {
169         return MapDocument.mapBuilder();
170     }
171 
172     /**
173      * Provides Builder methods of {@link ListBuilder} to directly create Document with List of Documents
174      * @return Builder methods to Construct Document with List of Documents.
175      */
listBuilder()176     static ListBuilder listBuilder() {
177         return ListDocument.listBuilder();
178     }
179 
180     /**
181      * Creates a document is a {@code null} value.
182      *
183      * @return Implementation of a Null Document.
184      */
fromNull()185     static Document fromNull() {
186         return new NullDocument();
187     }
188 
189     /**
190      * Gets the value of the document as a Java type that represents the
191      * document type data model: {@code boolean}, {@code String} for Strings and Numbers,
192      * {@code null}, {@code List<Object>}, or
193      * {@code Map<String, Object>}.
194      * @return Returns the document as one of a fixed set of Java types.
195      */
unwrap()196     Object unwrap();
197 
198     /**
199      * Checks if the document is a {@code null} value.
200      * @return Returns true if the document is a {@code null} value.
201      */
isNull()202     default boolean isNull() {
203         return false;
204     }
205 
206     /**
207      * @return Returns true if this document is a boolean value.
208      */
isBoolean()209     default boolean isBoolean() {
210         return false;
211     }
212 
213     /**
214      * Gets the document as a {@code boolean} if it is a boolean.
215      * @return Returns the boolean value.
216      * @throws UnsupportedOperationException if the document is not a boolean.
217      */
asBoolean()218     boolean asBoolean();
219 
220     /**
221      * @return Returns true if this document is a string value.
222      */
isString()223     default boolean isString() {
224         return false;
225     }
226 
227     /**
228      * Gets the document as a {@code String}.
229      *
230      * @return Returns the string value.
231      * @throws UnsupportedOperationException if the document is not a string.
232      */
asString()233     String asString();
234 
235     /**
236      * @return Returns true if this document is a number value.
237      */
isNumber()238     default boolean isNumber() {
239         return false;
240     }
241 
242     /**
243      * Gets the document as a {@link SdkNumber} if it is a {@link SdkNumber}.
244      * @return Returns the {@link SdkNumber}.
245      * @throws UnsupportedOperationException if the document is not a number.
246      */
asNumber()247     SdkNumber asNumber();
248 
249     /**
250      * @return Returns true if this document is a Map.
251      */
isMap()252     default boolean isMap() {
253         return false;
254     }
255 
256     /**
257      * Gets the document as a {@code Map}.
258      * <p>Each value contained in the {@code Map} is the same as how the value
259      * would be represented by {@link Document}.
260      * @return Returns the Document map.
261      * @throws UnsupportedOperationException if the document is not an Map.
262      */
asMap()263     Map<String, Document> asMap();
264 
265     /**
266      * @return Returns true if this document is a document type List.
267      */
isList()268     default boolean isList() {
269         return false;
270     }
271 
272     /**
273      * Gets the document as a {@code List} if it is a document type array.
274      * <p>Each value contained in the {@code List} is the same as how the
275      * value would be represented by {@link Document}.
276      *
277      * @return Returns the lists of Document.
278      * @throws UnsupportedOperationException if the document is not an List.
279      */
asList()280     List<Document> asList();
281 
282 
283     /**
284      * Accepts a visitor to the Document.
285      * @param <R> visitor return type.
286      * @param visitor Visitor to dispatch to.
287      * @return Returns the accepted result.
288      */
accept(DocumentVisitor<? extends R> visitor)289     <R> R accept(DocumentVisitor<? extends R> visitor);
290 
291     /**
292      * Accepts a visitor with the Document.
293      * @param visitor Visitor to dispatch to.
294      */
accept(VoidDocumentVisitor visitor)295     void accept(VoidDocumentVisitor visitor);
296 
297     interface MapBuilder {
298         /**
299          * Inserts a Key Value pair to a Document Map with String key and a Document created from the given String.
300          * @param key Map Key for the Document.
301          * @param stringValue String value which will be used to create a Document to be inserted in a DocumentMap.
302          * @return Builder which provides APIs to put Key Value pair to a Document Map.
303          */
putString(String key, String stringValue)304         MapBuilder putString(String key, String stringValue);
305 
306         /**
307          * Inserts a Key Value pair to a Document Map with String key and a Document created from the given Number.
308          * @param key Map Key for the Document.
309          * @param numberValue Number value which will be used to create a Document to be inserted in a DocumentMap.
310          * @return Builder which provides APIs to put Key Value pair to a Document Map.
311          */
putNumber(String key, SdkNumber numberValue)312         MapBuilder putNumber(String key, SdkNumber numberValue);
313 
314         /**
315          * Inserts a Key Value pair to a Document Map with String key and a Document created from the given integer.
316          * @param key Map Key for the Document.
317          * @param numberValue Integer value which will be used to create a Document to be inserted in a DocumentMap.
318          * @return Builder which provides APIs to put Key Value pair to a Document Map.
319          */
putNumber(String key, int numberValue)320         MapBuilder putNumber(String key, int numberValue);
321 
322         /**
323          * Inserts a Key Value pair to a Document Map with String key and a Document created from the given long.
324          * @param key Map Key for the Document.
325          * @param numberValue long value which will be used to create a Document to be inserted in a DocumentMap.
326          * @return Builder which provides APIs to put Key Value pair to a Document Map.
327          */
putNumber(String key, long numberValue)328         MapBuilder putNumber(String key, long numberValue);
329 
330         /**
331          * Inserts a Key Value pair to a Document Map with String key and a Document created from the given double.
332          * @param key Map Key for the Document.
333          * @param numberValue double value which will be used to create a Document to be inserted in a DocumentMap.
334          * @return Builder which provides APIs to put Key Value pair to a Document Map.
335          */
putNumber(String key, double numberValue)336         MapBuilder putNumber(String key, double numberValue);
337 
338         /**
339          * Inserts a Key Value pair to a Document Map with String key and a Document created from the given float.
340          * @param key Map Key for the Document.
341          * @param numberValue float value which will be used to create a Document to be inserted in a DocumentMap.
342          * @return Builder which provides APIs to put Key Value pair to a Document Map.
343          */
putNumber(String key, float numberValue)344         MapBuilder putNumber(String key, float numberValue);
345 
346         /**
347          * Inserts a Key Value pair to a Document Map with String key and a Document created from the given BigDecimal.
348          * @param key Map Key for the Document.
349          * @param numberValue BigDecimal value which will be used to create a Document to be inserted in a DocumentMap.
350          * @return Builder which provides APIs to put Key Value pair to a Document Map.
351          */
putNumber(String key, BigDecimal numberValue)352         MapBuilder putNumber(String key, BigDecimal numberValue);
353 
354         /**
355          * Inserts a Key Value pair to a Document Map with String key and a Document created from the given BigInteger.
356          * @param key Map Key for the Document.
357          * @param numberValue BigInteger value which will be used to create a Document to be inserted in a DocumentMap.
358          * @return Builder which provides APIs to put Key Value pair to a Document Map.
359          */
putNumber(String key, BigInteger numberValue)360         MapBuilder putNumber(String key, BigInteger numberValue);
361 
362         /**
363          *
364          * Inserts a Key Value pair to a Document Map with String key and a Document created from the given String.
365          * @param key Map Key for the Document.
366          * @param numberValue String value which will be used to create a Document to be inserted in a DocumentMap.
367          * @return Builder which provides APIs to put Key Value pair to a Document Map.
368          */
putNumber(String key, String numberValue)369         MapBuilder putNumber(String key, String numberValue);
370 
371         /**
372          * Inserts a Key Value pair to a Document Map with String key and a Document created from the given boolean.
373          * @param key Map Key for the Document.
374          * @param booleanValue Boolean value which will be used to create a Document to be inserted in a DocumentMap.
375          * @return Builder which provides APIs to put Key Value pair to a Document Map.
376          */
putBoolean(String key, boolean booleanValue)377         MapBuilder putBoolean(String key, boolean booleanValue);
378 
379         /**
380          * Inserts a Key Value pair to a Document Map with String key and the given Document.
381          * @param key Map Key for the Document.
382          * @param document Document to be inserted in a DocumentMap.
383          * @return Builder which provides APIs to put Key Value pair to a Document Map.
384          */
putDocument(String key, Document document)385         MapBuilder putDocument(String key, Document document);
386 
387         /**
388          * Inserts a Key Value pair to a Document Map with String key and value with Null Document.
389          * @param key Map Key for the Document.
390          * @return Builder which provides APIs to put Key Value pair to a Document Map.
391          */
putNull(String key)392         MapBuilder putNull(String key);
393 
394         /**
395          * Inserts a Key Value pair to a Document Map with String key and value as List of Document.
396          * @param key Map Key for the Document.
397          * @param documentList List of Documents.
398          * @return Builder which provides APIs to put Key Value pair to a Document Map.
399          */
putList(String key, List<Document> documentList)400         MapBuilder putList(String key, List<Document> documentList);
401 
402         /**
403          * Inserts a Key Value pair to a Document Map with String key and value constructed from Consumer of
404          * {@link ListBuilder}.
405          * @param key Map Key for the Document.
406          * @param listBuilderConsumer Consumer that accepts {@link ListBuilder}
407          * @return Builder which provides APIs to put Key Value pair to a Document Map.
408          */
putList(String key, Consumer<ListBuilder> listBuilderConsumer)409         MapBuilder putList(String key, Consumer<ListBuilder> listBuilderConsumer);
410 
411         /**
412          * Inserts a Key Value pair to a Document Map with String key and Document constructed from Document Map.
413          * @param key Map Key for the Document.
414          * @param documentMap Map of Document.
415          * @return Builder which provides APIs to put Key Value pair to a Document Map.
416          */
putMap(String key, Map<String, Document> documentMap)417         MapBuilder putMap(String key, Map<String, Document> documentMap);
418 
419         /**
420          * Inserts a Key Value pair to a Document Map with String key and value constructed from Consumer of
421          * {@link MapBuilder}.
422          * @param key Map Key for the Document.
423          * @param mapBuilderConsumer Consumer that accepts {@link ListBuilder}
424          * @return Builder which provides APIs to put Key Value pair to a Document Map.
425          */
putMap(String key, Consumer<MapBuilder> mapBuilderConsumer)426         MapBuilder putMap(String key, Consumer<MapBuilder> mapBuilderConsumer);
427 
428         /**
429          * Creates a new {@link Document} with the key value pair pair inserted using put  method.
430          * @return The new {@link Document}.
431          */
build()432         Document build();
433     }
434 
435     interface ListBuilder {
436 
437         /**
438          * Adds a Document which is constructed from the given stringValue..
439          * @param stringValue String Value from which the Document to be added is created.
440          * @return Builder which provides APIs to add Document to a Document List.
441          */
addString(String stringValue)442         ListBuilder addString(String stringValue);
443 
444         /**
445          * Adds a Document which is constructed from the given boolean.
446          * @param booleanValue Boolean value from which the  Document to be added is created.
447          * @return Builder which provides APIs to add Document to a Document List.
448          */
addBoolean(boolean booleanValue)449         ListBuilder addBoolean(boolean booleanValue);
450 
451         /**
452          * Adds a Document which is constructed from the given {@link SdkNumber}.
453          * @param numberValue {@link SdkNumber} from which the Document  to be added is created.
454          * @return Builder which provides APIs to add Document to a Document List.
455          */
addNumber(SdkNumber numberValue)456         ListBuilder addNumber(SdkNumber numberValue);
457 
458         /**
459          * Adds a Document which is constructed from the given integer.
460          * @param numberValue integer from which the Document  to be added is created.
461          * @return Builder which provides APIs to add Document to a Document List.
462          */
addNumber(int numberValue)463         ListBuilder addNumber(int numberValue);
464 
465         /**
466          * Adds a Document which is constructed from the given long.
467          * @param numberValue long from which the Document  to be added is created.
468          * @return Builder which provides APIs to add Document to a Document List.
469          */
addNumber(long numberValue)470         ListBuilder addNumber(long numberValue);
471 
472         /**
473          * Adds a Document which is constructed from the given float.
474          * @param numberValue float from which the Document  to be added is created.
475          * @return Builder which provides APIs to add Document to a Document List.
476          */
addNumber(float numberValue)477         ListBuilder addNumber(float numberValue);
478 
479         /**
480          * Adds a Document which is constructed from the given double.
481          * @param numberValue double from which the Document  to be added is created.
482          * @return Builder which provides APIs to add Document to a Document List.
483          */
addNumber(double numberValue)484         ListBuilder addNumber(double numberValue);
485 
486         /**
487          * Adds a Document which is constructed from the given BigDecimal.
488          * @param numberValue BigDecimal from which the Document  to be added is created.
489          * @return Builder which provides APIs to add Document to a Document List.
490          */
addNumber(BigDecimal numberValue)491         ListBuilder addNumber(BigDecimal numberValue);
492 
493         /**
494          * Adds a Document which is constructed from the given BigInteger.
495          * @param numberValue BigInteger from which the Document  to be added is created.
496          * @return Builder which provides APIs to add Document to a Document List.
497          */
addNumber(BigInteger numberValue)498         ListBuilder addNumber(BigInteger numberValue);
499 
500         /**
501          *
502          * Adds a Document which is constructed from the given String.
503          * @param numberValue String from which the Document  to be added is created.
504          * @return Builder which provides APIs to add Document to a Document List.
505          */
addNumber(String numberValue)506         ListBuilder addNumber(String numberValue);
507 
508 
509         /**
510          * Adds a Document to the constructed Document List.
511          * @param document Document that will be added to a Document List.
512          * @return Builder which provides APIs to add Document to a Document List.
513          */
addDocument(Document document)514         ListBuilder addDocument(Document document);
515 
516         /**
517          * Inserts a Document Value constructed from Consumer of {@link MapBuilder}.
518          * @param mapBuilderConsumer Consumer that accepts {@link ListBuilder}
519          * @return Builder which provides APIs to put Key Value pair to a Document Map.
520          */
addMap(Consumer<MapBuilder> mapBuilderConsumer)521         ListBuilder addMap(Consumer<MapBuilder> mapBuilderConsumer);
522 
523         /**
524          * Inserts a Null Document to the constructed Document List.
525          * @return Builder which provides APIs to add Document to a Document List.
526          */
addNull()527         ListBuilder addNull();
528 
529         /**
530          * Creates a new {@link Document} with the List members as added with add method.
531          * @return The new {@link Document}.
532 
533          */
build()534         Document build();
535     }
536 }
537