@Immutable @BuiltBy(value=BasicEntityBuilder.class) public interface Entity
Base class for data entities in LensKit. The LensKit data model consists of various entities, each of which has a type, a numeric identifier (as a long
), and additional data (attributes) in a key-value mapping.
Two entities are equal if their types, IDs, and data are equal.
Identifiers are scoped per-type; it is acceptable to LensKit for two entities with different types to have the same identifier, and they are not considered to be the same entity.
Attributes are identified by name; for type safety, the are often accessed using typed names, such as those defined in CommonAttributes
. Attribute values cannot be null; if you must safely handle the absence of an attribute, test with hasAttribute(TypedName)
or use maybeGet(TypedName)
.
Modifier and Type | Method and Description |
---|---|
Map<String,Object> |
asMap()
View this entity as a map.
|
Object |
get(String attr)
Get the value of an attribute by name.
|
<T> T |
get(TypedName<T> name)
Get the value of an attribute.
|
Set<String> |
getAttributeNames()
Get the names of the attributes in this entity.
|
Collection<Attribute<?>> |
getAttributes()
Get the attribute-value pairs.
|
boolean |
getBoolean(TypedName<Boolean> name)
Get the value of a attr that contains a boolean.
|
double |
getDouble(TypedName<Double> name)
Get the value of a attr that contains a double.
|
long |
getId()
Get the identifier of this entity.
|
int |
getInteger(TypedName<Integer> name)
Get the value of a attr that contains a int.
|
long |
getLong(TypedName<Long> name)
Get the value of a attribute that contains a long.
|
EntityType |
getType()
Get the type of this entity.
|
Set<TypedName<?>> |
getTypedAttributeNames()
Get the attributes in this entity.
|
boolean |
hasAttribute(String name)
Check if the entity has a field with a particular name.
|
boolean |
hasAttribute(TypedName<?> name)
Check if the entity has a attribute.
|
Object |
maybeGet(String attr)
Get the value of a possibly-missing attribute by name.
|
<T> T |
maybeGet(TypedName<T> name)
Get the value of a possibly-missing attribute.
|
long getId()
Get the identifier of this entity.
EntityType getType()
Get the type of this entity.
Set<TypedName<?>> getTypedAttributeNames()
Get the attributes in this entity.
Collection<Attribute<?>> getAttributes()
Get the attribute-value pairs.
Map<String,Object> asMap()
View this entity as a map.
boolean hasAttribute(String name)
Check if the entity has a field with a particular name.
name
- The field name to look for.true
if the entity has a field named name
.boolean hasAttribute(TypedName<?> name)
Check if the entity has a attribute.
name
- The attribute name to look for.true
if the entity contains attribute
and the value is of the associated type. that the data is of a compatible type.@Nonnull <T> T get(TypedName<T> name)
Get the value of an attribute.
name
- The attribute name.T
- The attribute’s type.NoSuchAttributeException
- if the specified attribute is not present.IllegalArgumentException
- if a attribute with the same name as attribute
is present, but it is of an incompatible type.@Nonnull Object get(String attr)
Get the value of an attribute by name.
attr
- The attribute name.NoSuchAttributeException
- if the specified attribute is not present.@Nullable <T> T maybeGet(TypedName<T> name)
Get the value of a possibly-missing attribute.
name
- The attribute name.T
- The attribute’s type.null
if it is not present.IllegalArgumentException
- if a attribute with the same name as attribute
is present, but it is of an incompatible type.@Nullable Object maybeGet(String attr)
Get the value of a possibly-missing attribute by name.
attr
- The attribute name.null
if it is not present.long getLong(TypedName<Long> name)
Get the value of a attribute that contains a long.
name
- The attribute name.NoSuchAttributeException
- if the specified attribute is not present.IllegalArgumentException
- if the attribute is present but its type is not Long
.double getDouble(TypedName<Double> name)
Get the value of a attr that contains a double.
name
- The attribute name.NoSuchAttributeException
- if the specified attr is not present.IllegalArgumentException
- if the attr is present but is not of type Double
.int getInteger(TypedName<Integer> name)
Get the value of a attr that contains a int.
name
- The attribute name.NoSuchAttributeException
- if the specified attr is not present.IllegalArgumentException
- if the attr is present but is not of type Integer
.boolean getBoolean(TypedName<Boolean> name)
Get the value of a attr that contains a boolean.
name
- The attribute name.NoSuchAttributeException
- if the specified attr is not present.IllegalArgumentException
- if the attr is present but is not of type Boolean
.