Node
Keywords: org.neo4j.api.core.Node interface

Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.neo4j.api.core
Interface Node

    public interface Node

Represents a node in the network with properties and relationships to other entities. Along with `relationships`, nodes are the core building blocks of the Neo data representation model. Nodes are created by invoking the `EmbeddedNeo.createNode()` method.

Node has three major groups of operations: operations that deal with relationships, operations that deal with properties and operations that create `traversers`.

The relationship operations provide a number of overloaded accessors (such as `getRelationships(...)` with "filters" for type, direction, etc), as well as the factory method `createRelationshipTo(...)` that connects two nodes with a relationship. It also includes the convenience method `getSingleRelationship(...)` for accessing the commonly occuring one-to-zero-or-one association. Of particular interest might be that there are no `hasRelationship(...)` methods. The idiomatic way to check if a node has any relationships matching a "filter" of type and direction is: `if ( node.getRelationships(filter).iterator().hasNext() ) { ... }`

The property operations give access to the key-value property pairs. Property keys are always strings. Valid property value types are all the Java primitives (`int`, `byte`, `float`, etc), `java.lang.String`s and arrays of primitives and Strings. Please note that Neo does NOT accept arbitrary objects as property values. `setProperty()` takes a `java.lang.Object` for design reasons only.

The traversal factory methods instantiate a `traverser` that starts traversing from this node.

Method Summary
`Relationship` | `createRelationshipTo(Node otherNode, RelationshipType type)` Creates a relationship between this node and another node.
`void` | `delete()` Deletes this node.
`long` | `getId()` Returns the unique id of this node.
`java.lang.Object` | `getProperty(java.lang.String key)` Returns the property value associated with the given key.
`java.lang.Object` | `getProperty(java.lang.String key, java.lang.Object defaultValue)` Returns the property value associated with the given key, or a default value.
`java.lang.Iterable<java.lang.String>` | `getPropertyKeys()` Returns all currently valid property keys, or an empty iterable if this node has no properties.
`java.lang.Iterable<java.lang.Object>` | `getPropertyValues()` Returns all currently valid property values, or an empty iterable if this node has no properties.
`java.lang.Iterable<Relationship>` | `getRelationships()` Returns all the relationships attached to this node.
`java.lang.Iterable<Relationship>` | `getRelationships(Direction dir)` Returns all `OUTGOING` or `INCOMING` relationships from this node.
`java.lang.Iterable<Relationship>` | `getRelationships(RelationshipType... type)` Returns all the relationships of type `type` that are attached to this node, regardless of direction.
`java.lang.Iterable<Relationship>` | `getRelationships(RelationshipType type, Direction dir)` Returns all relationships with the given type and direction that are attached to this node.
`Relationship` | `getSingleRelationship(RelationshipType type, Direction dir)` Returns the only relationship of a given type and direction that is attached to this node, or `null`.
`boolean` | `hasProperty(java.lang.String key)` Returns `true` if this node has a property accessible through the given key, `false` otherwise.
`java.lang.Object` | `removeProperty(java.lang.String key)` Removes and returns the property associated with the given key.
`void` | `setProperty(java.lang.String key, java.lang.Object value)` Sets the property value for the given key to `value`.
`Traverser` | `traverse(Traverser.Order traversalOrder, StopEvaluator stopEvaluator, ReturnableEvaluator returnableEvaluator, java.lang.Object... relationshipTypesAndDirections)` Instantiates a traverser that will start at this node and traverse according to the given order and evaluators along the specified relationship type and direction pairs.
`Traverser` | `traverse(Traverser.Order traversalOrder, StopEvaluator stopEvaluator, ReturnableEvaluator returnableEvaluator, RelationshipType relationshipType, Direction direction)` Instantiates a traverser that will start at this node and traverse according to the given order and evaluators along the specified relationship type and direction.
`Traverser` | `traverse(Traverser.Order traversalOrder, StopEvaluator stopEvaluator, ReturnableEvaluator returnableEvaluator, RelationshipType firstRelationshipType, Direction firstDirection, RelationshipType secondRelationshipType, Direction secondDirection)` Instantiates a traverser that will start at this node and traverse according to the given order and evaluators along the two specified relationship type and direction pairs.

Method Detail

### getId

    long getId()

  Returns the unique id of this node. Ids are garbage collected over time so are only guaranteed to be unique at a specific set of time: if the node is deleted, it's likely that a new node at some point will get the old id. This make node ids brittle as public APIs.

  Returns:
    the id of this node

### delete

    void delete()

  Deletes this node. Invoking any methods on this node after `delete()` has returned is invalid and will lead to unspecified behavior.

### getRelationships

    java.lang.Iterable<Relationship> getRelationships()

  Returns all the relationships attached to this node. If no relationships are attached to this node, an empty iterable will be returned.

  Returns:
    all relationships attached to this node

