Skip to content

Commit 4e7a05d

Browse files
committed
GH-4931 add support for getSubjectByType
1 parent c39f32a commit 4e7a05d

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

core/model/src/main/java/org/eclipse/rdf4j/model/util/Configurations.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.eclipse.rdf4j.model.Model;
2222
import org.eclipse.rdf4j.model.Resource;
2323
import org.eclipse.rdf4j.model.Value;
24+
import org.eclipse.rdf4j.model.vocabulary.RDF;
2425
import org.slf4j.Logger;
2526
import org.slf4j.LoggerFactory;
2627

@@ -42,7 +43,6 @@ public class Configurations {
4243
*
4344
* @return <code>true</code> if <code>org.eclipse.rdf4j.model.vocabulary.useLegacyConfig</code> system property is
4445
* set to <code>true</code>, <code>false</code> otherwise.
45-
*
4646
* @since 5.0.0
4747
*/
4848
public static boolean useLegacyConfig() {
@@ -181,6 +181,30 @@ public static Optional<IRI> getIRIValue(Model model, Resource subject, IRI prope
181181
return fallbackResult;
182182
}
183183

184+
/**
185+
* Retrieve the subject of the supplied type, falling back to a supplied legacy type.
186+
*
187+
* @param model the model to retrieve property values from.
188+
* @param type the type to retrieve the value of.
189+
* @param legacyType legacy type to use if the supplied type has no value in the model.
190+
* @return The subject of the supplied type (or the legacy type), if present.
191+
*/
192+
@InternalUseOnly
193+
public static Optional<Resource> getSubjectByType(Model model, IRI type, IRI legacyType) {
194+
var preferredType = useLegacyConfig() ? legacyType : type;
195+
var fallbackType = useLegacyConfig() ? type : legacyType;
196+
197+
var preferredResult = Models.subject(model.getStatements(null, RDF.TYPE, preferredType));
198+
var fallbackResult = Models.subject(model.getStatements(null, RDF.TYPE, fallbackType));
199+
200+
logDiscrepancyWarning(preferredResult, fallbackResult);
201+
if (preferredResult.isPresent()) {
202+
return preferredResult;
203+
}
204+
205+
return fallbackResult;
206+
}
207+
184208
private static void logDiscrepancyWarning(Optional<? extends Value> preferred,
185209
Optional<? extends Value> fallback) {
186210
if (!fallback.isEmpty() && !preferred.equals(fallback)) {

0 commit comments

Comments
 (0)