-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#27 Making Node a self-referential interface type, allowing for speci…
…fic sub-types
- Loading branch information
1 parent
9a76633
commit 63a95a6
Showing
15 changed files
with
238 additions
and
166 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
javac-plugin/src/main/java/org/moditect/deptective/internal/graph/Cycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright 2019 The ModiTect authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.moditect.deptective.internal.graph; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* A cycle between multiple nodes. | ||
* | ||
* @author Gunnar Morling | ||
* @param <T> the specific node type | ||
*/ | ||
public class Cycle<T extends Node<T>> { | ||
|
||
private static final String STRING_DELIMITER = ", "; | ||
private final List<T> nodes; | ||
|
||
public Cycle(List<T> nodes) { | ||
this.nodes = Collections.unmodifiableList(nodes); | ||
} | ||
|
||
public List<T> getNodes() { | ||
return nodes; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return nodes.stream() | ||
.map(Node::asShortString) | ||
.sorted() | ||
.collect(Collectors.joining(STRING_DELIMITER)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,17 +18,17 @@ | |
/** | ||
* @author Gerd Wütherich ([email protected]) | ||
*/ | ||
public class Dependency { | ||
public class Dependency<T extends Node<T>> { | ||
|
||
private final Node to; | ||
private final T to; | ||
private final int aggregatedWeight; | ||
|
||
public Dependency(Node to, int aggregatedWeight) { | ||
public Dependency(T to, int aggregatedWeight) { | ||
this.to = to; | ||
this.aggregatedWeight = aggregatedWeight; | ||
} | ||
|
||
public Node getTo() { | ||
public T getTo() { | ||
return to; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,13 @@ | |
/** | ||
* @author Gerd Wütherich ([email protected]) | ||
*/ | ||
public interface IDependencyStructureMatrix { | ||
public interface IDependencyStructureMatrix<T extends Node<T>> { | ||
|
||
List<Node> getOrderedNodes(); | ||
List<T> getOrderedNodes(); | ||
|
||
List<Dependency> getUpwardDependencies(); | ||
List<Dependency<T>> getUpwardDependencies(); | ||
|
||
List<List<Node>> getCycles(); | ||
List<List<T>> getCycles(); | ||
|
||
boolean isCellInCycle(int i, int j); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,87 +15,35 @@ | |
*/ | ||
package org.moditect.deptective.internal.graph; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
/** | ||
* A node in a graph. | ||
* | ||
* @author Gerd Wütherich ([email protected]) | ||
* @author Gunnar Morling | ||
* @param <T> A specific sub-type, following the self-referential generic type pattern | ||
*/ | ||
public class Node { | ||
|
||
private Map<Node, Dependency> outgoingDependencies; | ||
private final String id; | ||
|
||
public Node(String id) { | ||
this.id = Objects.requireNonNull(id); | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public Dependency getOutgoingDependencyTo(Node node) { | ||
if (!hasOutgoingDependencies()) { | ||
return null; | ||
} | ||
|
||
return outgoingDependencies.get(node); | ||
} | ||
public interface Node<T extends Node<T>> { | ||
|
||
public Set<Dependency> getOutgoingDependenciesTo(Collection<Node> nodes) { | ||
return Objects.requireNonNull(nodes).stream() | ||
.map(node -> getOutgoingDependencyTo(node)) | ||
.filter(dep -> dep != null) | ||
default Set<Dependency<T>> getOutgoingDependenciesTo(Iterable<T> nodes) { | ||
return StreamSupport.stream(nodes.spliterator(), false) | ||
.map(c -> getOutgoingDependencyTo(c)) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
public boolean hasOutgoingDependencies() { | ||
return outgoingDependencies != null && !outgoingDependencies.isEmpty(); | ||
} | ||
|
||
public void addOutgoingDependency(Node to, int aggregatedWeight) { | ||
outgoingDependencies().put(to, new Dependency(to, aggregatedWeight)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Node [id=" + id + "]"; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((id == null) ? 0 : id.hashCode()); | ||
return result; | ||
} | ||
Dependency<T> getOutgoingDependencyTo(T node); | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
Node other = (Node) obj; | ||
if (id == null) { | ||
if (other.id != null) | ||
return false; | ||
} | ||
else if (!id.equals(other.id)) | ||
return false; | ||
return true; | ||
} | ||
boolean hasOutgoingDependencies(); | ||
|
||
private Map<Node, Dependency> outgoingDependencies() { | ||
if (outgoingDependencies == null) { | ||
outgoingDependencies = new HashMap<>(); | ||
} | ||
return outgoingDependencies; | ||
/** | ||
* Returns a concise string representation of this node, e.g. used when rendering this node in a diagram. | ||
*/ | ||
default String asShortString() { | ||
return toString(); | ||
} | ||
} |
Oops, something went wrong.