### getRelationships

    java.lang.Iterable<Relationship> getRelationships(RelationshipType... type)

  Returns all the relationships of type `type` that are attached to this node, regardless of direction. If no relationships of the given type are attached to this node, an empty iterable will be returned.

  Parameters:
    `type` - the given relationship type(s)
  Returns:
    all relationships of the given type(s) that are attached to this node

### getRelationships

    java.lang.Iterable<Relationship> getRelationships(Direction dir)

  Returns all `OUTGOING` or `INCOMING` relationships from this node. If there are no relationships with the given direction attached to this node, an empty iterable will be returned. If `BOTH` is passed in as a direction, relationships of both directions are returned (effectively turning this into `getRelationships()`).

  Parameters:
    `dir` - the given direction, where `Direction.OUTGOING` means all relationships that have this node as `start node` and `Direction.INCOMING` means all relationships that have this node as `end node`
  Returns:
    all relationships with the given direction that are attached to this node

### getRelationships

    java.lang.Iterable<Relationship> getRelationships(RelationshipType type,
                                                      Direction dir)

  Returns all relationships with the given type and direction that are attached to this node. If there are no matching relationships, an empty iterable will be returned.

  Parameters:
    `type` - the given type
    `dir` - the given direction, where `Direction.OUTGOING` means all relationships that have this node as `start node` and `Direction.INCOMING` means all relationships that have this node as `end node`
  Returns:
    all relationships attached to this node that match the given type and direction

### getSingleRelationship

    Relationship getSingleRelationship(RelationshipType type,
                                       Direction dir)

  Returns the only relationship of a given type and direction that is attached to this node, or `null`. This is a convenience method that is used in the commonly occuring situation where a node has exactly zero or one relationships of a given type and direction to another node. Typically this invariant is maintained by the rest of the code: if at any time more than one such relationships exist, it is a fatal error that should generate an unchecked exception. This method reflects that semantics and returns either:

  1. `null` if there are zero relationships of the given type and direction,
  2. the relationship if there's exactly one, or
  3. throws an unchecked exception in all other cases.

  This method should be used only in situations with an invariant as described above. In those situations, a "state-checking" method (e.g. `hasSingleRelationship(...)`) is not required, because this method behaves correctly "out of the box."

  Parameters:
    `type` - the type of the wanted relationship
    `dir` - the direction of the wanted relationship (where `Direction.OUTGOING` means a relationship that has this node as `start node` and `Direction.INCOMING` means a relationship that has this node as `end node`) or `Direction.BOTH` if direction is irrelevant
  Returns:
    the single relationship matching the given type and direction if exactly one such relationship exists, or `null` if exactly zero such relationships exists
  Throws:
    `java.lang.RuntimeException` - if more than one relationship matches the given type and direction

### createRelationshipTo

    Relationship createRelationshipTo(Node otherNode,
                                      RelationshipType type)

  Creates a relationship between this node and another node. The relationship is of type `type`. It starts at this node and ends at `otherNode`.

  Parameters:
    `otherNode` - the end node of the new relationship
    `type` - the type of the new relationship
  Returns:
    the newly created relationship

### hasProperty

    boolean hasProperty(java.lang.String key)

  Returns `true` if this node has a property accessible through the given key, `false` otherwise. If key is `null`, this method returns `false`.

  Parameters:
    `key` - the property key
  Returns:
    `true` if this node has a property accessible through the given key, `false` otherwise

### getProperty

    java.lang.Object getProperty(java.lang.String key)

  Returns the property value associated with the given key. The value is of one of the valid property types, i.e. a Java primitive, a `String` or an array of any of the valid types. If there's no property associated with `key` an unchecked exception is raised.

  Parameters:
    `key` - the property key
  Returns:
    the property value associated with the given key
  Throws:
    `java.lang.RuntimeException` - if there's no property associated with `key`

### getProperty

    java.lang.Object getProperty(java.lang.String key,
                                 java.lang.Object defaultValue)

  Returns the property value associated with the given key, or a default value. The value is of one of the valid property types, i.e. a Java primitive, a `String` or an array of any of the valid types. If `defaultValue` is not of a supported type, an unchecked exception is raised.

  Parameters:
    `key` - the property key
  Returns:
    the property value associated with the given key
  Throws:
    `java.lang.IllegalArgumentException` - if `defaultValue` is of an unsupported type

### setProperty

    void setProperty(java.lang.String key,
                     java.lang.Object value)

  Sets the property value for the given key to `value`. The property value must be one of the valid property types, i.e:
  - `boolean` or `boolean[]`
  - `byte` or `byte[]`
  - `short` or `short[]`
  - `int` or `int[]`
  - `long` or `long[]`
  - `float` or `float[]`
  - `double` or `double[]`
  - `char` or `char[]`
  - `java.lang.String` or `String[]`

  Parameters:
    `key` - the key with which the new property value will be associated
    `value` - the new property value, of one of the valid property types
  Throws:
    `java.lang.IllegalArgumentException` - if `value` is of an unsuppoprted type

