default void forEach(BiConsumer<Path, Value> consumer) { Path root = Path.root(); stream().forEach((field) -> { Path path = root.append(field.getName().toString()); Value value = field.getValue(); if (value.isDocument()) { value.asDocument().forEach((p, v) -> { consumer.accept(path.append(p), v); }); } else if (value.isArray()) { value.asArray().forEach((entry) -> { consumer.accept(path.append(Integer.toString(entry.getIndex())), entry.getValue()); }); } else { consumer.accept(path, value); } }); }
/** * Transform all of the field values using the supplied {@link BiFunction transformer function}. * * @param transformer the transformer that should be used to transform each field value; may not be null * @return this document with transformed fields, or this document if the transformer changed none of the values */ default Document transform(BiFunction<CharSequence, Value, Value> transformer) { for (Field field : this) { Value existing = get(field.getName()); Value updated = transformer.apply(field.getName(), existing); if (updated == null) updated = Value.nullValue(); if (updated != existing) { setValue(field.getName(), updated); } } return this; }
/** * Attempts to copy all of the acceptable fields from the source and set on this document, overwriting any existing * values. * * @param fields the name/value pairs to be set on this object; may not be null * @param acceptableFieldNames the predicate to determine which fields from the source should be copied; may not be null * @return This document, to allow for chaining methods */ default Document putAll(Iterable<Field> fields, Predicate<CharSequence> acceptableFieldNames) { for (Field field : fields) { if (acceptableFieldNames.test(field.getName())) { setValue(field.getName(), field.getValue()); } } return this; }
protected void writeDocument(Document document, JsonGenerator generator) throws IOException { generator.writeStartObject(); try { document.stream().forEach((field) -> { try { generator.writeFieldName(field.getName().toString()); writeValue(field.getValue(), generator); } catch (IOException e) { throw new WritingError(e); } }); generator.writeEndObject(); } catch (WritingError e) { throw e.wrapped(); } }
default void forEach(BiConsumer<Path, Value> consumer) { Path root = Path.root(); stream().forEach((field) -> { Path path = root.append(field.getName().toString()); Value value = field.getValue(); if (value.isDocument()) { value.asDocument().forEach((p, v) -> { consumer.accept(path.append(p), v); }); } else if (value.isArray()) { value.asArray().forEach((entry) -> { consumer.accept(path.append(Integer.toString(entry.getIndex())), entry.getValue()); }); } else { consumer.accept(path, value); } }); }
/** * Sets on this object all name/value pairs from the supplied object. If the supplied object is null, this method does * nothing. * * @param fields the name/value pairs to be set on this object; may not be null * @return This document, to allow for chaining methods */ default Document putAll(Iterator<Field> fields) { while (fields.hasNext()) { Field field = fields.next(); setValue(field.getName(), field.getValue()); } return this; }
default boolean isNotNull() { return !isNull(); }
default boolean isNull() { Value v = getValue(); return v == null || v.isNull(); }
/** * Sets on this object all name/value pairs from the supplied object. If the supplied object is null, this method does * nothing. * * @param fields the name/value pairs to be set on this object; may not be null * @return This document, to allow for chaining methods */ default Document putAll(Iterable<Field> fields) { for (Field field : fields) { setValue(field.getName(), field.getValue()); } return this; }
@Override public boolean hasAll(Document that) { if (that == null) return true; if (this.size() < that.size()) { // Can't have all of 'that' if 'that' is bigger ... return false; } return that.stream().allMatch(field -> { Value thatValue = field.getValue(); Value thisValue = this.get(field.getName()); return Value.compareTo(thisValue, thatValue) == 0; }); }
/** * Transform all of the field values using the supplied {@link BiFunction transformer function}. * * @param transformer the transformer that should be used to transform each field value; may not be null * @return this document with transformed fields, or this document if the transformer changed none of the values */ default Document transform(BiFunction<CharSequence, Value, Value> transformer) { for (Field field : this) { Value existing = get(field.getName()); Value updated = transformer.apply(field.getName(), existing); if (updated == null) updated = Value.nullValue(); if (updated != existing) { setValue(field.getName(), updated); } } return this; }
/** * Sets on this object all name/value pairs from the supplied object. If the supplied object is null, this method does * nothing. * * @param fields the name/value pairs to be set on this object; may not be null * @return This document, to allow for chaining methods */ default Document putAll(Iterator<Field> fields) { while (fields.hasNext()) { Field field = fields.next(); setValue(field.getName(), field.getValue()); } return this; }
protected void writeDocument(Document document, JsonGenerator generator) throws IOException { generator.writeStartObject(); try { document.stream().forEach((field) -> { try { generator.writeFieldName(field.getName().toString()); writeValue(field.getValue(), generator); } catch (IOException e) { throw new WritingError(e); } }); generator.writeEndObject(); } catch (WritingError e) { throw e.wrapped(); } }
/** * Attempts to copy all of the acceptable fields from the source and set on this document, overwriting any existing * values. * * @param fields the name/value pairs to be set on this object; may not be null * @param acceptableFieldNames the predicate to determine which fields from the source should be copied; may not be null * @return This document, to allow for chaining methods */ default Document putAll(Iterable<Field> fields, Predicate<CharSequence> acceptableFieldNames) { for (Field field : fields) { if (acceptableFieldNames.test(field.getName())) { setValue(field.getName(), field.getValue()); } } return this; }
default boolean isNotNull() { return !isNull(); }