Commit ce2476c1 authored by Antoine Dutot's avatar Antoine Dutot

Merge pull request #126 from trajar/master

updated api to use common collection interfaces, equals/hash
parents 91e7e5df b546ffec
......@@ -256,4 +256,44 @@ public class Point2 implements java.io.Serializable {
return buf.toString();
}
@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
Point2 point2 = (Point2) o;
if (Double.compare(point2.x, x) != 0)
{
return false;
}
if (Double.compare(point2.y, y) != 0)
{
return false;
}
return true;
}
@Override
public int hashCode()
{
int result;
long temp;
temp = Double.doubleToLongBits(x);
result = (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}
\ No newline at end of file
......@@ -328,4 +328,42 @@ public class Point3 extends Point2 implements java.io.Serializable {
return buf.toString();
}
@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
if (!super.equals(o))
{
return false;
}
Point3 point3 = (Point3) o;
if (Double.compare(point3.z, z) != 0)
{
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = super.hashCode();
long temp;
temp = Double.doubleToLongBits(z);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}
\ No newline at end of file
......@@ -31,6 +31,8 @@
*/
package org.graphstream.ui.geom;
import java.util.Arrays;
public class Vector2 implements java.io.Serializable {
// Attributes
......@@ -286,4 +288,11 @@ public class Vector2 implements java.io.Serializable {
return sb.toString();
}
@Override
public int hashCode()
{
return data != null ? Arrays.hashCode(data) : 0;
}
}
\ No newline at end of file
......@@ -31,14 +31,14 @@
*/
package org.graphstream.ui.graphicGraph;
import java.util.ArrayList;
import java.util.HashMap;
import org.graphstream.graph.Edge;
import org.graphstream.graph.Node;
import org.graphstream.stream.SourceBase.ElementType;
import org.graphstream.ui.graphicGraph.stylesheet.Selector;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Graphical edge.
*
......@@ -229,7 +229,7 @@ public class GraphicEdge extends GraphicElement implements Edge {
* The actual set of edges between two nodes (see the
* connectivity in the graphic graph).
*/
protected void countSameEdges(ArrayList<GraphicEdge> edgeList) {
protected void countSameEdges(Iterable<GraphicEdge> edgeList) {
for (GraphicEdge other : edgeList) {
if (other != this) {
if ((other.from == from && other.to == to)
......
......@@ -38,15 +38,16 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.graphstream.graph.Edge;
import org.graphstream.graph.EdgeFactory;
import org.graphstream.graph.Element;
import org.graphstream.graph.ElementNotFoundException;
import org.graphstream.graph.Graph;
import org.graphstream.graph.IdAlreadyInUseException;
import org.graphstream.graph.Node;
import org.graphstream.graph.NodeFactory;
import org.graphstream.graph.ElementNotFoundException;
import org.graphstream.graph.IdAlreadyInUseException;
import org.graphstream.graph.implementations.AbstractElement;
import org.graphstream.stream.AttributeSink;
import org.graphstream.stream.ElementSink;
......@@ -57,10 +58,10 @@ import org.graphstream.stream.file.FileSource;
import org.graphstream.ui.geom.Point3;
import org.graphstream.ui.graphicGraph.stylesheet.Style;
import org.graphstream.ui.graphicGraph.stylesheet.StyleConstants;
import org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.Units;
import org.graphstream.ui.graphicGraph.stylesheet.StyleSheet;
import org.graphstream.ui.graphicGraph.stylesheet.Value;
import org.graphstream.ui.graphicGraph.stylesheet.Values;
import org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.Units;
import org.graphstream.util.GraphListeners;
/**
......@@ -146,7 +147,7 @@ public class GraphicGraph extends AbstractElement implements Graph,
* map is sorted by node. For each node an array of edges lists the
* connectivity.
*/
protected HashMap<GraphicNode, ArrayList<GraphicEdge>> connectivity;
protected final Map<GraphicNode, List<GraphicEdge>> connectivity;
/**
* The style of this graph. This is a shortcut to avoid searching it in the
......@@ -216,7 +217,7 @@ public class GraphicGraph extends AbstractElement implements Graph,
listeners = new GraphListeners(this);
styleSheet = new StyleSheet();
styleGroups = new StyleGroupSet(styleSheet);
connectivity = new HashMap<GraphicNode, ArrayList<GraphicEdge>>();
connectivity = new HashMap<GraphicNode, List<GraphicEdge>>();
styleGroups.addListener(this);
styleGroups.addElement(this); // Add style to this graph.
......@@ -487,7 +488,7 @@ public class GraphicGraph extends AbstractElement implements Graph,
while (keys.hasNext()) {
GraphicNode node = keys.next();
System.err.printf(" [%s] -> ", node.getId());
ArrayList<GraphicEdge> edges = connectivity.get(node);
Iterable<GraphicEdge> edges = connectivity.get(node);
for (GraphicEdge edge : edges)
System.err.printf(" (%s %d)", edge.getId(),
edge.getMultiIndex());
......@@ -675,8 +676,8 @@ public class GraphicGraph extends AbstractElement implements Graph,
styleGroups.addElement(edge);
ArrayList<GraphicEdge> l1 = connectivity.get(n1);
ArrayList<GraphicEdge> l2 = connectivity.get(n2);
List<GraphicEdge> l1 = connectivity.get(n1);
List<GraphicEdge> l2 = connectivity.get(n2);
if (l1 == null) {
l1 = new ArrayList<GraphicEdge>();
......@@ -783,8 +784,8 @@ public class GraphicGraph extends AbstractElement implements Graph,
GraphicNode node1 = (GraphicNode) styleGroups.getNode(to);
if (node0 != null && node1 != null) {
ArrayList<GraphicEdge> edges0 = connectivity.get(node0);
ArrayList<GraphicEdge> edges1 = connectivity.get(node1);
Collection<GraphicEdge> edges0 = connectivity.get(node0);
Collection<GraphicEdge> edges1 = connectivity.get(node1);
for (GraphicEdge edge0 : edges0) {
for (GraphicEdge edge1 : edges1) {
......@@ -816,7 +817,7 @@ public class GraphicGraph extends AbstractElement implements Graph,
// We must do a copy of the connectivity set for the node
// since we will be modifying the connectivity as we process
// edges.
ArrayList<GraphicEdge> l = new ArrayList<GraphicEdge>(
List<GraphicEdge> l = new ArrayList<GraphicEdge>(
connectivity.get(node));
for (GraphicEdge edge : l)
......
......@@ -31,14 +31,6 @@
*/
package org.graphstream.ui.graphicGraph;
import static org.graphstream.ui.graphicGraph.GraphPosLengthUtils.nodePosition;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import org.graphstream.graph.Edge;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
......@@ -46,6 +38,14 @@ import org.graphstream.stream.SourceBase.ElementType;
import org.graphstream.ui.geom.Point3;
import org.graphstream.ui.graphicGraph.stylesheet.Selector;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import static org.graphstream.ui.graphicGraph.GraphPosLengthUtils.nodePosition;
/**
* Graphical node.
*
......@@ -225,7 +225,7 @@ public class GraphicNode extends GraphicElement implements Node {
}
public int getDegree() {
ArrayList<GraphicEdge> edges = mygraph.connectivity.get(this);
List<GraphicEdge> edges = mygraph.connectivity.get(this);
if (edges != null)
return edges.size();
......@@ -235,7 +235,7 @@ public class GraphicNode extends GraphicElement implements Node {
@SuppressWarnings("unchecked")
public <T extends Edge> T getEdge(int i) {
ArrayList<GraphicEdge> edges = mygraph.connectivity.get(this);
List<GraphicEdge> edges = mygraph.connectivity.get(this);
if (edges != null && i >= 0 && i < edges.size())
return (T) edges.get(i);
......@@ -258,7 +258,7 @@ public class GraphicNode extends GraphicElement implements Node {
@SuppressWarnings("unchecked")
public <T extends Edge> Iterator<T> getEdgeIterator() {
ArrayList<GraphicEdge> edges = mygraph.connectivity.get(this);
List<GraphicEdge> edges = mygraph.connectivity.get(this);
if (edges != null)
return (Iterator<T>) edges.iterator();
......@@ -284,7 +284,7 @@ public class GraphicNode extends GraphicElement implements Node {
@SuppressWarnings("all")
public <T extends Edge> T getEdgeToward(String id) {
ArrayList<? extends Edge> edges = mygraph.connectivity.get(this);
List<? extends Edge> edges = mygraph.connectivity.get(this);
for (Edge edge : edges) {
if (edge.getOpposite(this).getId().equals(id))
......
......@@ -32,19 +32,21 @@
package org.graphstream.ui.graphicGraph;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import org.graphstream.graph.Edge;
import org.graphstream.graph.Element;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.ui.graphicGraph.stylesheet.Rule;
import org.graphstream.ui.graphicGraph.stylesheet.Selector;
import org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.ShadowMode;
import org.graphstream.ui.graphicGraph.stylesheet.StyleSheet;
import org.graphstream.ui.graphicGraph.stylesheet.StyleSheetListener;
import org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.ShadowMode;
/**
* A set of style groups.
......@@ -68,27 +70,27 @@ public class StyleGroupSet implements StyleSheetListener {
/**
* All the groups indexed by their unique identifier.
*/
protected HashMap<String, StyleGroup> groups = new HashMap<String, StyleGroup>();
protected final Map<String, StyleGroup> groups = new TreeMap<String, StyleGroup>();
/**
* Allows to retrieve the group containing a node knowing the node id.
*/
protected HashMap<String, String> byNodeIdGroups = new HashMap<String, String>();
protected final Map<String, String> byNodeIdGroups = new TreeMap<String, String>();
/**
* Allows to retrieve the group containing an edge knowing the node id.
*/
protected HashMap<String, String> byEdgeIdGroups = new HashMap<String, String>();
protected final Map<String, String> byEdgeIdGroups = new TreeMap<String, String>();
/**
* Allows to retrieve the group containing a sprite knowing the node id.
*/
protected HashMap<String, String> bySpriteIdGroups = new HashMap<String, String>();
protected final Map<String, String> bySpriteIdGroups = new TreeMap<String, String>();
/**
* Allows to retrieve the group containing a graph knowing the node id.
*/
protected HashMap<String, String> byGraphIdGroups = new HashMap<String, String>();
protected final Map<String, String> byGraphIdGroups = new TreeMap<String, String>();
/**
* Virtual set of nodes. This set provides fake methods to make it appear as
......@@ -287,7 +289,7 @@ public class StyleGroupSet implements StyleSheetListener {
* The kind of element.
* @return The element or null if not found.
*/
protected Element getElement(String id, HashMap<String, String> elt2grp) {
protected Element getElement(String id, Map<String, String> elt2grp) {
String gid = elt2grp.get(id);
if (gid != null) {
......@@ -662,6 +664,10 @@ public class StyleGroupSet implements StyleSheetListener {
*/
public void removeElement(Element element) {
String gid = getElementGroup(element);
if (null == gid)
{
return;
}
StyleGroup group = groups.get(gid);
if (group != null) {
......@@ -1029,7 +1035,7 @@ public class StyleGroupSet implements StyleSheetListener {
* The name space.
*/
protected void checkForNewIdStyle(Rule newRule,
HashMap<String, String> elt2grp) {
Map<String, String> elt2grp) {
// There is only one element that matches the identifier.
Element element = getElement(newRule.selector.getId(), elt2grp);
......@@ -1053,8 +1059,8 @@ public class StyleGroupSet implements StyleSheetListener {
* The name space.
*/
protected void checkForNewStyle(Rule newRule,
HashMap<String, String> elt2grp) {
ArrayList<Element> elementsToCheck = new ArrayList<Element>();
Map<String, String> elt2grp) {
Collection<Element> elementsToCheck = new ArrayList<Element>();
for (String eltId : elt2grp.keySet())
elementsToCheck.add(getElement(eltId, elt2grp));
......@@ -1403,11 +1409,11 @@ public class StyleGroupSet implements StyleSheetListener {
* The kind of graph element.
*/
protected class ElementIterator<E extends Element> implements Iterator<E> {
protected HashMap<String, String> elt2grp;
protected Map<String, String> elt2grp;
protected Iterator<String> elts;
public ElementIterator(HashMap<String, String> elements2groups) {
public ElementIterator(final Map<String, String> elements2groups) {
elt2grp = elements2groups;
elts = elements2groups.keySet().iterator();
}
......
......@@ -31,17 +31,6 @@
*/
package org.graphstream.ui.swingViewer;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import org.graphstream.ui.graphicGraph.GraphicElement;
import org.graphstream.ui.graphicGraph.GraphicGraph;
import org.graphstream.ui.swingViewer.util.Camera;
......@@ -50,6 +39,16 @@ import org.graphstream.ui.swingViewer.util.DefaultShortcutManager;
import org.graphstream.ui.swingViewer.util.MouseManager;
import org.graphstream.ui.swingViewer.util.ShortcutManager;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Collection;
/**
* Base for constructing views.
*
......@@ -333,7 +332,7 @@ public class DefaultView extends View implements WindowListener, ComponentListen
// Methods deferred to the renderer
@Override
public ArrayList<GraphicElement> allNodesOrSpritesIn(double x1, double y1,
public Collection<GraphicElement> allNodesOrSpritesIn(double x1, double y1,
double x2, double y2) {
return renderer.allNodesOrSpritesIn(x1, y1, x2, y2);
}
......
......@@ -31,14 +31,14 @@
*/
package org.graphstream.ui.swingViewer;
import java.awt.Container;
import java.awt.Graphics2D;
import java.util.ArrayList;
import org.graphstream.ui.graphicGraph.GraphicElement;
import org.graphstream.ui.graphicGraph.GraphicGraph;
import org.graphstream.ui.swingViewer.util.Camera;
import java.awt.Container;
import java.awt.Graphics2D;
import java.util.Collection;
/**
* Interface for classes that draw a GraphicGraph in a swing component.
*
......@@ -98,7 +98,7 @@ public interface GraphRenderer {
* The rectangle highest point ordinate.
* @return The set of sprites and nodes in the given rectangle.
*/
public abstract ArrayList<GraphicElement> allNodesOrSpritesIn(double x1,
public abstract Collection<GraphicElement> allNodesOrSpritesIn(double x1,
double y1, double x2, double y2);
// Command
......
......@@ -31,16 +31,15 @@
*/
package org.graphstream.ui.swingViewer;
import java.util.ArrayList;
import javax.swing.JPanel;
import org.graphstream.ui.graphicGraph.GraphicElement;
import org.graphstream.ui.graphicGraph.GraphicGraph;
import org.graphstream.ui.swingViewer.util.Camera;
import org.graphstream.ui.swingViewer.util.MouseManager;
import org.graphstream.ui.swingViewer.util.ShortcutManager;
import javax.swing.JPanel;
import java.util.Collection;
/**
* A view on a graphic graph.
*
......@@ -112,7 +111,7 @@ public abstract class View extends JPanel {
* The rectangle highest point ordinate.
* @return The set of sprites and nodes in the given rectangle.
*/
public abstract ArrayList<GraphicElement> allNodesOrSpritesIn(double x1,
public abstract Collection<GraphicElement> allNodesOrSpritesIn(double x1,
double y1, double x2, double y2);
// Command
......
......@@ -44,6 +44,7 @@ import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import javax.imageio.ImageIO;
......@@ -134,7 +135,7 @@ public class SwingBasicGraphRenderer extends GraphRendererBase {
return camera;
}
public ArrayList<GraphicElement> allNodesOrSpritesIn(double x1, double y1,
public Collection<GraphicElement> allNodesOrSpritesIn(double x1, double y1,
double x2, double y2) {
return camera.allNodesOrSpritesIn(graph, x1, y1, x2, y2);
}
......
......@@ -36,7 +36,10 @@ import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import org.graphstream.graph.Node;
import org.graphstream.ui.geom.Point2;
......@@ -387,9 +390,9 @@ public class DefaultCamera implements Camera {
* The rectangle highest point ordinate.
* @return The set of sprites and nodes in the given rectangle.
*/
public ArrayList<GraphicElement> allNodesOrSpritesIn(GraphicGraph graph,
public Collection<GraphicElement> allNodesOrSpritesIn(GraphicGraph graph,
double x1, double y1, double x2, double y2) {
ArrayList<GraphicElement> elts = new ArrayList<GraphicElement>();
List<GraphicElement> elts = new ArrayList<GraphicElement>();
for (Node node : graph) {
if (isNodeIn((GraphicNode) node, x1, y1, x2, y2))
......@@ -401,7 +404,7 @@ public class DefaultCamera implements Camera {
elts.add(sprite);
}
return elts;
return Collections.unmodifiableList(elts);
}
/**
......
......@@ -31,15 +31,14 @@
*/
package org.graphstream.ui.swingViewer.util;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import org.graphstream.graph.Node;
import org.graphstream.ui.graphicGraph.GraphicElement;
import org.graphstream.ui.graphicGraph.GraphicGraph;
import org.graphstream.ui.graphicGraph.GraphicSprite;
import org.graphstream.ui.swingViewer.View;
import java.awt.event.MouseEvent;
public class DefaultMouseManager implements MouseManager {
// Attribute
......@@ -88,7 +87,7 @@ public class DefaultMouseManager implements MouseManager {
}
protected void mouseButtonRelease(MouseEvent event,
ArrayList<GraphicElement> elementsInArea) {
Iterable<GraphicElement> elementsInArea) {
for (GraphicElement element : elementsInArea) {
if (!element.hasAttribute("ui.selected"))
element.addAttribute("ui.selected");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment