001/* 002 * Copyright (C) 2016 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.graph; 018 019import com.google.common.annotations.Beta; 020import javax.annotation.Nullable; 021 022/** 023 * An interface for <a href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)">graph</a>- 024 * structured data, whose edges have associated non-unique values. 025 * 026 * <p>A graph is composed of a set of nodes and a set of edges connecting pairs of nodes. 027 * 028 * <p>There are three main interfaces provided to represent graphs. In order of increasing 029 * complexity they are: {@link Graph}, {@link ValueGraph}, and {@link Network}. You should generally 030 * prefer the simplest interface that satisfies your use case. See the <a 031 * href="https://github.com/google/guava/wiki/GraphsExplained#choosing-the-right-graph-type"> 032 * "Choosing the right graph type"</a> section of the Guava User Guide for more details. 033 * 034 * <h3>Capabilities</h3> 035 * 036 * <p>{@code ValueGraph} supports the following use cases (<a 037 * href="https://github.com/google/guava/wiki/GraphsExplained#definitions">definitions of 038 * terms</a>): 039 * 040 * <ul> 041 * <li>directed graphs 042 * <li>undirected graphs 043 * <li>graphs that do/don't allow self-loops 044 * <li>graphs whose nodes/edges are insertion-ordered, sorted, or unordered 045 * <li>graphs whose edges have associated values 046 * </ul> 047 * 048 * <p>{@code ValueGraph}, as a subtype of {@code Graph}, explicitly does not support parallel edges, 049 * and forbids implementations or extensions with parallel edges. If you need parallel edges, use 050 * {@link Network}. (You can use a positive {@code Integer} edge value as a loose representation of 051 * edge multiplicity, but the {@code *degree()} and mutation methods will not reflect your 052 * interpretation of the edge value as its multiplicity.) 053 * 054 * <h3>Building a {@code ValueGraph}</h3> 055 * 056 * <p>The implementation classes that `common.graph` provides are not public, by design. To create 057 * an instance of one of the built-in implementations of {@code ValueGraph}, use the {@link 058 * ValueGraphBuilder} class: 059 * 060 * <pre>{@code 061 * MutableValueGraph<Integer, Double> graph = ValueGraphBuilder.directed().build(); 062 * }</pre> 063 * 064 * <p>{@link ValueGraphBuilder#build()} returns an instance of {@link MutableValueGraph}, which is a 065 * subtype of {@code ValueGraph} that provides methods for adding and removing nodes and edges. If 066 * you do not need to mutate a graph (e.g. if you write a method than runs a read-only algorithm on 067 * the graph), you should use the non-mutating {@link ValueGraph} interface, or an {@link 068 * ImmutableValueGraph}. 069 * 070 * <p>You can create an immutable copy of an existing {@code ValueGraph} using {@link 071 * ImmutableValueGraph#copyOf(ValueGraph)}: 072 * 073 * <pre>{@code 074 * ImmutableValueGraph<Integer, Double> immutableGraph = ImmutableValueGraph.copyOf(graph); 075 * }</pre> 076 * 077 * <p>Instances of {@link ImmutableValueGraph} do not implement {@link MutableValueGraph} 078 * (obviously!) and are contractually guaranteed to be unmodifiable and thread-safe. 079 * 080 * <p>The Guava User Guide has <a 081 * href="https://github.com/google/guava/wiki/GraphsExplained#building-graph-instances">more 082 * information on (and examples of) building graphs</a>. 083 * 084 * <h3>Additional documentation</h3> 085 * 086 * <p>See the Guava User Guide for the {@code common.graph} package (<a 087 * href="https://github.com/google/guava/wiki/GraphsExplained">"Graphs Explained"</a>) for 088 * additional documentation, including: 089 * 090 * <ul> 091 * <li><a 092 * href="https://github.com/google/guava/wiki/GraphsExplained#equals-hashcode-and-graph-equivalence"> 093 * {@code equals()}, {@code hashCode()}, and graph equivalence</a> 094 * <li><a href="https://github.com/google/guava/wiki/GraphsExplained#synchronization"> 095 * Synchronization policy</a> 096 * <li><a href="https://github.com/google/guava/wiki/GraphsExplained#notes-for-implementors">Notes 097 * for implementors</a> 098 * </ul> 099 * 100 * @author James Sexton 101 * @author Joshua O'Madadhain 102 * @param <N> Node parameter type 103 * @param <V> Value parameter type 104 * @since 20.0 105 */ 106@Beta 107public interface ValueGraph<N, V> extends Graph<N> { 108 109 /** 110 * If there is an edge connecting {@code nodeU} to {@code nodeV}, returns the non-null value 111 * associated with that edge. 112 * 113 * <p>In an undirected graph, this is equal to {@code edgeValue(nodeV, nodeU)}. 114 * 115 * @throws IllegalArgumentException if there is no edge connecting {@code nodeU} to {@code nodeV}. 116 */ 117 V edgeValue(Object nodeU, Object nodeV); 118 119 /** 120 * If there is an edge connecting {@code nodeU} to {@code nodeV}, returns the non-null value 121 * associated with that edge; otherwise, returns {@code defaultValue}. 122 * 123 * <p>In an undirected graph, this is equal to {@code edgeValueOrDefault(nodeV, nodeU, 124 * defaultValue)}. 125 */ 126 V edgeValueOrDefault(Object nodeU, Object nodeV, @Nullable V defaultValue); 127 128 // 129 // ValueGraph identity 130 // 131 132 /** 133 * For the default {@link ValueGraph} implementations, returns true if {@code this == object} 134 * (reference equality). External implementations are free to define this method as they see fit, 135 * as long as they satisfy the {@link Object#equals(Object)} contract. 136 * 137 * <p>To compare two {@link ValueGraph}s based on their contents rather than their references, see 138 * {@link Graphs#equivalent(ValueGraph, ValueGraph)}. 139 */ 140 @Override 141 boolean equals(@Nullable Object object); 142 143 /** 144 * For the default {@link ValueGraph} implementations, returns {@code 145 * System.identityHashCode(this)}. External implementations are free to define this method as they 146 * see fit, as long as they satisfy the {@link Object#hashCode()} contract. 147 */ 148 @Override 149 int hashCode(); 150}