All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testifyproject.google.common.graph.GraphBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2016 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in org.testifyproject.testifyprojectpliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org.testifyproject/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.testifyproject.google.org.testifyproject.testifyprojectmon.graph;

import static org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.base.Preconditions.checkNotNull;
import static org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.graph.Graphs.checkNonNegative;

import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.annotations.Beta;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.base.Optional;

/**
 * A builder for constructing instances of {@link MutableGraph} with user-defined properties.
 *
 * 

A graph built by this class will have the following properties by default: * *

    *
  • does not allow self-loops *
  • orders {@link Graph#nodes()} in the order in which the elements were added *
* *

Example of use: * *

{@code
 * MutableGraph graph = GraphBuilder.undirected().allowsSelfLoops(true).build();
 * graph.putEdge("bread", "bread");
 * graph.putEdge("chocolate", "peanut butter");
 * graph.putEdge("peanut butter", "jelly");
 * }
* * @author James Sexton * @author Joshua O'Madadhain * @since 20.0 */ @Beta public final class GraphBuilder extends AbstractGraphBuilder { /** Creates a new instance with the specified edge directionality. */ private GraphBuilder(boolean directed) { super(directed); } /** Returns a {@link GraphBuilder} for building directed graphs. */ public static GraphBuilder directed() { return new GraphBuilder(true); } /** Returns a {@link GraphBuilder} for building undirected graphs. */ public static GraphBuilder undirected() { return new GraphBuilder(false); } /** * Returns a {@link GraphBuilder} initialized with all properties queryable from {@code graph}. * *

The "queryable" properties are those that are exposed through the {@link Graph} interface, * such as {@link Graph#isDirected()}. Other properties, such as {@link #expectedNodeCount(int)}, * are not set in the new builder. */ public static GraphBuilder from(Graph graph) { return new GraphBuilder(graph.isDirected()) .allowsSelfLoops(graph.allowsSelfLoops()) .nodeOrder(graph.nodeOrder()); } /** * Specifies whether the graph will allow self-loops (edges that connect a node to itself). * Attempting to add a self-loop to a graph that does not allow them will throw an {@link * UnsupportedOperationException}. */ public GraphBuilder allowsSelfLoops(boolean allowsSelfLoops) { this.allowsSelfLoops = allowsSelfLoops; return this; } /** * Specifies the expected number of nodes in the graph. * * @throws IllegalArgumentException if {@code expectedNodeCount} is negative */ public GraphBuilder expectedNodeCount(int expectedNodeCount) { this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); return this; } /** Specifies the order of iteration for the elements of {@link Graph#nodes()}. */ public GraphBuilder nodeOrder(ElementOrder nodeOrder) { GraphBuilder newBuilder = cast(); newBuilder.nodeOrder = checkNotNull(nodeOrder); return newBuilder; } /** Returns an empty {@link MutableGraph} with the properties of this {@link GraphBuilder}. */ public MutableGraph build() { return new ConfigurableMutableGraph(this); } @SuppressWarnings("unchecked") private GraphBuilder cast() { return (GraphBuilder) this; } }