E
- Kind of elements used as this graph's nodes.public class Graph<E> extends Object implements IGraph<E>
This boils down to maintaining a list of children and parents of each node, updating them in sync with each other.
Take note that the elements of this graph are not necessarily all connected together. This can be used to represent a set of trees, a set of undirected graphs, a set of roots with no children...
This class is not intended to be sub-classed.
Constructor and Description |
---|
Graph()
Constructs an empty graph.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E element)
Adds a new element to this graph, if it does not exists yet.
|
void |
addChildren(E element,
Set<E> newChildren)
Connects the given set of elements to a given parent.
|
void |
addParentData(E element,
E parentData)
Set the parent data for the given element.
|
PruningIterator<E> |
breadthFirstIterator()
Returns a breadth-first iterator over this whole graph.
|
void |
clear()
Clears this graph and goes back to a pristine state.
|
boolean |
contains(E element)
Checks whether this graph already contains the given element.
|
Iterator<E> |
depthFirstIterator(E root)
Returns a depth first iterator created with the given element as root.
|
Set<E> |
getDirectParents(E element)
Returns the direct parents of the given
element . |
E |
getParentData(E element)
Get the parent data of the given element.
|
Set<E> |
getSubgraphContaining(E element)
Returns the set of all elements of the subgraph containing the given element.
|
Set<E> |
getSubgraphContaining(E element,
Set<E> endPoints)
Returns the set of all elements of the subgraph containing the given element and ending at the given
boundaries.
|
Set<E> |
getTreeFrom(E root)
Returns the tree starting from the given root element if it is contained in the graph.
|
Set<E> |
getTreeFrom(E root,
Set<E> endPoints)
Returns the tree starting from the given root element and ending at the given boundaries..
|
boolean |
hasChild(E parent,
E potentialChild)
Checks if the given element is a parent of the given potential child, directly or not.
|
void |
remove(E element)
Removes the given element's node from this graph.
|
void |
removeAll(Collection<E> elements)
Removes the given elements' nodes from this graph.
|
public boolean contains(E element)
public void clear()
public boolean add(E element)
public void remove(E element)
public void removeAll(Collection<E> elements)
public void addChildren(E element, Set<E> newChildren)
addChildren
in interface IGraph<E>
element
- The element that is to be connected with new children.newChildren
- The set of elements to connect to the given parent.public boolean hasChild(E parent, E potentialChild)
public Set<E> getDirectParents(E element)
element
.
Note that the returned set is a view over a sub-graph of this graph, and that changes to it will not be reflected within the graph itself.
getDirectParents
in interface IGraph<E>
element
- The element which parents we seek.element
.public Set<E> getTreeFrom(E root)
Contrarily to getSubgraphContaining(Object)
, this will only iterate over the children (and
recursively) of the given node, without ever "going up" to parents of these children.
Note that the returned set is a view over a sub-graph of this graph, and that changes to it will not be reflected within the graph itself.
getTreeFrom
in interface IGraph<E>
root
- The element we are to consider as the root of a tree.public Set<E> getTreeFrom(E root, Set<E> endPoints)
Contrarily to getSubgraphContaining(Object, Set)
, this will only iterate over the children
(and recursively) of the given node, without ever "going up" to parents of these children.
Note that the returned set is a view over a sub-graph of this graph, and that changes to it will not be reflected within the graph itself.
getTreeFrom
in interface IGraph<E>
root
- The element we are to consider as the root of a tree.endPoints
- Boundaries of the tree.public Set<E> getSubgraphContaining(E element)
Note that the returned set is a view over a sub-graph of this graph, and that changes to it will not be reflected within the graph itself.
getSubgraphContaining
in interface IGraph<E>
element
- Element we need the subgraph of.public Set<E> getSubgraphContaining(E element, Set<E> endPoints)
Note that the returned set is a view over a sub-graph of this graph, and that changes to it will not be reflected within the graph itself.
getSubgraphContaining
in interface IGraph<E>
element
- Element we need the subgraph of.endPoints
- Boundaries of the needed subgraph.public PruningIterator<E> breadthFirstIterator()
The returned iterator does not support removal, and will fail or return undefined results if this graph is modified after the iterator's creation.
breadthFirstIterator
in interface IGraph<E>
public Iterator<E> depthFirstIterator(E root)
The root will be returned first, then the left-most child of that root, then the left-most child of that child if any, or the closest sibling to the right of the current element. For example, with the following tree:
A / \ B C / / \ D E FThe iteration order will be : A, B, D, C, E, F.
The returned iterator does not support removal, and will fail or return undefined results if this graph is modified after the iterator's creation.
depthFirstIterator
in interface IGraph<E>
root
- The root of the tree over which we need to iterate.public E getParentData(E element)
The parent data, is the URI of the parent resource's object in case of a containment relationship between the given element and its parent.
getParentData
in interface IGraph<E>
element
- Element we need the parent data of.E
if this element has a parent data, null
otherwise.public void addParentData(E element, E parentData)
The parent data, is the URI of the parent resource's object in case of a containment relationship between the given element and its parent.
If the given element has several parents, then the addition of a new parent data results in delete the parent data. If the given element has no parents, then the addition of a new parent data results in delete the parent data.addParentData
in interface IGraph<E>
element
- Element for which we need to set the parent data.parentData
- The parent data to set.
Copyright (c) 2006, 2015 Obeo and others. All rights reserved.