Class ImmutableGraph<V>

java.lang.Object
cloud.opencode.base.graph.ImmutableGraph<V>
Type Parameters:
V - the vertex type | 顶点类型
All Implemented Interfaces:
Graph<V>

public final class ImmutableGraph<V> extends Object implements Graph<V>
Immutable Graph - An unmodifiable snapshot of a graph 不可变图 - 图的不可修改快照

Creates a deep copy of a graph that implements Graph but throws UnsupportedOperationException on all mutation methods.

创建图的深拷贝,实现 Graph 接口,但在所有修改方法上抛出 UnsupportedOperationException

Features | 主要功能:

  • Deep copy detached from original graph - 与原图完全分离的深拷贝
  • All query methods work normally - 所有查询方法正常工作
  • All mutation methods throw UnsupportedOperationException - 所有修改方法抛出UnsupportedOperationException
  • Thread-safe (immutable) - 线程安全(不可变)

Usage Examples | 使用示例:

Graph<String> original = OpenGraph.directed();
original.addEdge("A", "B");
Graph<String> snapshot = ImmutableGraph.copyOf(original);
// snapshot.addVertex("C"); // throws UnsupportedOperationException
Since:
JDK 25, opencode-base-graph V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addEdge(V from, V to)
    Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
    void
    addEdge(V from, V to, double weight)
    Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
    void
    addVertex(V vertex)
    Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
    void
    Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
    boolean
    containsEdge(V from, V to)
    Check if the graph contains an edge 检查图是否包含边
    boolean
    containsVertex(V vertex)
    Check if the graph contains a vertex 检查图是否包含顶点
    static <V> ImmutableGraph<V>
    copyOf(Graph<V> graph)
    Create an immutable copy of the given graph 创建给定图的不可变副本
    int
    Get the number of edges 获取边数量
    Get all edges 获取所有边
    double
    getWeight(V from, V to)
    Get the weight of an edge 获取边的权重
    inEdges(V vertex)
    Get incoming edges to a vertex 获取顶点的入边
    boolean
    Check if this is a directed graph 检查是否为有向图
    neighbors(V vertex)
    Get neighboring vertices 获取邻接顶点
    outEdges(V vertex)
    Get outgoing edges from a vertex 获取顶点的出边
    void
    removeEdge(V from, V to)
    Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
    void
    removeVertex(V vertex)
    Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
     
    int
    Get the number of vertices 获取顶点数量
    Get all vertices 获取所有顶点

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface Graph

    inDegree, isEmpty, outDegree, snapshot
  • Method Details

    • copyOf

      public static <V> ImmutableGraph<V> copyOf(Graph<V> graph)
      Create an immutable copy of the given graph 创建给定图的不可变副本

      If the graph is already an ImmutableGraph, the same instance is returned without copying — the graph is already immutable, so no defensive copy is needed.

      如果图已经是 ImmutableGraph,则直接返回同一实例而不复制——该图已不可变,无需防御性拷贝。

      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the source graph | 源图
      Returns:
      an immutable copy, or the same instance if already immutable | 不可变副本,如果已是不可变图则返回同一实例
      Throws:
      NullPointerException - if graph is null | 当图为null时抛出
    • addVertex

      public void addVertex(V vertex)
      Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
      Specified by:
      addVertex in interface Graph<V>
      Parameters:
      vertex - ignored | 忽略
      Throws:
      UnsupportedOperationException - always | 始终抛出
    • addEdge

      public void addEdge(V from, V to)
      Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
      Specified by:
      addEdge in interface Graph<V>
      Parameters:
      from - ignored | 忽略
      to - ignored | 忽略
      Throws:
      UnsupportedOperationException - always | 始终抛出
    • addEdge

      public void addEdge(V from, V to, double weight)
      Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
      Specified by:
      addEdge in interface Graph<V>
      Parameters:
      from - ignored | 忽略
      to - ignored | 忽略
      weight - ignored | 忽略
      Throws:
      UnsupportedOperationException - always | 始终抛出
    • removeVertex

      public void removeVertex(V vertex)
      Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
      Specified by:
      removeVertex in interface Graph<V>
      Parameters:
      vertex - ignored | 忽略
      Throws:
      UnsupportedOperationException - always | 始终抛出
    • removeEdge

      public void removeEdge(V from, V to)
      Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
      Specified by:
      removeEdge in interface Graph<V>
      Parameters:
      from - ignored | 忽略
      to - ignored | 忽略
      Throws:
      UnsupportedOperationException - always | 始终抛出
    • vertices

      public Set<V> vertices()
      Description copied from interface: Graph
      Get all vertices 获取所有顶点
      Specified by:
      vertices in interface Graph<V>
      Returns:
      set of vertices | 顶点集合
    • edges

      public Set<Edge<V>> edges()
      Description copied from interface: Graph
      Get all edges 获取所有边
      Specified by:
      edges in interface Graph<V>
      Returns:
      set of edges | 边集合
    • neighbors

      public Set<V> neighbors(V vertex)
      Description copied from interface: Graph
      Get neighboring vertices 获取邻接顶点
      Specified by:
      neighbors in interface Graph<V>
      Parameters:
      vertex - the vertex | 顶点
      Returns:
      set of neighboring vertices | 邻接顶点集合
    • outEdges

      public Set<Edge<V>> outEdges(V vertex)
      Description copied from interface: Graph
      Get outgoing edges from a vertex 获取顶点的出边
      Specified by:
      outEdges in interface Graph<V>
      Parameters:
      vertex - the vertex | 顶点
      Returns:
      set of outgoing edges | 出边集合
    • inEdges

      public Set<Edge<V>> inEdges(V vertex)
      Description copied from interface: Graph
      Get incoming edges to a vertex 获取顶点的入边
      Specified by:
      inEdges in interface Graph<V>
      Parameters:
      vertex - the vertex | 顶点
      Returns:
      set of incoming edges | 入边集合
    • vertexCount

      public int vertexCount()
      Description copied from interface: Graph
      Get the number of vertices 获取顶点数量
      Specified by:
      vertexCount in interface Graph<V>
      Returns:
      vertex count | 顶点数量
    • edgeCount

      public int edgeCount()
      Description copied from interface: Graph
      Get the number of edges 获取边数量
      Specified by:
      edgeCount in interface Graph<V>
      Returns:
      edge count | 边数量
    • containsVertex

      public boolean containsVertex(V vertex)
      Description copied from interface: Graph
      Check if the graph contains a vertex 检查图是否包含顶点
      Specified by:
      containsVertex in interface Graph<V>
      Parameters:
      vertex - the vertex to check | 要检查的顶点
      Returns:
      true if contains | 如果包含返回true
    • containsEdge

      public boolean containsEdge(V from, V to)
      Description copied from interface: Graph
      Check if the graph contains an edge 检查图是否包含边
      Specified by:
      containsEdge in interface Graph<V>
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
      Returns:
      true if contains | 如果包含返回true
    • getWeight

      public double getWeight(V from, V to)
      Description copied from interface: Graph
      Get the weight of an edge 获取边的权重
      Specified by:
      getWeight in interface Graph<V>
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
      Returns:
      the edge weight, or Double.MAX_VALUE if no edge exists | 边权重,如果边不存在则返回Double.MAX_VALUE
    • isDirected

      public boolean isDirected()
      Description copied from interface: Graph
      Check if this is a directed graph 检查是否为有向图
      Specified by:
      isDirected in interface Graph<V>
      Returns:
      true if directed | 如果是有向图返回true
    • clear

      public void clear()
      Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException
      Specified by:
      clear in interface Graph<V>
      Throws:
      UnsupportedOperationException - always | 始终抛出
    • toString

      public String toString()
      Overrides:
      toString in class Object