Skip to content

Other Document and Entry Operations

inno-juanal edited this page Mar 9, 2017 · 20 revisions

Removing

Removing an Entry

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);

Remove All Entries

To remove all entries in a document, invoke:

document.clear();

Getting Keys and Entries

Checking whether an Entry Exists

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).

Getting all Entry Keys

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());
}

Getting All Entries

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.

Getting Unit Entries

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).