public final class Record extends Object implements Serializable, Comparable<Record>
A record is a structured identified by an URI
ID and having a number of key-value
properties, where the key is a URI
and the value is a non-empty list of objects, which
can be other Record
s, URI
s, BNode
s, Literal
s or
Statement
s. Records are used to carry the data of resources, representations, mentions,
entities, axioms, contexts and of any structured property value.
Records are created via factory methods create()
:
create()
creates an empty record, without ID and properties;create(URI, URI...)
creates a new record with the ID and the types (values
of property rdf:type
supplied;create(Record, boolean)
creates a copy of a supplied method, possibly
performing deep-cloning of its properties.
Record equality (equals(Object)
, hashCode()
) is defined in terms of the
record ID only, while toString()
emits only the record type and ID. Beware that the ID
can change during a Record
lifetime (via method setID(URI)
): this provides for
increased flexibility, but pay attention not to change the ID when storing records in indexed
data structure such as Set
s and Map
s, which rely on hashCode()
and
equals()
to produce constant outcomes. Additional method #toString(boolean)
allows for emitting a complete record representation including its properties, while equality
of (selected) properties in different records can be checked by comparing the respective
hashes, computed via hash(URI...)
; the same hash()
method can help in creating
syntetic IDs based on the values of some properties (e.g., following a pattern
PREFIX + record.hash(p1, p2, ...)
.
Access to and manipulation of properties is performed as follows:
getProperties()
returns the list of
properties having some value for a record instance.get(URI)
, which is complemented by a
number of auxiliary methods for ease of use. They are described below:
get(URI)
, get(URI, Class)
and get(URI, Class, List)
allow
retrieving all the values of a specific property, either as a list of objects or converted to a
specific target class; the last methed supports also the specification of a default value,
which is returned if the property has no values or if conversion fails.getUnique(URI)
, getUnique(URI, Class)
,
getUnique(URI, Class, Object)
allow retrieving the unique value of a property, either
as an object or converted to a specific class; unless a default value is specified, the methods
fail in case multiple values are associated to the property, thus helping enforcing the
uniqueness expectation.isTrue(URI)
, isFalse(URI)
are convenience methods that can be used for
boolean properties; they fail if used on properties having multiple or non-boolean values.isNull(URI)
, isUnique(URI)
are convenience methods that can be used to
test whether a property has at least or at most one value.count(URI)
is a convenience method for counting the values of a property; it may
be faster than using get()
.set(URI, Object, Object...)
,
add(URI, Object, Object...)
and remove(URI, Object, Object...)
, that allow,
respectively, to set all the values of a property, to add some new values to a property or to
remove existing values from the values of a property. For ease of use, these methods accept (at
least) an argument object which can be a Record
, URI
, BNode
,
Statement
, Literal
, an object convertible to Literal
or any array or
iterable of the former types. A list of values is extracted from the supplied objects, and used
for modifying the values of the property.clear(URI...)
and
retain(URI...)
, which remove all the properties respectively matching or not matching
a supplied list, allowing as a special case (no properties specified) to remove all the
properties of a record instance.
Instances of this interface are thread safe. Cloning of record instances (via
create(Record, boolean)
) is supported and is a relatively inexpensive operation; a
copy-on-write approach is adopted to reduce the memory usage of cloned objects, which share
their state with the source object as long as one of the two is changed.
Modifier and Type | Method and Description |
---|---|
Record |
add(org.openrdf.model.URI property,
Object first,
Object... other)
Adds one or more values to the property specified.
|
Record |
clear(org.openrdf.model.URI... properties)
Clears the properties specified, or all the stored properties if no property is specified.
|
int |
compareTo(Record other)
Comparison is based on the record IDs only.
|
int |
count(org.openrdf.model.URI property)
Returns the number of values assigned to the property specified.
|
static Record |
create()
Creates a new record with no properties and ID assigned.
|
static Record |
create(Record record,
boolean deepClone)
Creates a new record having the same ID and properties of the supplied record, possibly
performing a deep-cloning (copy constructor).
|
static Record |
create(org.openrdf.model.URI id,
org.openrdf.model.URI... types)
Creates a new record with the ID and the types specified (property
rdf:type ), and
no additional properties. |
static Stream<Record> |
decode(Stream<org.openrdf.model.Statement> stream,
Iterable<? extends org.openrdf.model.URI> types,
Boolean chunked)
Performs RDF-to-record decoding by converting a stream of RDF statements in a stream of
records.
|
static Stream<org.openrdf.model.Statement> |
encode(Stream<? extends Record> stream,
Iterable<? extends org.openrdf.model.URI> types)
Performs record-to-RDF encoding by converting a stream of records in a stream of RDF
statements.
|
boolean |
equals(Object object)
Two records are equal if they have the same IDs.
|
List<Object> |
get(org.openrdf.model.URI property)
Returns the
Object values of the property specified. |
<T> List<T> |
get(org.openrdf.model.URI property,
Class<T> valueClass)
Returns the values of the property converted to instances of a certain class.
|
<T> List<T> |
get(org.openrdf.model.URI property,
Class<T> valueClass,
List<T> defaultValue)
Returns the values of the property converted to instances of a certain class, or the
default value supplied in case of failure or if the property has no values.
|
org.openrdf.model.URI |
getID()
Returns the ID of this record.
|
List<org.openrdf.model.URI> |
getProperties()
Returns all the properties currently defined for this record.
|
org.openrdf.model.URI |
getSystemType()
Returns the system type for this record, i.e., the
rdf:type URI under the
ks: namespace, if any. |
Object |
getUnique(org.openrdf.model.URI property)
Returns the unique
Object value of a property, or null if it has no value. |
<T> T |
getUnique(org.openrdf.model.URI property,
Class<T> valueClass)
Returns the unique value of the property converted to an instance of a certain class, or
null if the property has no value.
|
<T> T |
getUnique(org.openrdf.model.URI property,
Class<T> valueClass,
T defaultValue)
Returns the unique value of the property converted to an instance of a certain class, or
the default value supplied in case of failure.
|
String |
hash(org.openrdf.model.URI... properties)
Computes a string-valued hash code of the properties specified, or of all the available
properties, if no URI is specified.
|
int |
hashCode()
The returned hash code depends only on the record ID.
|
boolean |
isFalse(org.openrdf.model.URI property)
Determines whether the property specified has been set to false.
|
boolean |
isNull(org.openrdf.model.URI property)
Determines whether the property specified is null, i.e., it has no value.
|
boolean |
isTrue(org.openrdf.model.URI property)
Determines whether the property specified has been set to true.
|
boolean |
isUnique(org.openrdf.model.URI property)
Determines whether the property specified has at most one value.
|
Record |
remove(org.openrdf.model.URI property,
Object first,
Object... other)
Removes one or more values from the property specified.
|
Record |
retain(org.openrdf.model.URI... properties)
Retains only the properties specified, clearing the remaining ones.
|
Record |
set(org.openrdf.model.URI property,
Object first,
Object... other)
Sets the values of the property specified.
|
Record |
setID(org.openrdf.model.URI id)
Sets the ID of this record.
|
String |
toString()
The returned string contains only the ID of the record.
|
String |
toString(Map<String,String> namespaces,
boolean includeProperties)
Returns a string representation of the record, optionally using the namespaces supplied and
emitting record properties.
|
public static Record create()
public static Record create(org.openrdf.model.URI id, org.openrdf.model.URI... types)
rdf:type
), and
no additional properties.id
- the ID of the new record, possibly null in order not to assign ittypes
- the types of the record, assigned to property rdf:type
public static Record create(Record record, boolean deepClone)
Record
type, which are
shared by the source and cloned object in case of shallow-cloning, and cloned themselves in
case of deep-cloning.record
- the reference record to clonedeepClone
- true to perform a deep-cloning, false to perform a shallow-cloning@Nullable public org.openrdf.model.URI getID()
public Record setID(@Nullable org.openrdf.model.URI id)
id
- the new ID of this record or null to clear it@Nullable public org.openrdf.model.URI getSystemType() throws IllegalArgumentException
rdf:type
URI under the
ks:
namespace, if any.IllegalArgumentException
- in case multiple system types are bound to the recordpublic List<org.openrdf.model.URI> getProperties()
public boolean isNull(org.openrdf.model.URI property)
property
- the property to readpublic boolean isUnique(org.openrdf.model.URI property)
property
- the property to readpublic boolean isTrue(org.openrdf.model.URI property) throws IllegalStateException, IllegalArgumentException
getUnique(URI, Class, Object)
specifying Boolean.class
as the class
and an appropriate default value to be returned in case of failure.property
- the property to readIllegalStateException
- in case the property has multiple valuesIllegalArgumentException
- in case the property value is not of boolean typepublic boolean isFalse(org.openrdf.model.URI property) throws IllegalStateException, IllegalArgumentException
getUnique(URI, Class, Object)
specifying Boolean.class
as the class
and an appropriate default value to be returned in case of failure.property
- the property to readIllegalStateException
- in case the property has multiple valuesIllegalArgumentException
- in case the property value is not of boolean typepublic int count(org.openrdf.model.URI property)
get(URI)
.property
- the property@Nullable public Object getUnique(org.openrdf.model.URI property) throws IllegalStateException
Object
value of a property, or null if it has no value. Note
that this method fails if the property has multiple values; if this is not the desired
behaviour, use getUnique(URI, Class, Object)
supplying an appropriate type (could
be Object.class
) and default value to be returned in case of failure.property
- the property to readObject
value of the property; null if it has no valueIllegalStateException
- in case the property has multiple values@Nullable public <T> T getUnique(org.openrdf.model.URI property, Class<T> valueClass) throws IllegalStateException, IllegalArgumentException
getUnique(URI, Class, Object)
supplying an appropriate
default value to be returned in case of failure.T
- the type of resultproperty
- the property to readvalueClass
- the class to convert the value toIllegalStateException
- in case the property has multiple valuesIllegalArgumentException
- in case the unique property value cannot be converted to the class specified@Nullable public <T> T getUnique(org.openrdf.model.URI property, Class<T> valueClass, @Nullable T defaultValue)
T
- the type of resultproperty
- the property to readvalueClass
- the class to convert the value todefaultValue
- the default value to return in case the property has no valuepublic List<Object> get(org.openrdf.model.URI property)
Object
values of the property specified.property
- the property to readObject
values of the property, without
repetitions, in no particular order and possibly emptypublic <T> List<T> get(org.openrdf.model.URI property, Class<T> valueClass) throws IllegalArgumentException
get(URI, Class, List)
specifying an
appropriate default value to be returned in case of conversion failure.T
- the type of property valuesproperty
- the property to readvalueClass
- the class values have to be converted toIllegalArgumentException
- in case one of the property values cannot be converted to the class specifiedpublic <T> List<T> get(org.openrdf.model.URI property, Class<T> valueClass, List<T> defaultValue)
T
- the type of property valuesproperty
- the property to readvalueClass
- the class values have to be converted todefaultValue
- the default value to return in case conversion failspublic Record set(org.openrdf.model.URI property, @Nullable Object first, Object... other) throws IllegalArgumentException
Record
s, URI
s, BNode
s,
Statement
s, Literal
s, objects convertible to Literal
or any array
or iterable of the former types. Setting a property to null has the effect of clearing it.property
- the property to setfirst
- the first value, array or iterable of values to set, possibly nullother
- additional values, arrays or iterables of values to set (if specified, will be
merged with first
).IllegalArgumentException
- if one of the supplied values has an unsupported typepublic Record add(org.openrdf.model.URI property, @Nullable Object first, Object... other) throws IllegalArgumentException
Record
s, URI
s, BNode
s,
Statement
s, Literal
s, objects convertible to Literal
or any array
or iterable of the former types.property
- the property to modifyfirst
- the first value, array or iterable of values to add, possibly nullother
- additional values, arrays or iterables of values to set (if specified, will be
merged with first
).IllegalArgumentException
- if one of the supplied values has an unsupported typepublic Record remove(org.openrdf.model.URI property, @Nullable Object first, Object... other) throws IllegalArgumentException
Record
s, URI
s, BNode
s,
Statement
s, Literal
s, objects convertible to Literal
or any array
or iterable of the former types.property
- the property to modifyfirst
- the first value, array or iterable of values to remove, possibly nullother
- additional values, arrays or iterables of values to remove (if specified, will
be merged with first
).IllegalArgumentException
- if one of the supplied values has an unsupported typepublic Record retain(org.openrdf.model.URI... properties)
properties
- an array with the properties to retain, possibly empty (in which case all the
stored properties will be cleared)public Record clear(org.openrdf.model.URI... properties)
properties
- an array with the properties to retain, possibly empty (in which case all the
stored properties will be cleared)public int compareTo(Record other)
compareTo
in interface Comparable<Record>
public boolean equals(Object object)
public int hashCode()
public String hash(org.openrdf.model.URI... properties)
properties
- the properties to hash.public String toString(@Nullable Map<String,String> namespaces, boolean includeProperties)
#toString()
, optionally allowing to
emit also record properties and, recursively, properties of records nested in this record.namespaces
- the prefix-to-namespace mappings to be used when emitting property and value
URIs; if null, only non-abbreviated, full URIs will be emittedincludeProperties
- true if record properties should be emitted tooincludeProperties
settingpublic String toString()
public static Stream<org.openrdf.model.Statement> encode(Stream<? extends Record> stream, @Nullable Iterable<? extends org.openrdf.model.URI> types)
types
specify additional types to be added to encoded
records. Type information may be set to null (e.g., because unknown at the time the method
is called): in this case, it will be read from metadata attribute "types"
attached
to the stream; reading will happen just before decoding will take place, i.e., when a
terminal stream operation will be called.stream
- the stream of records to encode.types
- the types to be added to each record of the stream, null if to be read from
stream metadatapublic static Stream<Record> decode(Stream<org.openrdf.model.Statement> stream, @Nullable Iterable<? extends org.openrdf.model.URI> types, @Nullable Boolean chunked)
types
specify the types of records that have to be extracted
from the statement stream, while parameter chunked
specifies whether the input
statement stream is chunked, i.e., organized as a sequence of statement chunks with each
chunk containing the statements for a record (and its nested records). Chunked RDF streams
noticeably speed up decoding, and are always produced by the KnowledgeStore API. Type and
chunking information may be set to null (e.g., because unknown at the time the method is
called): in this case, they will be read from metadata attributes attached to the stream,
named "types"
and "chunked"
; reading will happen just before decoding will
take place, i.e., when a terminal stream operation will be called.stream
- the stream of statements to decodetypes
- the types of records to extract from the statement stream, null if to be read
from stream metadatachunked
- true if the input statement stream is chunked, null if to be read from stream
metadataCopyright © 2015–2016 FBK-irst. All rights reserved.