Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/
package org.eclipse.rdf4j.model.util;

import java.util.Optional;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;

/**
* Either supplies a statement matching the given pattern, or
* {@link Optional#empty()} otherwise.
*
* @author Peter Ansell
*/
@FunctionalInterface
public interface GetStatementOptional {

/**
* Either supplies a statement matching the given pattern, or
* {@link Optional#empty()} otherwise.
*
* @param subject
* A {@link Resource} to be used to match to statements.
* @param predicate
* An {@link IRI} to be used to match to statements.
* @param object
* A {@link Value} to be used to match to statements.
* @param contexts
* An array of context {@link Resource} objects, or left out (not
* null) to select from all contexts.
* @return An {@link Optional} either containing a single statement matching
* the pattern or {@link Optional#empty()} otherwise.
*/
Optional<Statement> get(Resource subject, IRI predicate, Value object, Resource... contexts);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*******************************************************************************/
package org.eclipse.rdf4j.model.util;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.OpenRDFException;
import org.eclipse.rdf4j.model.Value;

/**
Expand All @@ -16,7 +16,7 @@
*
* @author Arjohn Kampman
*/
public class ModelException extends RuntimeException {
public class ModelException extends OpenRDFException {

private static final long serialVersionUID = 3886967415616842867L;

Expand Down
36 changes: 27 additions & 9 deletions core/model/src/main/java/org/eclipse/rdf4j/model/util/Models.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import java.util.function.Supplier;

import org.eclipse.rdf4j.model.BNode;
import org.eclipse.rdf4j.model.IRI;
Expand Down Expand Up @@ -79,7 +79,8 @@ public static Value anyObject(Model m) {
* @since 4.0
*/
public static Optional<Literal> objectLiteral(Model m) {
return m.stream().map(st -> st.getObject()).filter(o -> o instanceof Literal).map(l -> (Literal)l).findAny();
return m.stream().map(st -> st.getObject()).filter(o -> o instanceof Literal).map(
l -> (Literal)l).findAny();
}

/**
Expand All @@ -103,7 +104,8 @@ public static Literal anyObjectLiteral(Model m) {
* @since 4.0
*/
public static Optional<Resource> objectResource(Model m) {
return m.stream().map(st -> st.getObject()).filter(o -> o instanceof Resource).map(r -> (Resource)r).findAny();
return m.stream().map(st -> st.getObject()).filter(o -> o instanceof Resource).map(
r -> (Resource)r).findAny();
}

/**
Expand All @@ -128,16 +130,17 @@ public static Resource anyObjectResource(Model m) {
public static Optional<IRI> objectIRI(Model m) {
return m.stream().map(st -> st.getObject()).filter(o -> o instanceof IRI).map(r -> (IRI)r).findAny();
}

/**
* Retrieves an object value as a String from the statements in the given
* model. If more than one possible object value exists, any one value is picked
* and returned.
* model. If more than one possible object value exists, any one value is
* picked and returned.
*
* @param m
* the model from which to retrieve an object String value.
* @return an {@link Optional} object String value from the given model, which
* will be {@link Optional#empty() empty} if no such value exists.
* @return an {@link Optional} object String value from the given model,
* which will be {@link Optional#empty() empty} if no such value
* exists.
* @since 4.0
*/
public static Optional<String> objectString(Model m) {
Expand Down Expand Up @@ -210,7 +213,8 @@ public static URI anySubjectURI(Model m) {
* @since 4.0
*/
public static Optional<BNode> subjectBNode(Model m) {
return m.stream().map(st -> st.getSubject()).filter(s -> s instanceof BNode).map(s -> (BNode)s).findAny();
return m.stream().map(st -> st.getSubject()).filter(s -> s instanceof BNode).map(
s -> (BNode)s).findAny();
}

/**
Expand Down Expand Up @@ -573,4 +577,18 @@ private static <S extends Statement> Set<S> toSet(Iterable<S> iterable) {
}
return set;
}

/**
* Creates a {@link Supplier} of {@link ModelException} objects that be
* passed to {@link Optional#orElseThrow(Supplier)} to generate exceptions as
* necessary.
*
* @param message
* The message to be used for the exception
* @return A {@link Supplier} that will create {@link ModelException} objects
* with the given message.
*/
public static Supplier<ModelException> modelException(String message) {
return () -> new ModelException(message);
}
}
Loading