This is an automated email from the ASF dual-hosted git repository.
afs pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
The following commit(s) were added to refs/heads/main by this push:
new cc47512a94 GH-3880: Migrate GraphUtils.exactlyOneProperty and
.atmostOneProperty
cc47512a94 is described below
commit cc47512a94b3b7150fe682cbc3f24c4f2493ba5b
Author: Andy Seaborne <[email protected]>
AuthorDate: Tue May 5 17:45:12 2026 +0100
GH-3880: Migrate GraphUtils.exactlyOneProperty and .atmostOneProperty
---
.../apache/jena/sparql/util/graph/GraphUtils.java | 61 ++++++++++++++++++++--
.../jena/fuseki/access/AssemblerAccessDataset.java | 4 +-
.../fuseki/access/AssemblerSecurityRegistry.java | 4 +-
.../jena/rdfpatch/filelog/AssemblerFileLog.java | 4 +-
.../jena/tdb2/assembler/DatasetAssemblerTDB2.java | 4 +-
.../text/assembler/TextIndexLuceneAssembler.java | 7 +--
6 files changed, 68 insertions(+), 16 deletions(-)
diff --git
a/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
b/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
index c72c737abf..9dc06908be 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
@@ -142,7 +142,41 @@ public class GraphUtils {
return values;
}
+ /** @deprecated */
+ @Deprecated(forRemoval=true)
public static boolean exactlyOneProperty(Resource r, Property p) {
+ StmtIterator sIter = r.listProperties(p);
+ try {
+ if ( !sIter.hasNext() )
+ return false;
+ sIter.next();
+ if ( sIter.hasNext() )
+ return false;
+ }
+ finally {
+ sIter.close();
+ }
+ return true;
+ }
+
+ /** Returns for exactly one object of a subject-property, else return
false. */
+ public static boolean checkExactlyOneProperty(Resource r, Property p) {
+ StmtIterator sIter = r.listProperties(p);
+ try {
+ if ( !sIter.hasNext() )
+ return false;
+ sIter.next();
+ if ( sIter.hasNext() )
+ return false;
+ }
+ finally {
+ sIter.close();
+ }
+ return true;
+ }
+
+ /** Check for exactly one object of a subject-property. Throw an exception
is this condition is not correct. */
+ public static void exactlyOnePropertyEx(Resource r, Property p) {
StmtIterator sIter = r.listProperties(p);
try {
if ( !sIter.hasNext() )
@@ -154,9 +188,10 @@ public class GraphUtils {
finally {
sIter.close();
}
- return true;
}
+ /** @deprecated Use {@link #atMostOneProperty} which always returns a
boolean and does not throw exceptions. */
+ @Deprecated(forRemoval = true)
public static boolean atmostOneProperty(Resource r, Property p) {
StmtIterator sIter = r.listProperties(p);
try {
@@ -172,8 +207,24 @@ public class GraphUtils {
return true;
}
+ /** Check whether the resource-property pair has zero or one values.
Return true or false. */
+ public static boolean atMostOneProperty(Resource r, Property p) {
+ StmtIterator sIter = r.listProperties(p);
+ try {
+ if ( !sIter.hasNext() )
+ return true;
+ sIter.next();
+ if ( sIter.hasNext() )
+ return false;
+ }
+ finally {
+ sIter.close();
+ }
+ return true;
+ }
+
public static boolean getBooleanValue(Resource r, Property p) {
- if ( !GraphUtils.atmostOneProperty(r, p) )
+ if ( !GraphUtils.atMostOneProperty(r, p) )
throw new NotUniqueException(r, p);
Statement s = r.getProperty(p);
if ( s == null )
@@ -249,7 +300,7 @@ public class GraphUtils {
}
public static RDFNode getAsRDFNode(Resource r, Property p) {
- if ( !atmostOneProperty(r, p) )
+ if ( !atMostOneProperty(r, p) )
throw new NotUniqueException(r, p);
Statement s = r.getProperty(p);
if ( s == null )
@@ -258,7 +309,7 @@ public class GraphUtils {
}
public static Resource getResourceValue(Resource r, Property p) {
- if ( !atmostOneProperty(r, p) )
+ if ( !atMostOneProperty(r, p) )
throw new NotUniqueException(r, p);
Statement s = r.getProperty(p);
if ( s == null )
@@ -319,7 +370,7 @@ public class GraphUtils {
return distinctIterator;
}
- static class IterSO extends NiceIterator<Node> {
+ private static class IterSO extends NiceIterator<Node> {
private ExtendedIterator<Triple> it;
private boolean tripleConsumed;
private Triple triple;
diff --git
a/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AssemblerAccessDataset.java
b/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AssemblerAccessDataset.java
index ec3431d5ef..12a1a049a1 100644
---
a/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AssemblerAccessDataset.java
+++
b/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AssemblerAccessDataset.java
@@ -42,9 +42,9 @@ public class AssemblerAccessDataset extends AssemblerBase {
*/
@Override
public Dataset open(Assembler a, Resource root, Mode mode) {
- if ( ! GraphUtils.exactlyOneProperty(root,
VocabSecurity.pSecurityRegistry) )
+ if ( ! GraphUtils.checkExactlyOneProperty(root,
VocabSecurity.pSecurityRegistry) )
throw new AssemblerException(root, "Expected exactly one
access:registry property");
- if ( ! GraphUtils.exactlyOneProperty(root, VocabSecurity.pDataset) )
+ if ( ! GraphUtils.checkExactlyOneProperty(root,
VocabSecurity.pDataset) )
throw new AssemblerException(root, "Expected exactly one
access:dataset property");
RDFNode rnRegistry =
root.getProperty(VocabSecurity.pSecurityRegistry).getObject();
diff --git
a/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AssemblerSecurityRegistry.java
b/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AssemblerSecurityRegistry.java
index 3e734a177f..1ee932f850 100644
---
a/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AssemblerSecurityRegistry.java
+++
b/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AssemblerSecurityRegistry.java
@@ -109,9 +109,9 @@ public class AssemblerSecurityRegistry extends
AssemblerBase {
/** Format:: access:entry [ :user "user2"; :graphs
(<http://host/graphname3> ) ] */
private void parseStruct(MultiValuedMap<String, Node> map, Resource root,
Resource r) {
- if ( ! GraphUtils.exactlyOneProperty(r, VocabSecurity.pUser) )
+ if ( ! GraphUtils.checkExactlyOneProperty(r, VocabSecurity.pUser) )
throw new AssemblerException(root, "Expected exactly one
access:user property for "+r);
- if ( ! GraphUtils.exactlyOneProperty(r, VocabSecurity.pGraphs) )
+ if ( ! GraphUtils.checkExactlyOneProperty(r, VocabSecurity.pGraphs) )
throw new AssemblerException(root, "Expected exactly one
access:graphs property for "+r);
String user = GraphUtils.getStringValue(r, VocabSecurity.pUser);
diff --git
a/jena-rdfpatch/src/main/java/org/apache/jena/rdfpatch/filelog/AssemblerFileLog.java
b/jena-rdfpatch/src/main/java/org/apache/jena/rdfpatch/filelog/AssemblerFileLog.java
index 4961b533c3..7878291be7 100644
---
a/jena-rdfpatch/src/main/java/org/apache/jena/rdfpatch/filelog/AssemblerFileLog.java
+++
b/jena-rdfpatch/src/main/java/org/apache/jena/rdfpatch/filelog/AssemblerFileLog.java
@@ -21,7 +21,7 @@
package org.apache.jena.rdfpatch.filelog;
-import static org.apache.jena.sparql.util.graph.GraphUtils.exactlyOneProperty;
+import static
org.apache.jena.sparql.util.graph.GraphUtils.checkExactlyOneProperty;
import java.util.List;
@@ -69,7 +69,7 @@ public class AssemblerFileLog extends AssemblerBase {
@Override
public Object open(Assembler a, Resource root, Mode mode) {
- if ( !exactlyOneProperty(root, VocabPatch.pDataset) )
+ if ( !checkExactlyOneProperty(root, VocabPatch.pDataset) )
throw new AssemblerException(root, "No dataset to be logged");
if ( !root.hasProperty(VocabPatch.pLogFile) )
throw new AssemblerException(root, "No log file");
diff --git
a/jena-tdb2/src/main/java/org/apache/jena/tdb2/assembler/DatasetAssemblerTDB2.java
b/jena-tdb2/src/main/java/org/apache/jena/tdb2/assembler/DatasetAssemblerTDB2.java
index f1466ca036..39d7e3838b 100644
---
a/jena-tdb2/src/main/java/org/apache/jena/tdb2/assembler/DatasetAssemblerTDB2.java
+++
b/jena-tdb2/src/main/java/org/apache/jena/tdb2/assembler/DatasetAssemblerTDB2.java
@@ -21,7 +21,7 @@
package org.apache.jena.tdb2.assembler;
-import static org.apache.jena.sparql.util.graph.GraphUtils.exactlyOneProperty;
+import static
org.apache.jena.sparql.util.graph.GraphUtils.checkExactlyOneProperty;
import static org.apache.jena.sparql.util.graph.GraphUtils.getAsFilename;
import static org.apache.jena.tdb2.assembler.VocabTDB2.pLocation;
import static org.apache.jena.tdb2.assembler.VocabTDB2.pUnionDefaultGraph;
@@ -54,7 +54,7 @@ public class DatasetAssemblerTDB2 extends DatasetAssembler
}
public static DatasetGraph make(Assembler a, Resource root) {
- if ( !exactlyOneProperty(root, pLocation) )
+ if ( !checkExactlyOneProperty(root, pLocation) )
throw new AssemblerException(root, "No location given");
String dir = getAsFilename(root, pLocation);
diff --git
a/jena-text/src/main/java/org/apache/jena/query/text/assembler/TextIndexLuceneAssembler.java
b/jena-text/src/main/java/org/apache/jena/query/text/assembler/TextIndexLuceneAssembler.java
index 2316289530..bea441a670 100644
---
a/jena-text/src/main/java/org/apache/jena/query/text/assembler/TextIndexLuceneAssembler.java
+++
b/jena-text/src/main/java/org/apache/jena/query/text/assembler/TextIndexLuceneAssembler.java
@@ -34,11 +34,12 @@ import org.apache.jena.query.text.*;
import org.apache.jena.rdf.model.RDFNode ;
import org.apache.jena.rdf.model.Resource ;
import org.apache.jena.rdf.model.Statement ;
-import org.apache.jena.sparql.util.graph.GraphUtils ;
import org.apache.lucene.analysis.Analyzer ;
import org.apache.lucene.store.*;
import static org.apache.jena.query.text.assembler.TextVocab.*;
+import static
org.apache.jena.sparql.util.graph.GraphUtils.checkExactlyOneProperty;
+import static org.apache.jena.sparql.util.graph.GraphUtils.getResourceValue;
public class TextIndexLuceneAssembler extends AssemblerBase {
/*
@@ -53,7 +54,7 @@ public class TextIndexLuceneAssembler extends AssemblerBase {
@Override
public TextIndex open(Assembler a, Resource root, Mode mode) {
try {
- if ( !GraphUtils.exactlyOneProperty(root, pDirectory) )
+ if ( !checkExactlyOneProperty(root, pDirectory) )
throw new TextIndexException("No 'text:directory' property on
" + root) ;
Directory directory ;
@@ -200,7 +201,7 @@ public class TextIndexLuceneAssembler extends AssemblerBase
{
cacheQueries = cqNode.asLiteral().getBoolean();
}
- Resource r = GraphUtils.getResourceValue(root, pEntityMap) ;
+ Resource r = getResourceValue(root, pEntityMap) ;
EntityDefinition docDef = (EntityDefinition)a.open(r) ;
TextIndexConfig config = new TextIndexConfig(docDef);
config.setAnalyzer(analyzer);