Refactoring 2

- added a forgotten method in Vertex & test
- fixed Mark, now is Obj
This commit is contained in:
2018-10-16 21:55:27 +02:00
parent 25fb76afcf
commit b441871de0
4 changed files with 37 additions and 17 deletions

View File

@@ -116,7 +116,7 @@ public interface Graph<V, W extends Number> extends Iterable<V> {
* @throws NullPointerException if one of the param is null * @throws NullPointerException if one of the param is null
* @throws IllegalArgumentException if the vertex is not contained in the graph * @throws IllegalArgumentException if the vertex is not contained in the graph
*/ */
void mark(V vertex, String mark) throws NullPointerException, IllegalArgumentException; void mark(V vertex, Object mark) throws NullPointerException, IllegalArgumentException;
/** /**
* Remove the selected mark from the vertex.<br> * Remove the selected mark from the vertex.<br>
@@ -126,7 +126,7 @@ public interface Graph<V, W extends Number> extends Iterable<V> {
* @throws NullPointerException if a param is null * @throws NullPointerException if a param is null
* @throws IllegalArgumentException if the vertex is not contained in the graph * @throws IllegalArgumentException if the vertex is not contained in the graph
*/ */
void unMark(V vertex, String mark) throws NullPointerException, IllegalArgumentException; void unMark(V vertex, Object mark) throws NullPointerException, IllegalArgumentException;
/** /**
* Unmark the vertex selected.<br> * Unmark the vertex selected.<br>
@@ -148,7 +148,7 @@ public interface Graph<V, W extends Number> extends Iterable<V> {
* @throws NullPointerException if the vertex is null * @throws NullPointerException if the vertex is null
* @throws IllegalArgumentException if the vertex is not contained in the graph * @throws IllegalArgumentException if the vertex is not contained in the graph
*/ */
Collection<String> getMarks(V vertex) throws NullPointerException, IllegalArgumentException; Collection<Object> getMarks(V vertex) throws NullPointerException, IllegalArgumentException;
/** /**
* Remove the selected mark from all the vertices * Remove the selected mark from all the vertices
@@ -156,7 +156,7 @@ public interface Graph<V, W extends Number> extends Iterable<V> {
* @param mark the mark to remove * @param mark the mark to remove
* @throws NullPointerException if the mark is null * @throws NullPointerException if the mark is null
*/ */
void unMarkAll(String mark) throws NullPointerException; void unMarkAll(Object mark) throws NullPointerException;
/** /**
* Remove all the marker to all the vertex.<br> * Remove all the marker to all the vertex.<br>
@@ -488,7 +488,7 @@ public interface Graph<V, W extends Number> extends Iterable<V> {
* @param marker the marker * @param marker the marker
* @return a sub-graph of the current graph * @return a sub-graph of the current graph
*/ */
Graph<V, W> subGraph(String marker); Graph<V, W> subGraph(Object marker);
/** /**
* Get the minimum path from the source vertex to the destination vertex.<br> * Get the minimum path from the source vertex to the destination vertex.<br>

View File

@@ -32,7 +32,7 @@ public class MapGraph<V, W extends Number> implements Graph<V, W> {
/** /**
* Map that contains the vertex as key and all the marker as the value associated with that vertex. * Map that contains the vertex as key and all the marker as the value associated with that vertex.
*/ */
private final Map<V, Set<String>> marked = new HashMap<>(); private final Map<V, Set<Object>> marked = new HashMap<>();
/** /**
* Need this variable for not calculating each time the SCC or the cyclic part if the graph doesn't change * Need this variable for not calculating each time the SCC or the cyclic part if the graph doesn't change
@@ -103,16 +103,16 @@ public class MapGraph<V, W extends Number> implements Graph<V, W> {
} }
@Override @Override
public void mark(V vertex, String mark) throws NullPointerException, IllegalArgumentException { public void mark(V vertex, Object mark) throws NullPointerException, IllegalArgumentException {
checkNullAndExist(vertex); checkNullAndExist(vertex);
checkNull(mark); checkNull(mark);
Set<String> set = marked.computeIfAbsent(vertex, (m) -> new HashSet<>()); Set<Object> set = marked.computeIfAbsent(vertex, (m) -> new HashSet<>());
set.add(mark); set.add(mark);
} }
@Override @Override
public void unMark(V vertex, String mark) throws NullPointerException, IllegalArgumentException { public void unMark(V vertex, Object mark) throws NullPointerException, IllegalArgumentException {
checkNullAndExist(vertex); checkNullAndExist(vertex);
checkNull(mark); checkNull(mark);
marked.get(vertex).remove(mark); marked.get(vertex).remove(mark);
@@ -125,13 +125,13 @@ public class MapGraph<V, W extends Number> implements Graph<V, W> {
} }
@Override @Override
public Set<String> getMarks(V vertex) throws NullPointerException, IllegalArgumentException { public Set<Object> getMarks(V vertex) throws NullPointerException, IllegalArgumentException {
checkNullAndExist(vertex); checkNullAndExist(vertex);
return marked.computeIfAbsent(vertex, (m) -> new HashSet<>()); return marked.computeIfAbsent(vertex, (m) -> new HashSet<>());
} }
@Override @Override
public void unMarkAll(String mark) { public void unMarkAll(Object mark) {
checkNull(mark); checkNull(mark);
marked.forEach((v, m) -> m.remove(mark)); marked.forEach((v, m) -> m.remove(mark));
} }
@@ -394,7 +394,7 @@ public class MapGraph<V, W extends Number> implements Graph<V, W> {
} }
@Override @Override
public Graph<V, W> subGraph(final String marker) { public Graph<V, W> subGraph(final Object marker) {
final Graph<V, W> graph = new MapGraph<>(); final Graph<V, W> graph = new MapGraph<>();
final Set<V> allVertices = new HashSet<>(); final Set<V> allVertices = new HashSet<>();

View File

@@ -57,17 +57,29 @@ public class Vertex<V> {
* *
* @param mark the marker * @param mark the marker
* @throws NullPointerException if the marker is null * @throws NullPointerException if the marker is null
* @throws UnsupportedOperationException if the vertes is not in the graph anymore * @throws UnsupportedOperationException if the vertex is not in the graph anymore
*/ */
public void mark(String mark) throws NullPointerException, UnsupportedOperationException { public void mark(Object mark) throws NullPointerException, UnsupportedOperationException {
throwIfNotContained(); throwIfNotContained();
graph.mark(vertex, mark); graph.mark(vertex, mark);
} }
/**
* Remove the specified mark from this vertex
*
* @param mark the marker
* @throws NullPointerException if the mark is null
* @throws UnsupportedOperationException if the vertex is not in the graph anymore
*/
public void unMark(Object mark) throws UnsupportedOperationException {
throwIfNotContained();
graph.unMark(vertex, mark);
}
/** /**
* Remove all the marker from the vertex * Remove all the marker from the vertex
* *
* @throws UnsupportedOperationException if the vertes is not in the graph anymore * @throws UnsupportedOperationException if the vertex is not in the graph anymore
*/ */
public void unMark() throws UnsupportedOperationException { public void unMark() throws UnsupportedOperationException {
throwIfNotContained(); throwIfNotContained();
@@ -78,9 +90,9 @@ public class Vertex<V> {
* Get all the marks that are associated with this vertex * Get all the marks that are associated with this vertex
* *
* @return a set of marks * @return a set of marks
* @throws UnsupportedOperationException if the vertes is not in the graph anymore * @throws UnsupportedOperationException if the vertex is not in the graph anymore
*/ */
public Collection<String> getMarks() throws UnsupportedOperationException { public Collection<Object> getMarks() throws UnsupportedOperationException {
throwIfNotContained(); throwIfNotContained();
return graph.getMarks(vertex); return graph.getMarks(vertex);
} }

View File

@@ -1140,6 +1140,7 @@ public class TestGraph {
shouldThrow(nullException, () -> vertex.addChild(null, 3)); shouldThrow(nullException, () -> vertex.addChild(null, 3));
shouldThrow(nullException, () -> vertex.addChild(null, null)); shouldThrow(nullException, () -> vertex.addChild(null, null));
shouldThrow(nullException, () -> vertex.mark(null)); shouldThrow(nullException, () -> vertex.mark(null));
shouldThrow(nullException, () -> vertex.unMark(null));
shouldThrow(nullException, () -> vertex.removeChild(null)); shouldThrow(nullException, () -> vertex.removeChild(null));
shouldThrow(new NullPointerException(), () -> vertex.visit(null, null)); shouldThrow(new NullPointerException(), () -> vertex.visit(null, null));
@@ -1185,6 +1186,13 @@ public class TestGraph {
shouldContain(graph.getMarks(vertex.getValue()), "ciao", "ciao2"); shouldContain(graph.getMarks(vertex.getValue()), "ciao", "ciao2");
vertex.unMark(); vertex.unMark();
shouldContain(vertex.getMarks()); shouldContain(vertex.getMarks());
vertex.mark("cio");
vertex.mark(1);
shouldContain(vertex.getMarks(), "cio", 1);
vertex.unMark(1);
shouldContain(vertex.getMarks(), "cio");
vertex.unMark("cio");
shouldContain(vertex.getMarks());
vertex.removeChild("1"); vertex.removeChild("1");
shouldContain(vertex.getChildren(), "2"); shouldContain(vertex.getChildren(), "2");