📄 defaultgraph.java
字号:
package net.sourceforge.jpowergraph;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* A default implementation of a mutable graph.
*/
public class DefaultGraph extends AbstractGraph {
/** Nodes of the graph. */
protected List m_nodes;
/** Edges of the graph. */
protected List m_edges;
/**
* Creates a new graph.
*/
public DefaultGraph() {
m_nodes=new LinkedList();
m_edges=new LinkedList();
}
/**
* Returns a collection of the nodes in the graph.
*
* @return a collection of the nodes in the graph
*/
public List getNodes() {
return m_nodes;
}
/**
* Returns a collection of the edges in the graph.
*
* @return the collection of the edges in the graph
*/
public List getEdges() {
return m_edges;
}
/**
* Adds elements to the graph.
*
* @param nodes the nodes added to the graph (may be <code>null</code>)
* @param edges the edges added to the graph (may be <code>null</code>)
*/
public synchronized void addElements(Collection nodes,Collection edges) {
if (nodes!=null) {
Iterator iterator=nodes.iterator();
while (iterator.hasNext()) {
DefaultNode node=(DefaultNode)iterator.next();
if (!m_nodes.contains(node))
m_nodes.add(node);
}
}
if (edges!=null) {
Iterator iterator=edges.iterator();
while (iterator.hasNext()) {
DefaultEdge edge=(DefaultEdge)iterator.next();
if (!m_edges.contains(edge)) {
m_edges.add(edge);
((DefaultNode)edge.getFrom()).notifyEdgeAdded(edge);
((DefaultNode)edge.getTo()).notifyEdgeAdded(edge);
}
}
}
fireElementsAdded(nodes,edges);
}
/**
* Deletes elements from the graph.
*
* @param nodes the nodes to delete (may be <code>null</code>)
* @param edges the edges to delete (may be <code>null</code>)
*/
public synchronized void deleteElements(Collection nodes,Collection edges) {
if (nodes!=null) {
Iterator iterator=nodes.iterator();
while (iterator.hasNext()) {
DefaultNode node=(DefaultNode)iterator.next();
m_nodes.remove(node);
}
}
if (edges!=null) {
Iterator iterator=edges.iterator();
while (iterator.hasNext()) {
DefaultEdge edge=(DefaultEdge)iterator.next();
if (m_edges.remove(edge)) {
((DefaultNode)edge.getFrom()).notifyEdgeRemoved(edge);
((DefaultNode)edge.getTo()).notifyEdgeRemoved(edge);
}
}
}
fireElementsRemoved(nodes,edges);
}
/**
* Merges the elements in the specified Collections with those already in the graph.
*
* If a node/edge in the specified Collection is not in the graph its is added, if
* it is then it is ignored, if a node/edge in the graph is not in the specified list then
* it is removedfrom the graph. This is done to allow a complete state change of the graph
* without deleting the nodeMovement values, this means that only changes between two
* graphs are made.
*
* @param nodes the nodes added to the graph (may be <code>null</code>)
* @param edges the edges added to the graph (may be <code>null</code>)
*/
public void mergeElements(Collection nodes, Collection edges) {
if (nodes != null){
for (Iterator i = m_nodes.iterator(); i.hasNext();) {
Node cnode = (Node) i.next();
if (nodes.contains(cnode)) {
nodes.remove(cnode);
}
else {
i.remove();
}
}
}
if (edges != null){
for (Iterator i = m_edges.iterator(); i.hasNext();) {
Edge cedge = (Edge) i.next();
if (edges.contains(cedge)) {
edges.remove(cedge);
}
else {
i.remove();
}
}
}
if ((nodes != null && nodes.size() > 0) || (edges != null && edges.size() > 0)) {
addElements(nodes, edges);
}
}
/**
* Clears the graph.
*/
public synchronized void clear() {
m_nodes.clear();
m_edges.clear();
fireGraphContentsChanged();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -