Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisBlock committed Oct 30, 2018
1 parent f993a74 commit 090b256
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 29 deletions.
4 changes: 3 additions & 1 deletion README.md
@@ -1,3 +1,5 @@
**Note: This library is in an early stage of development. There will be breaking changes to the public API until the first stable version is released.**

GraphView
===========

Expand All @@ -14,7 +16,7 @@ Download

```groovy
dependencies {
implementation 'de.blox:graphview:0.4.0'
implementation 'de.blox:graphview:0.4.1'
}
```
Usage
Expand Down
2 changes: 1 addition & 1 deletion graphview/build.gradle
Expand Up @@ -40,7 +40,7 @@ ext {
siteUrl = "https://github.com/Team-Blox/GraphView"
gitUrl = "https://github.com/Team-Blox/GraphView.git"

libraryVersion = '0.4.0'
libraryVersion = '0.4.1'

developerId = 'Team-Blox'
developerName = 'Blox'
Expand Down
1 change: 1 addition & 0 deletions graphview/src/main/java/de/blox/graphview/Algorithm.java
Expand Up @@ -9,4 +9,5 @@
public interface Algorithm {
void run(Graph graph);
void drawEdges(Canvas canvas, Graph graph, Paint mLinePaint);
void setEdgeRenderer(EdgeRenderer renderer);
}
Expand Up @@ -37,7 +37,7 @@ public BaseGraphAdapter(@NonNull Context context, @LayoutRes int layoutRes, @Non

@Override
public void notifySizeChanged() {
if (graph != null) {
if (graph != null && graph.getNodeCount() > 0) {
getAlgorithm().run(graph);
}
}
Expand All @@ -56,6 +56,7 @@ public void setAlgorithm(@NonNull Algorithm algorithm) {
Conditions.isNonNull(algorithm, "algorithm can't be null");

mAlgorithm = algorithm;
graph.setAsTree(getAlgorithm() instanceof BuchheimWalkerAlgorithm);
}

@Override
Expand All @@ -75,6 +76,8 @@ public void setGraph(@NonNull Graph graph) {
this.graph.addNodeObserver(this);

mDataSetObservable.notifyChanged();

graph.setAsTree(getAlgorithm() instanceof BuchheimWalkerAlgorithm);
}

@Override
Expand Down
101 changes: 85 additions & 16 deletions graphview/src/main/java/de/blox/graphview/Graph.java
Expand Up @@ -15,9 +15,11 @@ public class Graph {
private List<Edge> edges = new ArrayList<>();

private List<NodeObserver> observers = new ArrayList<>();
private boolean isTree = true;

/**
* Add one {@link Node} to the graph without a connection the other nodes.
*
* @param node
*/
public void addNode(@NonNull Node node) {
Expand All @@ -28,6 +30,7 @@ public void addNode(@NonNull Node node) {

/**
* Add one or more {@link Node Nodes} to the graph without a connection the other nodes.
*
* @param nodes
*/
public void addNodes(@NonNull Node... nodes) {
Expand All @@ -39,31 +42,44 @@ public void addNodes(@NonNull Node... nodes) {
/**
* Remove a {@link Node} from the graph. If the node has a connection with an other node, the connection
* will be removed too.
*
* @param node
*/
public void removeNode(@NonNull Node node) {
if(!nodes.contains(node)) {
removeNodeInternal(node);

for (NodeObserver observer : observers) {
observer.notifyNodeRemoved(node);
}
}

private void removeNodeInternal(Node node) {
if (!nodes.contains(node)) {
throw new IllegalArgumentException("Unable to find node in graph.");
}

if (isTree) {
for (Node n : successorsOf(node)) {
removeNodeInternal(n);
}
}

nodes.remove(node);

final Iterator<Edge> iterator = edges.iterator();
while (iterator.hasNext()) {
Edge edge = iterator.next();
if(edge.getSource().equals(node) || edge.getDestination().equals(node)) {
if (edge.getSource().equals(node) || edge.getDestination().equals(node)) {
iterator.remove();
}
}

for(NodeObserver observer : observers) {
observer.notifyNodeRemoved(node);
}
}

/**
* Remove {@link Node Nodes} from the graph. If the node has a connection with an other node, the connection
* will be removed too.
*
* @param nodes
*/
public void removeNodes(@NonNull Node... nodes) {
Expand All @@ -75,6 +91,7 @@ public void removeNodes(@NonNull Node... nodes) {
/**
* Add an edge between two {@link Node Nodes}. Both nodes will be added to the graph, if the graph
* doesn't contain these nodes already (you don't have to use {@link #addNode(Node)} anymore).
*
* @param source
* @param destination
*/
Expand All @@ -84,7 +101,7 @@ public void addEdge(@NonNull Node source, @NonNull Node destination) {

edges.add(new Edge(source, destination));

for(NodeObserver observer : observers) {
for (NodeObserver observer : observers) {
observer.notifyInvalidated();
}
}
Expand All @@ -97,26 +114,32 @@ public void removeNodeObserver(NodeObserver nodeObserver) {
observers.remove(nodeObserver);
}

public boolean hasNodes() {
return !nodes.isEmpty();
}

/**
* Returns the node at {@code position}.
*
* @param position
* @return
*/
public Node getNode(int position) {
if(position < 0) {
if (position < 0) {
throw new IllegalArgumentException("position can't be negative");
}

final int size = nodes.size();
if(position >= size) {
if (position >= size) {
throw new IndexOutOfBoundsException("Position: " + position + ", Size: " + size);
}

return nodes.get(position);
}

/**
* Returns the number of nodes in this graph.
*
* @return
*/
public int getNodeCount() {
Expand All @@ -125,6 +148,7 @@ public int getNodeCount() {

/**
* Returns all nodes in this graph.
*
* @return
*/
public List<Node> getNodes() {
Expand All @@ -133,20 +157,20 @@ public List<Node> getNodes() {

/**
* Returns all edges in this graph.
*
* @return
*/
public List<Edge> getEdges() {
return edges;
}

/**
*
* @param node
* @return
*/
public boolean hasSuccessor(Node node) {
for(Edge edge : edges) {
if(edge.getSource().equals(node)) {
for (Edge edge : edges) {
if (edge.getSource().equals(node)) {
return true;
}
}
Expand All @@ -156,33 +180,78 @@ public boolean hasSuccessor(Node node) {

/**
* Finds all successors of {@code node}.
*
* @param node
* @return
*/
public List<Node> successorsOf(Node node) {
List<Node> successors = new ArrayList<>();
for(Edge edge : edges) {
if(edge.getSource().equals(node)) {
for (Edge edge : edges) {
if (edge.getSource().equals(node)) {
successors.add(edge.getDestination());
}
}

return successors;
}

/**
* @param node
* @return
*/
public boolean hasPredecessor(Node node) {
for (Edge edge : edges) {
if (edge.getDestination().equals(node)) {
return true;
}
}

return false;
}

/**
* Finds all predecessors of {@code node}.
*
* @param node
* @return
*/
public List<Node> predecessorsOf(Node node) {
List<Node> predecessors = new ArrayList<>();
for(Edge edge : edges) {
if(edge.getDestination().equals(node)) {
for (Edge edge : edges) {
if (edge.getDestination().equals(node)) {
predecessors.add(edge.getSource());
}
}

return predecessors;
}

public boolean contains(Node node) {
return nodes.contains(node);
}

public boolean containsData(Object data) {
for (Node node : nodes) {
if (node.getData().equals(data)) {
return true;
}
}

return false;
}

public Node getNode(Object data) {
for (Node node : nodes) {
if (node.getData().equals(data)) {
return node;
}
}

return null;
}

// Todo this is a quick fix and should be removed later
public void setAsTree(boolean isTree) {
this.isTree = isTree;
}
}
Expand Up @@ -300,7 +300,7 @@ public void setSelection(int position) {
@Override
protected void dispatchDraw(Canvas canvas) {
GraphAdapter adapter = getAdapter();
if (adapter != null) {
if (adapter != null && adapter.getGraph() != null) {
adapter.getAlgorithm().drawEdges(canvas, adapter.getGraph(), linePaint);
}

Expand Down
4 changes: 4 additions & 0 deletions graphview/src/main/java/de/blox/graphview/GraphView.java
Expand Up @@ -93,6 +93,10 @@ public void setAdapter(GraphAdapter adapter) {
graphNodeContainerView.setAdapter(adapter);
}

public GraphAdapter getAdapter() {
return graphNodeContainerView.getAdapter();
}

public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
graphNodeContainerView.setOnItemClickListener(onItemClickListener);
}
Expand Down
15 changes: 15 additions & 0 deletions graphview/src/main/java/de/blox/graphview/Node.java
Expand Up @@ -53,4 +53,19 @@ public String toString() {
", size=" + size +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Node node = (Node) o;

return data.equals(node.data);
}

@Override
public int hashCode() {
return data.hashCode();
}
}
8 changes: 8 additions & 0 deletions graphview/src/main/java/de/blox/graphview/Vector.java
Expand Up @@ -17,10 +17,18 @@ public Vector add(Vector operand) {
return new Vector(operand.x + x, operand.y + y);
}

public Vector add(float x, float y) {
return new Vector(this.x + x, this.y + y);
}

public Vector subtract(Vector operand) {
return new Vector(x - operand.x, y - operand.y);
}

public Vector subtract(float x, float y) {
return new Vector(this.x - x, this.y - y);
}

public Vector multiply(Vector operand) {
return new Vector(x * operand.x, y * operand.y);
}
Expand Down

0 comments on commit 090b256

Please sign in to comment.