-
Notifications
You must be signed in to change notification settings - Fork 1
Other Document and Entry Operations
Invoke the remove
method to remove an entry:
document.entry("releaseYear").remove();
The typed entry references also allow you to remove an entry. However, note that the type and its conversion is not taken into account. For instance, invoking document.intEntry("releaseYear").remove()
removes the entry even if the value was actually an enum.
Note that the remove()
method is strict.: if there's no entry with the key, an exception is thrown. If you'd rather use a more lenient approach, then do this:
document.entry("keyThatMayOrNotExist").remove(RemoveOption.LENIENT);
To remove all entries in a document, invoke:
document.clear();
If all you want is to check whether an entry with a key, such as description exists, then use this:
document.containsKey("description")
If after checking for existence you'll retrieve the value, it's better to use a combination of isAssigned()
and getVal()
as shown in the example in [Getting Entries](Getting Entry Values).
The following example gets the keys of all entries and collects the upper-cased version in a set. (This example shows code in Java 6. Simplifications may be achieved with higher versions.)
Set<String> upperCased = new HashSet<String>();
for(String key: document.getKeys()) {
upperCased.add(key.toUpperCase());
}
Sometimes is necessary to get all entries for the implementation of a sophisticated Java service. Use getAllEntries
for this.
The following example copies entries that start with an underscore from the pipeline and exposes them under an entry named withUnderscore
.
In Java 7, you'd do something like this:
Document pipeDoc = Documents.wrap(pipeline);
Document newDoc= Documents.wrap(pipeline);
try (EntryIterableResource entries = pipeDoc.getAllEntries()) {
for (KeyValue entry : entries) {
String key = entry.getKey();
if (key.startsWith("_")) {
newDoc.entry(key).put(entry.getValue());
}
}
}
pipeDoc.docEntry("withUnderscore").put(newDoc);
Note that (unfortunately) we needed to not only iterate on the entries but also surround it with a try-with resources block. This is necessary because IDataCursor instances are used internally.
To get unit entries invoke the getUnitEntries
method.
for (KeyValue entry : document.getUnitEntries()) {
// Do something with entry
}
For more information about unit entries, refer to the [Unit and Split Entries section](Unit and Split Entries).