Skip to content

Commit fe179bf

Browse files
committed
feat: add utility class for handling RO-Crate graph operations
1 parent 0fba28d commit fe179bf

File tree

2 files changed

+57
-25
lines changed

2 files changed

+57
-25
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package edu.kit.datamanager.ro_crate.util;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
5+
import java.util.stream.StreamSupport;
6+
7+
/**
8+
* Utility class for handling operations on RO-Crate graphs.
9+
* Provides methods to find entities by ID or type within a graph.
10+
* <p>
11+
* {@see JsonUtilFunctions}.
12+
*/
13+
public class Graph {
14+
/**
15+
* Finds an entity in the graph by its ID.
16+
*
17+
* @param graph The JSON node representing the graph.
18+
* @param id The ID of the entity to find.
19+
* @return The entity as a JsonNode if found, null otherwise.
20+
*/
21+
public static JsonNode findEntityById(JsonNode graph, String id) {
22+
for (JsonNode entity : graph) {
23+
if (entity.has("@id") && entity.get("@id").asText().equals(id)) {
24+
return entity;
25+
}
26+
}
27+
return null;
28+
}
29+
30+
/**
31+
* Finds an entity in the graph by its type.
32+
*
33+
* @param graph The JSON node representing the graph.
34+
* @param type The type of the entity to find.
35+
* @return The entity as a JsonNode if found, null otherwise.
36+
*/
37+
public static JsonNode findEntityByType(JsonNode graph, String type) {
38+
return StreamSupport.stream(graph.spliterator(), false)
39+
.filter(entity -> entity.path("@type").asText().equals(type))
40+
.findFirst()
41+
.orElse(null);
42+
}
43+
44+
/**
45+
* Finds all entities in the graph by their type.
46+
*
47+
* @param graph The JSON node representing the graph.
48+
* @param type The type of the entities to find.
49+
* @return An array of JsonNode containing all entities of the specified type.
50+
*/
51+
public static JsonNode[] findEntitiesByType(JsonNode graph, String type) {
52+
return StreamSupport.stream(graph.spliterator(), false)
53+
.filter(entity -> entity.path("@type").asText().equals(type))
54+
.toArray(JsonNode[]::new);
55+
}
56+
}

src/test/java/edu/kit/datamanager/ro_crate/writer/RoCrateMetadataGenerationTest.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
import java.io.IOException;
1515
import java.nio.file.Files;
1616
import java.nio.file.Path;
17-
import java.util.stream.StreamSupport;
1817

18+
import static edu.kit.datamanager.ro_crate.util.Graph.*;
1919
import static org.junit.jupiter.api.Assertions.*;
2020

2121
class RoCrateMetadataGenerationTest {
@@ -401,28 +401,4 @@ void should_PreserveExistingProvenance_When_ModifyingCrate(@TempDir Path tempDir
401401
assertEquals(2, roCrateJavaEntity.get("Action").size(),
402402
"should have both actions");
403403
}
404-
405-
private JsonNode findEntityById(JsonNode graph, String id) {
406-
for (JsonNode entity : graph) {
407-
if (entity.has("@id") && entity.get("@id").asText().equals(id)) {
408-
return entity;
409-
}
410-
}
411-
return null;
412-
}
413-
414-
private JsonNode findEntityByType(JsonNode graph, String type) {
415-
for (JsonNode entity : graph) {
416-
if (entity.has("@type") && entity.get("@type").asText().equals(type)) {
417-
return entity;
418-
}
419-
}
420-
return null;
421-
}
422-
423-
private JsonNode[] findEntitiesByType(JsonNode graph, String type) {
424-
return StreamSupport.stream(graph.spliterator(), false)
425-
.filter(entity -> entity.has("@type") && entity.get("@type").asText().equals(type))
426-
.toArray(JsonNode[]::new);
427-
}
428404
}

0 commit comments

Comments
 (0)