public class DocumentBuilderImpl extends AbstractBuilder implements DocumentBuilder
AbstractBuilder.BuilderElement
ASSERTIONS_ENABLED, BUILDER_ELEMENT_CLASS, myElements, mySize
Constructor and Description |
---|
DocumentBuilderImpl()
Creates a new builder.
|
DocumentBuilderImpl(AbstractBuilder outerScope)
Creates a new builder.
|
DocumentBuilderImpl(DocumentAssignable seedDocument)
Creates a new builder.
|
Modifier and Type | Method and Description |
---|---|
DocumentBuilder |
add(ElementAssignable elementRef)
Adds a pre-built element to the document.
|
DocumentBuilder |
add(String name,
boolean value)
Adds a boolean element.
|
DocumentBuilder |
add(String name,
byte[] data)
Adds a binary element using sub-type zero (the default).
|
DocumentBuilder |
add(String name,
byte subType,
byte[] data)
Adds a binary element.
|
DocumentBuilder |
add(String name,
Date timestamp)
Adds a timestamp element.
|
DocumentBuilder |
add(String name,
DocumentAssignable document)
Adds a pre-constructed document to the array.
|
DocumentBuilder |
add(String name,
double value)
Adds a double element.
|
DocumentBuilder |
add(String name,
int value)
Adds a integer (32-bit signed) element.
|
DocumentBuilder |
add(String name,
long value)
Adds a long (64-bit signed) element.
|
DocumentBuilder |
add(String name,
Object value)
Adds the value to the document after trying to coerce the value into the
best possible element type.
|
DocumentBuilder |
add(String name,
ObjectId id)
Adds an ObjectId element.
|
DocumentBuilder |
add(String name,
Pattern pattern)
Adds an ObjectId element.
|
DocumentBuilder |
add(String name,
String value)
Adds a string element.
|
DocumentBuilder |
add(String name,
String databaseName,
String collectionName,
ObjectId id)
Deprecated.
|
DocumentBuilder |
add(String name,
UUID uuid)
Adds a (sub-type 4)
UUID binary element. |
DocumentBuilder |
addBinary(String name,
byte[] value)
Adds a binary element using sub-type zero (the default).
|
DocumentBuilder |
addBinary(String name,
byte subType,
byte[] value)
Adds a binary element using sub-type zero (the default).
|
DocumentBuilder |
addBoolean(String name,
boolean value)
Adds a boolean element.
|
DocumentBuilder |
addDBPointer(String name,
String databaseName,
String collectionName,
ObjectId id)
Deprecated.
|
DocumentBuilder |
addDocument(String name,
DocumentAssignable value)
Adds a pre-built document element.
|
DocumentBuilder |
addDouble(String name,
double value)
Adds a double element.
|
DocumentBuilder |
addInteger(String name,
int value)
Adds a integer (32-bit signed) element.
|
DocumentBuilder |
addJavaScript(String name,
String code)
Adds a JavaScript element.
|
DocumentBuilder |
addJavaScript(String name,
String code,
DocumentAssignable scope)
Adds a JavaScript with Scope element.
|
DocumentBuilder |
addLegacyUuid(String name,
UUID uuid)
Adds a legacy (sub-type 3)
UUID binary element. |
DocumentBuilder |
addLong(String name,
long value)
Adds a long (64-bit signed) element.
|
DocumentBuilder |
addMaxKey(String name)
Adds a minimum key value element.
|
DocumentBuilder |
addMinKey(String name)
Adds a minimum key value element.
|
DocumentBuilder |
addMongoTimestamp(String name,
long value)
Adds a MongoDB Timestamp element.
|
DocumentBuilder |
addNull(String name)
Adds a
null valued element. |
DocumentBuilder |
addObjectId(String name,
ObjectId id)
Adds an ObjectId element.
|
DocumentBuilder |
addRegularExpression(String name,
Pattern pattern)
Adds a regular expression element.
|
DocumentBuilder |
addRegularExpression(String name,
String pattern,
String options)
Adds a regular expression element.
|
DocumentBuilder |
addString(String name,
String value)
Adds a string element.
|
DocumentBuilder |
addSymbol(String name,
String symbol)
Adds a symbol element.
|
DocumentBuilder |
addTimestamp(String name,
long timestamp)
Adds a timestamp element.
|
DocumentBuilder |
addUuid(String name,
UUID uuid)
Adds a (sub-type 4)
UUID binary element. |
Document |
asDocument()
Converts the object into a document.
|
Document |
build()
Returns the
Document being constructed. |
protected Element |
build(String name)
Constructs the final form of the element being constructed.
|
DocumentBuilder |
push(String name)
Pushes a context for constructing a sub-document.
|
ArrayBuilder |
pushArray(String name)
Pushes a context for constructing a sub-array.
|
DocumentBuilder |
remove(String name)
Removes all
Element s that have the provided name from the
document being built. |
DocumentBuilder |
reset()
Resets the builder back to an empty state.
|
String |
toString() |
doPush, doPushArray, pop, subElements
public DocumentBuilderImpl()
public DocumentBuilderImpl(AbstractBuilder outerScope)
outerScope
- The outer document scope.public DocumentBuilderImpl(DocumentAssignable seedDocument)
seedDocument
- The document to seed the builder with. The builder will
contain the seed document elements plus any added/appended
elements.public DocumentBuilder add(ElementAssignable elementRef) throws IllegalArgumentException
add
in interface DocumentBuilder
elementRef
- The element to add.DocumentBuilder
for method chaining.IllegalArgumentException
- If the element
is null
.public DocumentBuilder add(String name, boolean value) throws IllegalArgumentException
This is a equivalent to DocumentBuilder.addBoolean(String,boolean)
but less
verbose.
add
in interface DocumentBuilder
name
- The name of the element.value
- The boolean value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder add(String name, byte subType, byte[] data) throws IllegalArgumentException
This is a equivalent to DocumentBuilder.addBinary(String,byte, byte[])
but less
verbose.
add
in interface DocumentBuilder
name
- The name of the element.subType
- The sub-type for the binary data.data
- The binary value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or data
is null
.public DocumentBuilder add(String name, byte[] data) throws IllegalArgumentException
This is a equivalent to DocumentBuilder.addBinary(String,byte[])
but will insert
a NullElement
if the data
is null
instead of
throwing an IllegalArgumentException
.
add
in interface DocumentBuilder
name
- The name of the element.data
- The binary value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder add(String name, Date timestamp) throws IllegalArgumentException
This is a equivalent to addTimeStamp(timestamp.getTime())
but will insert a NullElement
if the timestamp
is null
instead of throwing an
IllegalArgumentException
.
add
in interface DocumentBuilder
name
- The name of the element.timestamp
- The number of milliseconds since the Unix epoch.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder add(String name, DocumentAssignable document) throws IllegalArgumentException
This is a equivalent to DocumentBuilder.addDocument(String,DocumentAssignable)
but will insert a NullElement
if the document
is
null
instead of throwing an IllegalArgumentException
.
add
in interface DocumentBuilder
name
- The name of the element.document
- The document to add to the array.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder add(String name, double value) throws IllegalArgumentException
This is a equivalent to DocumentBuilder.addDouble(String,double)
but less
verbose.
add
in interface DocumentBuilder
name
- The name of the element.value
- The double value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder add(String name, int value) throws IllegalArgumentException
This is a equivalent to DocumentBuilder.addInteger(String,int)
but less verbose.
add
in interface DocumentBuilder
name
- The name of the element.value
- The integer value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder add(String name, long value) throws IllegalArgumentException
This is a equivalent to DocumentBuilder.addLong(String,long)
but less verbose.
add
in interface DocumentBuilder
name
- The name of the element.value
- The long value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder add(String name, Object value) throws IllegalArgumentException
IllegalArgumentException
is thrown.
This method does type inspection which can be slow. It is generally much faster to use the type specific methods of this interface.
add
in interface DocumentBuilder
name
- The name of the element.value
- The Object value to coerce into an element.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
or the value
cannot be coerced into an element type.public DocumentBuilder add(String name, ObjectId id) throws IllegalArgumentException
This is a equivalent to DocumentBuilder.addObjectId(String,ObjectId)
but will
insert a NullElement
if the id
is null
instead of throwing an IllegalArgumentException
.
add
in interface DocumentBuilder
name
- The name of the element.id
- The ObjectId to add.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or id
is null
.public DocumentBuilder add(String name, Pattern pattern) throws IllegalArgumentException
This is a equivalent to DocumentBuilder.addRegularExpression(String,Pattern)
but
will insert a NullElement
if the pattern
is
null
instead of throwing an IllegalArgumentException
.
add
in interface DocumentBuilder
name
- The name of the element.pattern
- The pattern for the regular expression.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder add(String name, String value) throws IllegalArgumentException
This is a equivalent to DocumentBuilder.addString(String,String)
but will insert
a NullElement
if the value
is null
instead
of throwing an IllegalArgumentException
.
add
in interface DocumentBuilder
name
- The name of the element.value
- The string value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.@Deprecated public DocumentBuilder add(String name, String databaseName, String collectionName, ObjectId id) throws IllegalArgumentException
This is a equivalent to
DocumentBuilder.addDBPointer(String,String, String, ObjectId)
but less verbose.
add
in interface DocumentBuilder
name
- The name of the element.databaseName
- The name of the database containing the document.collectionName
- The name of the collection containing the document.id
- The id for the document.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
, databaseName
,
collectionName
, or id
is null
.public DocumentBuilder add(String name, UUID uuid) throws IllegalArgumentException
UUID
binary element.
This is a equivalent to DocumentBuilder.addUuid(String,UUID)
but will insert a
NullElement
if the uuid
is null
instead of
throwing an IllegalArgumentException
.
add
in interface DocumentBuilder
name
- The name of the element.uuid
- The UUID
to add.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder addBinary(String name, byte subType, byte[] value) throws IllegalArgumentException
addBinary
in interface DocumentBuilder
name
- The name of the element.subType
- The sub-type for the binary data.value
- The binary value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or data
is null
.public DocumentBuilder addBinary(String name, byte[] value) throws IllegalArgumentException
This method throws an IllegalArgumentException
if the
data
is null
. If you would prefer a
NullElement
be inserted in the document use the
DocumentBuilder.add(String, byte[])
method instead.
addBinary
in interface DocumentBuilder
name
- The name of the element.value
- The binary value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or value
is null
.public DocumentBuilder addBoolean(String name, boolean value) throws IllegalArgumentException
addBoolean
in interface DocumentBuilder
name
- The name of the element.value
- The boolean value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.@Deprecated public DocumentBuilder addDBPointer(String name, String databaseName, String collectionName, ObjectId id) throws IllegalArgumentException
addDBPointer
in interface DocumentBuilder
name
- The name of the element.databaseName
- The name of the database containing the document.collectionName
- The name of the collection containing the document.id
- The id for the document.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
, databaseName
,
collectionName
, or id
is null
.public DocumentBuilder addDocument(String name, DocumentAssignable value) throws IllegalArgumentException
DocumentBuilder.push(String)
a sub
document.
This method throws an IllegalArgumentException
if the
value
is null
. If you would prefer a
NullElement
be inserted in the document use the
DocumentBuilder.add(String, DocumentAssignable)
method instead.
addDocument
in interface DocumentBuilder
name
- The name of the element.value
- The document value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or value
is null
.public DocumentBuilder addDouble(String name, double value) throws IllegalArgumentException
addDouble
in interface DocumentBuilder
name
- The name of the element.value
- The double value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder addInteger(String name, int value) throws IllegalArgumentException
addInteger
in interface DocumentBuilder
name
- The name of the element.value
- The integer value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder addJavaScript(String name, String code) throws IllegalArgumentException
addJavaScript
in interface DocumentBuilder
name
- The name of the element.code
- The java script code.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or code
is null
.public DocumentBuilder addJavaScript(String name, String code, DocumentAssignable scope) throws IllegalArgumentException
addJavaScript
in interface DocumentBuilder
name
- The name of the element.code
- The java script code.scope
- The scope for the JacaScript code.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
, value
, or scope
is
null
.public DocumentBuilder addLegacyUuid(String name, UUID uuid) throws IllegalArgumentException
UUID
binary element.
This method throws an IllegalArgumentException
if the
uuid
is null
.
addLegacyUuid
in interface DocumentBuilder
name
- The name of the element.uuid
- The UUID
to add.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or uuid
is null
.public DocumentBuilder addLong(String name, long value) throws IllegalArgumentException
addLong
in interface DocumentBuilder
name
- The name of the element.value
- The long value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder addMaxKey(String name) throws IllegalArgumentException
addMaxKey
in interface DocumentBuilder
name
- The name of the element.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder addMinKey(String name) throws IllegalArgumentException
addMinKey
in interface DocumentBuilder
name
- The name of the element.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder addMongoTimestamp(String name, long value) throws IllegalArgumentException
addMongoTimestamp
in interface DocumentBuilder
name
- The name of the element.value
- The mongoDB timstamp value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder addNull(String name) throws IllegalArgumentException
null
valued element.addNull
in interface DocumentBuilder
name
- The name of the element.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder addObjectId(String name, ObjectId id) throws IllegalArgumentException
This method throws an IllegalArgumentException
if the id
is null
. If you would prefer a NullElement
be
inserted in the document use the DocumentBuilder.add(String, ObjectId)
method
instead.
addObjectId
in interface DocumentBuilder
name
- The name of the element.id
- The ObjectId to add.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or id
is null
.public DocumentBuilder addRegularExpression(String name, Pattern pattern) throws IllegalArgumentException
This method throws an IllegalArgumentException
if the
pattern
is null
. If you would prefer a
NullElement
be inserted in the document use the
DocumentBuilder.add(String, Pattern)
method instead.
addRegularExpression
in interface DocumentBuilder
name
- The name of the element.pattern
- The pattern for the regular expression.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or pattern
is null
.public DocumentBuilder addRegularExpression(String name, String pattern, String options) throws IllegalArgumentException
addRegularExpression
in interface DocumentBuilder
name
- The name of the element.pattern
- The pattern for the regular expression.options
- The regular expression options. See the BSON specification for
details. The options may be null
.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or pattern
is null
.
Note the options
may be null
.public DocumentBuilder addString(String name, String value) throws IllegalArgumentException
This method throws an IllegalArgumentException
if the
value
is null
. If you would prefer a
NullElement
be inserted in the document use the
DocumentBuilder.add(String, String)
method instead.
addString
in interface DocumentBuilder
name
- The name of the element.value
- The string value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or value
is null
.public DocumentBuilder addSymbol(String name, String symbol) throws IllegalArgumentException
addSymbol
in interface DocumentBuilder
name
- The name of the element.symbol
- The symbol value.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or symbol
is null
.public DocumentBuilder addTimestamp(String name, long timestamp) throws IllegalArgumentException
addTimestamp
in interface DocumentBuilder
name
- The name of the element.timestamp
- The number of milliseconds since the Unix epoch.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
is null
.public DocumentBuilder addUuid(String name, UUID uuid) throws IllegalArgumentException
UUID
binary element.
This method throws an IllegalArgumentException
if the
uuid
is null
. If you would prefer a
NullElement
be inserted in the document use the
DocumentBuilder.add(String, UUID)
method instead.
addUuid
in interface DocumentBuilder
name
- The name of the element.uuid
- The UUID
to add.DocumentBuilder
for method chaining.IllegalArgumentException
- If the name
or uuid
is null
.public Document asDocument()
Returns the result of build()
.
asDocument
in interface DocumentAssignable
build()
public Document build()
Document
being constructed.build
in interface DocumentBuilder
Document
.public DocumentBuilder push(String name)
push
in interface DocumentBuilder
name
- The name of the sub-document.DocumentBuilder
for constructing the sub-document.public ArrayBuilder pushArray(String name)
pushArray
in interface DocumentBuilder
name
- The name of the sub-array.ArrayBuilder
for constructing the sub-array.public DocumentBuilder remove(String name)
Element
s that have the provided name from the
document being built.
Note that adding a new element with the same name adds that element to the end of document's element list.
remove
in interface DocumentBuilder
name
- The name of the element to remove.DocumentBuilder
for method chaining.public DocumentBuilder reset()
reset
in interface Builder
reset
in interface DocumentBuilder
reset
in class AbstractBuilder
public String toString()
Overridden to return the current state of the builder as a document.
protected Element build(String name)
Overridden to return an DocumentElement
.
build
in class AbstractBuilder
name
- The name of the element.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.