Class GraphBuilder<V>

java.lang.Object
cloud.opencode.base.graph.builder.GraphBuilder<V>
Type Parameters:
V - the vertex type | 顶点类型

Performance | 性能特性:

  • Time complexity: O(1) per operation - 时间复杂度: O(1) 每次操作
  • Space complexity: O(V + E) - 空间复杂度: O(V + E)

public final class GraphBuilder<V> extends Object
Graph Builder 图构建器

Fluent builder for constructing graphs.

用于构建图的流式构建器。

Features | 特性:

  • Fluent API for graph construction | 流式API用于图构建
  • Batch edge/vertex addition | 批量添加边/顶点
  • Validation support | 验证支持
  • Initial capacity configuration | 初始容量配置

Usage Examples | 使用示例:

// Build a directed graph
Graph<String> graph = GraphBuilder.<String>directed()
    .addEdge("A", "B", 1.0)
    .addEdge("B", "C", 2.0)
    .addEdge("C", "D", 3.0)
    .build();

// Build with validation
Graph<String> validGraph = GraphBuilder.<String>directed()
    .addEdge("A", "B")
    .addEdge("B", "C")
    .buildAndValidate();

Security | 安全性:

  • Thread-safe: No (builder is not thread-safe, built graph is) - 线程安全: 否(构建器非线程安全,构建的图是)
  • Null-safe: No (does not validate inputs during building) - 空值安全: 否(构建过程中不验证输入)
Since:
JDK 25, opencode-base-graph V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • directed

      public static <V> GraphBuilder<V> directed()
      Create a builder for directed graph 创建有向图构建器
      Type Parameters:
      V - the vertex type | 顶点类型
      Returns:
      a new graph builder | 新的图构建器
    • undirected

      public static <V> GraphBuilder<V> undirected()
      Create a builder for undirected graph 创建无向图构建器
      Type Parameters:
      V - the vertex type | 顶点类型
      Returns:
      a new graph builder | 新的图构建器
    • initialCapacity

      public GraphBuilder<V> initialCapacity(int capacity)
      Set initial capacity for the graph 设置图的初始容量
      Parameters:
      capacity - the initial capacity | 初始容量
      Returns:
      this builder | 此构建器
    • addVertex

      public GraphBuilder<V> addVertex(V vertex)
      Add a vertex 添加顶点
      Parameters:
      vertex - the vertex to add | 要添加的顶点
      Returns:
      this builder | 此构建器
    • addVertices

      @SafeVarargs public final GraphBuilder<V> addVertices(V... vertices)
      Add multiple vertices 添加多个顶点
      Parameters:
      vertices - the vertices to add | 要添加的顶点
      Returns:
      this builder | 此构建器
    • addVertices

      public GraphBuilder<V> addVertices(Collection<V> vertices)
      Add vertices from collection 从集合添加顶点
      Parameters:
      vertices - the vertices to add | 要添加的顶点
      Returns:
      this builder | 此构建器
    • addEdge

      public GraphBuilder<V> addEdge(V from, V to)
      Add an edge with default weight 添加默认权重的边
      Parameters:
      from - source vertex | 源顶点
      to - target vertex | 目标顶点
      Returns:
      this builder | 此构建器
    • addEdge

      public GraphBuilder<V> addEdge(V from, V to, double weight)
      Add an edge with weight 添加带权重的边
      Parameters:
      from - source vertex | 源顶点
      to - target vertex | 目标顶点
      weight - edge weight | 边权重
      Returns:
      this builder | 此构建器
    • addEdge

      public GraphBuilder<V> addEdge(Edge<V> edge)
      Add an edge 添加边
      Parameters:
      edge - the edge to add | 要添加的边
      Returns:
      this builder | 此构建器
    • addEdges

      public GraphBuilder<V> addEdges(Collection<Edge<V>> edges)
      Add multiple edges 添加多个边
      Parameters:
      edges - the edges to add | 要添加的边
      Returns:
      this builder | 此构建器
    • configure

      public GraphBuilder<V> configure(Consumer<GraphBuilder<V>> configurer)
      Configure the builder 配置构建器
      Parameters:
      configurer - the configuration function | 配置函数
      Returns:
      this builder | 此构建器
    • build

      public Graph<V> build()
      Build the graph 构建图
      Returns:
      the built graph | 构建的图
    • buildAndValidate

      public Graph<V> buildAndValidate()
      Build and validate the graph 构建并验证图
      Returns:
      the built and validated graph | 构建并验证的图