Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
*
*/
package rinde.sim.core.graph;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.Nullable;
import com.google.common.base.Objects;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
/**
* Multimap-based implementation of a graph.
* @author Rinde van Lon
* @author Bartosz Michalik - added edge data
* + and dead end nodes
* @param The type of {@link ConnectionData} that is used in the edges.
*/
public class MultimapGraph extends AbstractGraph {
private final Multimap data;
private final Map, E> edgeData;
private final Set deadEndNodes;
/**
* Create a new empty graph.
*/
public MultimapGraph() {
data = LinkedHashMultimap.create();
this.edgeData = new HashMap, E>();
deadEndNodes = new LinkedHashSet();
}
/**
* Instantiates a new graph using the specified multimap.
* @param map The multimap that is copied into this new graph.
*/
public MultimapGraph(Multimap map) {
this.data = LinkedHashMultimap.create(map);
this.edgeData = new HashMap, E>();
this.deadEndNodes = new HashSet();
deadEndNodes.addAll(data.values());
deadEndNodes.removeAll(data.keySet());
}
@Override
public boolean containsNode(Point node) {
return data.containsKey(node) || deadEndNodes.contains(node);
}
@Override
public Collection getOutgoingConnections(Point node) {
return data.get(node);
}
@Override
public boolean hasConnection(Point from, Point to) {
return data.containsEntry(from, to);
}
@Override
public int getNumberOfConnections() {
return data.size();
}
@Override
public int getNumberOfNodes() {
return data.keySet().size() + deadEndNodes.size();
}
@Override
public E setConnectionData(Point from, Point to, @Nullable E connData) {
if (!hasConnection(from, to)) {
throw new IllegalArgumentException("the connection " + from + " -> " + to
+ "does not exist");
}
return this.edgeData.put(new Connection(from, to, null), connData);
}
@Nullable
@Override
public E connectionData(Point from, Point to) {
return edgeData.get(new Connection(from, to, null));
}
@Override
public Set getNodes() {
final Set nodes = new LinkedHashSet(data.keySet());
nodes.addAll(deadEndNodes);
return nodes;
}
@Override
public List> getConnections() {
final List> res = new ArrayList>(
edgeData.size());
for (final Entry p : data.entries()) {
final Connection connection = new Connection(p.getKey(),
p.getValue(), null);
final E eD = edgeData.get(connection);
connection.setData(eD);
res.add(connection);
}
return res;
}
/**
* Returns an unmodifiable view on the {@link Multimap} which back this graph.
* @return The view on the multimap.
*/
public Multimap getMultimap() {
return Multimaps.unmodifiableMultimap(data);
}
@Override
public boolean isEmpty() {
return data.isEmpty();
}
/**
* Warning: very inefficient! If this function is needed regularly it is
* advised to use {@link TableGraph} instead. {@inheritDoc}
*/
@Override
public Collection getIncomingConnections(Point node) {
final Set set = new LinkedHashSet();
for (final Entry entry : data.entries()) {
if (entry.getValue().equals(node)) {
set.add(entry.getKey());
}
}
return set;
}
/**
* Warning: very inefficient! If this function is needed regularly it is
* advised to use {@link TableGraph} instead.
* @param node The node to remove.
*/
@Override
public void removeNode(Point node) {
// copy data first to avoid concurrent modification exceptions
final List out = new ArrayList();
out.addAll(getOutgoingConnections(node));
for (final Point p : out) {
removeConnection(node, p);
}
final List in = new ArrayList();
in.addAll(getIncomingConnections(node));
for (final Point p : in) {
removeConnection(p, node);
}
deadEndNodes.remove(node);
}
@Override
public void removeConnection(Point from, Point to) {
checkArgument(hasConnection(from, to),
"Can not remove non-existing connection: %s -> %s", from, to);
data.remove(from, to);
removeData(from, to);
if (!data.containsKey(to)) {
deadEndNodes.add(to);
}
}
private void removeData(Point from, Point to) {
edgeData.remove(new Connection(from, to, null));
}
@Override
public int hashCode() {
return Objects.hashCode(data, deadEndNodes, edgeData);
}
@Override
protected void doAddConnection(Point from, Point to, @Nullable E connData) {
data.put(from, to);
deadEndNodes.remove(from);
if (!data.containsKey(to)) {
deadEndNodes.add(to);
}
if (connData != null) {
this.edgeData.put(new Connection(from, to, null), connData);
}
}
}