### removeProperty

    java.lang.Object removeProperty(java.lang.String key)

  Removes and returns the property associated with the given key. If there's no property associated with the key, then `null` is returned.

  Parameters:
    `key` - the property key
  Returns:
    the property value that used to be associated with the given key

### getPropertyKeys

    java.lang.Iterable<java.lang.String> getPropertyKeys()

  Returns all currently valid property keys, or an empty iterable if this node has no properties.

  Returns:
    all property keys

### getPropertyValues

    java.lang.Iterable<java.lang.Object> getPropertyValues()

  Returns all currently valid property values, or an empty iterable if this node has no properties. All values are of a supported property type, i.e. a Java primitive, a `String` or an array of any of the supported types.

  Returns:
    all property values

### traverse

    Traverser traverse(Traverser.Order traversalOrder,
                       StopEvaluator stopEvaluator,
                       ReturnableEvaluator returnableEvaluator,
                       RelationshipType relationshipType,
                       Direction direction)

  Instantiates a traverser that will start at this node and traverse according to the given order and evaluators along the specified relationship type and direction. If the traverser should traverse more than one `RelationshipType`/`Direction` pair, use one of the overloaded variants of this method. For more information about traversal, see the `Traverser` documentation.

  Parameters:
    `traversalOrder` - the traversal order
    `stopEvaluator` - an evaluator instructing the new traverser about when to stop traversing, either a predefined evaluator such as `StopEvaluator.END_OF_NETWORK` or a custom-written evaluator
    `returnableEvaluator` - an evaluator instructing the new traverser about whether a specific node should be returned from the traversal, either a predefined evaluator such as `ReturnableEvaluator.ALL` or a customer-written evaluator
    `relationshipType` - the relationship type that the traverser will traverse along
    `direction` - the direction in which the relationships of type `relationshipType` will be traversed
  Returns:
    a new traverser, configured as above

### traverse

    Traverser traverse(Traverser.Order traversalOrder,
                       StopEvaluator stopEvaluator,
                       ReturnableEvaluator returnableEvaluator,
                       RelationshipType firstRelationshipType,
                       Direction firstDirection,
                       RelationshipType secondRelationshipType,
                       Direction secondDirection)

  Instantiates a traverser that will start at this node and traverse according to the given order and evaluators along the two specified relationship type and direction pairs. If the traverser should traverse more than two `RelationshipType`/`Direction` pairs, use the overloaded `varargs variant` of this method. For more information about traversal, see the `Traverser` documentation.

  Parameters:
    `traversalOrder` - the traversal order
    `stopEvaluator` - an evaluator instructing the new traverser about when to stop traversing, either a predefined evaluator such as `StopEvaluator.END_OF_NETWORK` or a custom-written evaluator
    `returnableEvaluator` - an evaluator instructing the new traverser about whether a specific node should be returned from the traversal, either a predefined evaluator such as `ReturnableEvaluator.ALL` or a customer-written evaluator
    `firstRelationshipType` - the first of the two relationship types that the traverser will traverse along
    `firstDirection` - the direction in which the first relationship type will be traversed
    `secondRelationshipType` - the second of the two relationship types that the traverser will traverse along
    `secondDirection` - the direction that the second relationship type will be traversed
  Returns:
    a new traverser, configured as above

### traverse

    Traverser traverse(Traverser.Order traversalOrder,
                       StopEvaluator stopEvaluator,
                       ReturnableEvaluator returnableEvaluator,
                       java.lang.Object... relationshipTypesAndDirections)

  Instantiates a traverser that will start at this node and traverse according to the given order and evaluators along the specified relationship type and direction pairs. Unlike the overloaded variants of this method, the relationship types and directions are passed in as a "varargs" variable-length argument which means that an arbitrary set of relationship type and direction pairs can be specified. The variable-length argument list should be every other relationship type and direction, starting with relationship type, e.g:

  `node.traverse( BREADTH_FIRST, stopEval, returnableEval, MyRels.REL1, Direction.OUTGOING, MyRels.REL2, Direction.OUTGOING, MyRels.REL3, Direction.BOTH, MyRels.REL4, Direction.INCOMING );`

  Unfortunately, the compiler cannot enforce this so an unchecked exception is raised if the variable-length argument has a different constitution.

  For more information about traversal, see the `Traverser` documentation.

  Parameters:
    `traversalOrder` - the traversal order
    `stopEvaluator` - an evaluator instructing the new traverser about when to stop traversing, either a predefined evaluator such as `StopEvaluator.END_OF_NETWORK` or a custom-written evaluator
    `returnableEvaluator` - an evaluator instructing the new traverser about whether a specific node should be returned from the traversal, either a predefined evaluator such as `ReturnableEvaluator.ALL` or a customer-written evaluator
    `relationshipTypesAndDirections` - a variable-length list of relationship types and their directions, where the first argument is a relationship type, the second argument the first type's direction, the third a relationship type, the fourth its direction, etc
  Returns:
    a new traverser, configured as above
  Throws:
    `java.lang.RuntimeException` - if the variable-length relationship type / direction list is not as described above

Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD