-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphRepository.java
104 lines (90 loc) · 2.91 KB
/
GraphRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.github.sszuev.jena.ontapi;
import com.github.sszuev.jena.ontapi.impl.repositories.DocumentGraphRepository;
import org.apache.jena.graph.Graph;
import org.apache.jena.graph.GraphMemFactory;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* Graph repository.
* Each {@link Graph} is associated with ID.
* For OWL Ontology Graphs, Graph ID can be {@link com.github.sszuev.jena.ontapi.model.OntID#getImportsIRI()}.
*
* @see org.apache.jena.ontology.OntDocumentManager
*/
public interface GraphRepository {
/**
* A factory method to creates {@link GraphRepository} instance
* that loads graphs on demand from the location to memory.
* The location is specified by the method {@link DocumentGraphRepository#addMapping(String, String)}.
*
* @return {@link DocumentGraphRepository}
*/
static DocumentGraphRepository createGraphDocumentRepositoryMem() {
return createGraphDocumentRepository(GraphMemFactory::createDefaultGraph);
}
/**
* A factory method to creates {@link GraphRepository} instance
* that loads graphs on demand from the location.
* The location is specified by the method {@link DocumentGraphRepository#addMapping(String, String)}.
*
* @param factory {@link Supplier} to produce new {@link Graph}, {@code null} for default
* @return {@link DocumentGraphRepository}
*/
static DocumentGraphRepository createGraphDocumentRepository(Supplier<Graph> factory) {
return new DocumentGraphRepository(Objects.requireNonNull(factory, "Null graph factory"));
}
/**
* Gets Graph by ID.
*
* @param id {@code String} Graph's identifier
* @return {@link Graph}
*/
Graph get(String id);
/**
* @return {@code Stream} of Graph's identifiers
*/
Stream<String> ids();
/**
* Associates the specified graph with the specified ID,
* returning the previous association or {@code null} if there was no association.
*
* @param id {@code String} Graph's identifier
* @param graph {@link Graph}
* @return {@link Graph} or {@code null}
*/
Graph put(String id, Graph graph);
/**
* Removes graph.
*
* @param id {@code String} Graph's identifier
* @return {@link Graph}
*/
Graph remove(String id);
/**
* Removes all graphs.
*/
void clear();
/**
* @return number of graphs
*/
default long count() {
return ids().count();
}
/**
* Lists all graphs.
*
* @return {@code Stream} of {@link Graph}s
*/
default Stream<Graph> graphs() {
return ids().map(this::get).filter(Objects::nonNull);
}
/**
* @param id {@code String} Graph's identifier
* @return boolean
*/
default boolean contains(String id) {
Objects.requireNonNull(id);
return ids().anyMatch(id::equals);
}
}