@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 |
---|---|
java.util.Map<java.lang.String,java.lang.Object> |
asMap()
View this entity as a map.
|
java.lang.Object |
get(java.lang.String attr)
Get the value of an attribute by name.
|
<T> T |
get(TypedName<T> name)
Get the value of an attribute.
|
java.util.Set<java.lang.String> |
getAttributeNames()
Get the names of the attributes in this entity.
|
java.util.Collection<Attribute<?>> |
getAttributes()
Get the attribute-value pairs.
|
boolean |
getBoolean(TypedName<java.lang.Boolean> name)
Get the value of a attr that contains a boolean.
|
double |
getDouble(TypedName<java.lang.Double> name)
Get the value of a attr that contains a double.
|
long |
getId()
Get the identifier of this entity.
|
int |
getInteger(TypedName<java.lang.Integer> name)
Get the value of a attr that contains a int.
|
long |
getLong(TypedName<java.lang.Long> name)
Get the value of a attribute that contains a long.
|
EntityType |
getType()
Get the type of this entity.
|
java.util.Set<TypedName<?>> |
getTypedAttributeNames()
Get the attributes in this entity.
|
boolean |
hasAttribute(java.lang.String name)
Check if the entity has a field with a particular name.
|
boolean |
hasAttribute(TypedName<?> name)
Check if the entity has a attribute.
|
java.lang.Object |
maybeGet(java.lang.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.
java.util.Set<java.lang.String> getAttributeNames()
Get the names of the attributes in this entity.
java.util.Set<TypedName<?>> getTypedAttributeNames()
Get the attributes in this entity.
java.util.Collection<Attribute<?>> getAttributes()
Get the attribute-value pairs.
java.util.Map<java.lang.String,java.lang.Object> asMap()
View this entity as a map.
boolean hasAttribute(java.lang.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.@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.java.lang.IllegalArgumentException
- if a attribute with the same name as attribute
is present, but it is of an incompatible type.@Nonnull java.lang.Object get(java.lang.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.java.lang.IllegalArgumentException
- if a attribute with the same name as attribute
is present, but it is of an incompatible type.@Nullable java.lang.Object maybeGet(java.lang.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<java.lang.Long> name)
Get the value of a attribute that contains a long.
name
- The attribute name.NoSuchAttributeException
- if the specified attribute is not present.java.lang.IllegalArgumentException
- if the attribute is present but its type is not Long
.double getDouble(TypedName<java.lang.Double> name)
Get the value of a attr that contains a double.
name
- The attribute name.NoSuchAttributeException
- if the specified attr is not present.java.lang.IllegalArgumentException
- if the attr is present but is not of type Double
.int getInteger(TypedName<java.lang.Integer> name)
Get the value of a attr that contains a int.
name
- The attribute name.NoSuchAttributeException
- if the specified attr is not present.java.lang.IllegalArgumentException
- if the attr is present but is not of type Integer
.boolean getBoolean(TypedName<java.lang.Boolean> name)
Get the value of a attr that contains a boolean.
name
- The attribute name.NoSuchAttributeException
- if the specified attr is not present.java.lang.IllegalArgumentException
- if the attr is present but is not of type Boolean
.