# All the nodes in a `NeoGraph`.
#
# An identification scheme can be defined throught the `register` and `add`
# methods. The `id_property` attribute defines where the local ID (that is the
# ID managed by the collection) is stored in each node.
abstract class NeoNodeCollection
super SimpleCollection[NeoNode]
# The type of the local IDs.
type ID_TYPE: Serializable
# The property of the nodes that hold the local ID.
var id_property: String
# Retrieve the node that has the specified local id.
#
# Note: The default implementation uses `get_or_null`.
fun [](id: ID_TYPE): NeoNode do
var n = get_or_null(id)
assert n isa NeoNode
return n
end
# Retrieve the node that has the specified local id, or return `null`.
#
# Note: The default implementation uses `iterator`.
fun get_or_null(id: ID_TYPE): nullable NeoNode do
for n in self do
if id_of(n) == id then return n
end
return null
end
# There is a node that has the specified local id?
#
# Note: The default implementation uses `get_or_null`.
fun has_id(id: ID_TYPE): Bool do return get_or_null(id) isa NeoNode
# Return the local ID of the node.
fun id_of(node: NeoNode): ID_TYPE do return node[id_property].as(ID_TYPE)
# Set the local ID of the specified node.
#
# Just update the property at `property_id`. Do not check anything.
protected fun id_of=(node: NeoNode, id: ID_TYPE) do
node[id_property] = id
end
# Enlarge the collection to have at least the specified capacity.
#
# The capacity is specified in number of nodes. Used to minimize the
# number of times the collection need to be resized when adding nodes
# in batches.
#
# Do nothing by default.
fun enlarge(cap: Int) do end
# Add the specified node to the graph and set its local ID.
#
# SEE: `add`
# SEE: `create_node`
fun register(node: NeoNode) is abstract
# Add the specified node to the graph assuming that its local ID is already set.
#
# SEE: `create_node`
# SEE: `register`
redef fun add(node) is abstract
# Add a new node to the graph and return it.
#
# Set the local ID of the node before returning it.
#
# SEE: `add`
# SEE: `register`
fun create_node: NeoNode do
var node = new NeoNode
register(node)
return node
end
# Remove the node with the specified local ID.
fun remove_at(id: ID_TYPE) is abstract
# Remove the specified node.
#
# The local ID is used instead of `==` to seek the node.
fun remove_node(node: NeoNode) do
remove_at(id_of(node))
end
redef fun clear do
for node in self do remove_node(node)
end
redef fun remove(node) do
for n in self do
if node == n then
remove_node(n)
return
end
end
end
redef fun remove_all(node) do
for n in self do
if node == n then remove_node(n)
end
end
# Optimize the collection, possibly by rewritting it.
#
# The local ID of the elements may be changed by this method.
fun compact do end
end
lib/neo4j/graph/graph.nit:41,1--155,3