Class ImmutableGraph<V>
java.lang.Object
cloud.opencode.base.graph.ImmutableGraph<V>
- Type Parameters:
V- the vertex type | 顶点类型
- All Implemented Interfaces:
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 TypeMethodDescriptionvoidAlways throws UnsupportedOperationException 始终抛出 UnsupportedOperationExceptionvoidAlways throws UnsupportedOperationException 始终抛出 UnsupportedOperationExceptionvoidAlways throws UnsupportedOperationException 始终抛出 UnsupportedOperationExceptionvoidclear()Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationExceptionbooleancontainsEdge(V from, V to) Check if the graph contains an edge 检查图是否包含边booleancontainsVertex(V vertex) Check if the graph contains a vertex 检查图是否包含顶点static <V> ImmutableGraph<V> Create an immutable copy of the given graph 创建给定图的不可变副本intGet the number of edges 获取边数量edges()Get all edges 获取所有边doubleGet the weight of an edge 获取边的权重Get incoming edges to a vertex 获取顶点的入边booleanCheck if this is a directed graph 检查是否为有向图Get neighboring vertices 获取邻接顶点Get outgoing edges from a vertex 获取顶点的出边voidremoveEdge(V from, V to) Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationExceptionvoidremoveVertex(V vertex) Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationExceptiontoString()intGet the number of vertices 获取顶点数量vertices()Get all vertices 获取所有顶点
-
Method Details
-
copyOf
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
Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException- Specified by:
addVertexin interfaceGraph<V>- Parameters:
vertex- ignored | 忽略- Throws:
UnsupportedOperationException- always | 始终抛出
-
addEdge
-
addEdge
-
removeVertex
Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException- Specified by:
removeVertexin interfaceGraph<V>- Parameters:
vertex- ignored | 忽略- Throws:
UnsupportedOperationException- always | 始终抛出
-
removeEdge
Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException- Specified by:
removeEdgein interfaceGraph<V>- Parameters:
from- ignored | 忽略to- ignored | 忽略- Throws:
UnsupportedOperationException- always | 始终抛出
-
vertices
-
edges
-
neighbors
-
outEdges
-
inEdges
-
vertexCount
public int vertexCount()Description copied from interface:GraphGet the number of vertices 获取顶点数量- Specified by:
vertexCountin interfaceGraph<V>- Returns:
- vertex count | 顶点数量
-
edgeCount
-
containsVertex
Description copied from interface:GraphCheck if the graph contains a vertex 检查图是否包含顶点- Specified by:
containsVertexin interfaceGraph<V>- Parameters:
vertex- the vertex to check | 要检查的顶点- Returns:
- true if contains | 如果包含返回true
-
containsEdge
-
getWeight
Description copied from interface:GraphGet the weight of an edge 获取边的权重 -
isDirected
public boolean isDirected()Description copied from interface:GraphCheck if this is a directed graph 检查是否为有向图- Specified by:
isDirectedin interfaceGraph<V>- Returns:
- true if directed | 如果是有向图返回true
-
clear
public void clear()Always throws UnsupportedOperationException 始终抛出 UnsupportedOperationException- Specified by:
clearin interfaceGraph<V>- Throws:
UnsupportedOperationException- always | 始终抛出
-
toString
-