Skip to content

Commit

Permalink
Adding JacksonJsUtils.toArrayNode()
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-brandizi committed May 13, 2024
1 parent bf398f6 commit 8f73f9c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@

<!-- Required by
uk.ac.ebi.utils.opt.config
uk.ac.ebi.utils.opt.json
-->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
Expand Down
5 changes: 3 additions & 2 deletions revision-history.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Revision History

*This file was last revised on 2024-04-15*. **Please keep this note updated**.
*This file was last revised on 2024-05-13*. **Please keep this note updated**.

## 14.1-SNAPSHOT
* `ChainExecutor.andThen()` added (to be tested).
* `StreamUtils.sampleStream()` added.
* `uk.ac.ebi.utils.statistics.FisherExact` imported from the USeq project.
* `CollectionUtils`, adding functions to copy collections and make them read-only.
* `CollectionUtils`, adding functions to copy collections and making them read-only.
* `JacksonJsUtils.toArrayNode()` added.

## 14.0.1
* Various dependency upgrades.
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/uk/ac/ebi/utils/opt/json/JacksonJsUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package uk.ac.ebi.utils.opt.json;

import static java.util.stream.Collector.Characteristics.IDENTITY_FINISH;
import static java.util.stream.Collector.Characteristics.UNORDERED;

import java.util.function.BiConsumer;
import java.util.stream.Collector;
import java.util.stream.Collector.Characteristics;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

/**
* JSON utils based on the Jackson library.
*
* @author Marco Brandizi
* <dl><dt>Date:</dt><dd>13 May 2024</dd></dl>
*
*/
public class JacksonJsUtils
{

/**
* Returns a collector (i.e., for {@link Stream#collect(Collector)} and the like) that
* collects the elements of type T into an {@link ArrayNode}.
*
* This is a serialised collector, which requires synchronisation when elements are accumulated (ie,
* it doesn't have {@link Characteristics#CONCURRENT}.
*
* At the same time, it's an {@link Characteristics#UNORDERED} and {@link Characteristics#IDENTITY_FINISH}
* collector.
*
* @param objectMapper the mapper where the {@link ObjectMapper#createArrayNode()} is taken.
* @param accumulator method to add an element to a node. This is typically an addXXX() method
* from {@link ArrayNode}.
*
* TODO: tests
*
*/
public static <T> Collector<T, ArrayNode, ArrayNode> toArrayNode (
ObjectMapper objectMapper, BiConsumer<ArrayNode, T> accumulator
)
{
return Collector.<T, ArrayNode> of (
objectMapper::createArrayNode,
accumulator,
ArrayNode::addAll,
UNORDERED
);
}

/**
* Flavour that uses {@link ArrayNode#add(JsonNode)}
*/
public static Collector<JsonNode, ArrayNode, ArrayNode> toArrayNode (
ObjectMapper objectMapper
)
{
return toArrayNode ( objectMapper, ArrayNode::add );
}
}

0 comments on commit 8f73f9c

Please sign in to comment.