org.testifyproject.guava.common.graph.ValueGraph Maven / Gradle / Ivy
/*
* Copyright (C) 2016 The Guava 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.testifyproject.guava.common.graph;
import org.testifyproject.guava.common.annotations.Beta;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
/**
* An interface for graph-structured data,
* whose edges have associated non-unique values.
*
* A graph is composed of a set of nodes and a set of edges connecting pairs of nodes.
*
*
There are three primary interfaces provided to represent graphs. In order of increasing
* complexity they are: {@link Graph}, {@link ValueGraph}, and {@link Network}. You should generally
* prefer the simplest interface that satisfies your use case. See the
* "Choosing the right graph type" section of the Guava User Guide for more details.
*
*
Capabilities
*
* {@code ValueGraph} supports the following use cases (definitions of
* terms):
*
*
* - directed graphs
*
- undirected graphs
*
- graphs that do/don't allow self-loops
*
- graphs whose nodes/edges are insertion-ordered, sorted, or unordered
*
- graphs whose edges have associated values
*
*
* {@code ValueGraph}, as a subtype of {@code Graph}, explicitly does not support parallel edges,
* and forbids implementations or extensions with parallel edges. If you need parallel edges, use
* {@link Network}. (You can use a positive {@code Integer} edge value as a loose representation of
* edge multiplicity, but the {@code *degree()} and mutation methods will not reflect your
* interpretation of the edge value as its multiplicity.)
*
*
Building a {@code ValueGraph}
*
* The implementation classes that {@code common.graph} provides are not public, by design. To
* create an instance of one of the built-in implementations of {@code ValueGraph}, use the
* {@link ValueGraphBuilder} class:
*
*
{@code
* MutableValueGraph graph = ValueGraphBuilder.directed().build();
* }
*
* {@link ValueGraphBuilder#build()} returns an instance of {@link MutableValueGraph}, which is a
* subtype of {@code ValueGraph} that provides methods for adding and removing nodes and edges. If
* you do not need to mutate a graph (e.g. if you write a method than runs a read-only algorithm on
* the graph), you should use the non-mutating {@link ValueGraph} interface, or an {@link
* ImmutableValueGraph}.
*
*
You can create an immutable copy of an existing {@code ValueGraph} using {@link
* ImmutableValueGraph#copyOf(ValueGraph)}:
*
*
{@code
* ImmutableValueGraph immutableGraph = ImmutableValueGraph.copyOf(graph);
* }
*
* Instances of {@link ImmutableValueGraph} do not implement {@link MutableValueGraph}
* (obviously!) and are contractually guaranteed to be unmodifiable and thread-safe.
*
*
The Guava User Guide has more
* information on (and examples of) building graphs.
*
*
Additional documentation
*
* See the Guava User Guide for the {@code common.graph} package ("Graphs Explained") for
* additional documentation, including:
*
*
* -
* {@code equals()}, {@code hashCode()}, and graph equivalence
*
-
* Synchronization policy
*
- Notes
* for implementors
*
*
* @author James Sexton
* @author Joshua O'Madadhain
* @param Node parameter type
* @param Value parameter type
* @since 20.0
*/
@Beta
public interface ValueGraph extends BaseGraph {
//
// ValueGraph-level accessors
//
/** {@inheritDoc} */
@Override
Set nodes();
/** {@inheritDoc} */
@Override
Set> edges();
/**
* Returns a live view of this graph as a {@link Graph}. The resulting {@link Graph} will have an
* edge connecting node A to node B if this {@link ValueGraph} has an edge connecting A to B.
*/
Graph asGraph();
//
// ValueGraph properties
//
/** {@inheritDoc} */
@Override
boolean isDirected();
/** {@inheritDoc} */
@Override
boolean allowsSelfLoops();
/** {@inheritDoc} */
@Override
ElementOrder nodeOrder();
//
// Element-level accessors
//
/** {@inheritDoc} */
@Override
Set adjacentNodes(N node);
/** {@inheritDoc} */
@Override
Set predecessors(N node);
/** {@inheritDoc} */
@Override
Set successors(N node);
/** {@inheritDoc} */
@Override
int degree(N node);
/** {@inheritDoc} */
@Override
int inDegree(N node);
/** {@inheritDoc} */
@Override
int outDegree(N node);
/** {@inheritDoc} */
@Override
boolean hasEdgeConnecting(N nodeU, N nodeV);
/**
* Returns the value of the edge connecting {@code nodeU} to {@code nodeV}, if one is present;
* otherwise, returns {@code Optional.empty()}.
*
* In an undirected graph, this is equal to {@code edgeValue(nodeV, nodeU)}.
*
* @throws IllegalArgumentException if {@code nodeU} or {@code nodeV} is not an element of this
* graph
* @since 23.0 (since 20.0 with return type {@code V})
*/
Optional edgeValue(N nodeU, N nodeV);
/**
* Returns the value of the edge connecting {@code nodeU} to {@code nodeV}, if one is present;
* otherwise, returns {@code defaultValue}.
*
* In an undirected graph, this is equal to {@code edgeValueOrDefault(nodeV, nodeU,
* defaultValue)}.
*
* @throws IllegalArgumentException if {@code nodeU} or {@code nodeV} is not an element of this
* graph
*/
@Nullable
V edgeValueOrDefault(N nodeU, N nodeV, @Nullable V defaultValue);
//
// ValueGraph identity
//
/**
* Returns {@code true} iff {@code object} is a {@link ValueGraph} that has the same elements and
* the same structural relationships as those in this graph.
*
*
Thus, two value graphs A and B are equal if all of the following are true:
*
*
* - A and B have equal {@link #isDirected() directedness}.
*
- A and B have equal {@link #nodes() node sets}.
*
- A and B have equal {@link #edges() edge sets}.
*
- The {@link #edgeValue(Object, Object) value} of a given edge is the same in both A and B.
*
*
* Graph properties besides {@link #isDirected() directedness} do not affect equality.
* For example, two graphs may be considered equal even if one allows self-loops and the other
* doesn't. Additionally, the order in which nodes or edges are added to the graph, and the order
* in which they are iterated over, are irrelevant.
*
*
A reference implementation of this is provided by {@link AbstractValueGraph#equals(Object)}.
*/
@Override
boolean equals(@Nullable Object object);
/**
* Returns the hash code for this graph. The hash code of a graph is defined as the hash code of a
* map from each of its {@link #edges() edges} to the associated {@link #edgeValue(Object, Object)
* edge value}.
*
*
A reference implementation of this is provided by {@link AbstractValueGraph#hashCode()}.
*/
@Override
int hashCode();
}