/** * Creates an immutable graph based on the specified connections. Duplicate * connections are not allowed and will cause this method to fail. * @param connections The connections to use for creating a graph. * @return A new instance of an immutable graph. */ public static <E extends ConnectionData> ImmutableGraph<E> copyOf( Iterable<? extends Connection<? extends E>> connections) { return new ImmutableGraph<>(connections); }
@Override public int getNumberOfNodes() { return getNodes().size(); }
@Override public Connection<E> getConnection(Point from, Point to) { checkArgument(hasConnection(from, to), "%s -> %s is not a connection", from, to); return data.get(from, to); }
private void updateSnapshot() { snapshot = Optional.of(GraphRoadModelSnapshot.create( ImmutableGraph.copyOf(getGraph()), getDistanceUnit())); }
@Override public int hashCode() { int h = 1; h *= 1000003; h ^= this.graph.hashCode(); h *= 1000003; h ^= this.modelDistanceUnit.hashCode(); return h; }
@Override public Measure<Double, Length> getDistanceOfPath(Iterable<Point> path) throws IllegalArgumentException { final Iterator<Point> pathIt = path.iterator(); checkArgument(pathIt.hasNext(), "Cannot check distance of an empty path."); Point prev = pathIt.next(); Point cur = null; double distance = 0d; while (pathIt.hasNext()) { cur = pathIt.next(); distance += getGraph().connectionLength(prev, cur); prev = cur; } return Measure.valueOf(distance, getModelDistanceUnit()); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof GraphRoadModelSnapshot) { GraphRoadModelSnapshot that = (GraphRoadModelSnapshot) o; return (this.graph.equals(that.getGraph())) && (this.modelDistanceUnit.equals(that.getModelDistanceUnit())); } return false; }
/** * Creates a snapshot of a {@link GraphRoadModel}. * @param graph The graph to snapshot. * @param modelDistanceUnit The distance unit of the model. * @return A snapshot with the desired properties. */ public static GraphRoadModelSnapshot createGraphRoadModelSnapshot( Graph<?> graph, Unit<Length> modelDistanceUnit) { return new AutoValue_GraphRoadModelSnapshot(ImmutableGraph.copyOf(graph), modelDistanceUnit); } }
/** * Creates a new instance using the specified {@link Graph} as road structure. * The default units are used as defined by {@link AbstractRoadModel}. * @param g The graph which will be used as road structure. * @param b The builder that contains the properties. */ protected GraphRoadModelImpl(Graph<? extends ConnectionData> g, RoadModelBuilders.AbstractGraphRMB<?, ?, ?> b) { super(b.getDistanceUnit(), b.getSpeedUnit()); graph = g; snapshot = GraphRoadModelSnapshot.create( ImmutableGraph.copyOf(graph), b.getDistanceUnit()); registry = GraphSpatialRegistry.create(MapSpatialRegistry.<RoadUser>create()); }
@Override public Connection<E> getConnection(Point from, Point to) { checkArgument(hasConnection(from, to), "%s -> %s is not a connection", from, to); return data.get(from, to); }
/** * Creates an immutable graph based on the specified connections. Duplicate * connections are not allowed and will cause this method to fail. * @param connections The connections to use for creating a graph. * @return A new instance of an immutable graph. */ public static <E extends ConnectionData> ImmutableGraph<E> copyOf( Iterable<? extends Connection<? extends E>> connections) { return new ImmutableGraph<>(connections); }
@Override public int getNumberOfNodes() { return getNodes().size(); }
@Override public <T extends ConnectionData> boolean hasConnection( Connection<T> connection) { return hasConnection(connection.from(), connection.to()) && data.get(connection.from(), connection.to()).equals(connection); }
/** * Creates an immutable copy of the specified {@link Graph}. This method * recognizes when the supplied graph is an instance of {@link ImmutableGraph} * , and will avoid making a copy in this case. * @param graph A graph. * @param <E> The type of connection data. * @return An immutable copy of the graph. */ @SuppressWarnings("unchecked") public static <E extends ConnectionData> ImmutableGraph<E> copyOf( Graph<? extends E> graph) { if (graph instanceof ImmutableGraph) { return (ImmutableGraph<E>) graph; } return new ImmutableGraph<>(graph.getConnections()); }
@Override public <T extends ConnectionData> boolean hasConnection( Connection<T> connection) { return hasConnection(connection.from(), connection.to()) && data.get(connection.from(), connection.to()).equals(connection); }
/** * Creates an immutable copy of the specified {@link Graph}. This method * recognizes when the supplied graph is an instance of {@link ImmutableGraph} * , and will avoid making a copy in this case. * @param graph A graph. * @param <E> The type of connection data. * @return An immutable copy of the graph. */ @SuppressWarnings("unchecked") public static <E extends ConnectionData> ImmutableGraph<E> copyOf( Graph<? extends E> graph) { if (graph instanceof ImmutableGraph) { return (ImmutableGraph<E>) graph; } return new ImmutableGraph<>(graph.getConnections()); }