Class SafeGraphOperations
java.lang.Object
cloud.opencode.base.graph.security.SafeGraphOperations
Safe Graph Operations
安全图操作
Wrapper for graph operations with resource limits and timeout control.
带资源限制和超时控制的图操作包装器。
Features | 主要功能:
- Maximum vertex count limit | 最大顶点数量限制
- Maximum edge count limit | 最大边数量限制
- Timeout control for algorithms | 算法超时控制
- Resource exhaustion protection | 资源耗尽保护
Usage Examples | 使用示例:
// Safe vertex addition
SafeGraphOperations.safeAddVertex(graph, vertex);
// Safe edge addition
SafeGraphOperations.safeAddEdge(graph, from, to, weight);
// Shortest path with timeout
List<String> path = SafeGraphOperations.safeShortestPath(graph, source, target);
Security | 安全性:
- Thread-safe: No (mutable static configuration) - 线程安全: 否(可变静态配置)
- Null-safe: Yes (validates inputs before operations) - 空值安全: 是(操作前验证输入)
- Since:
- JDK 25, opencode-base-graph V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum traversal depth 默认最大遍历深度static final intDefault maximum edge count 默认最大边数量static final intDefault maximum vertex count 默认最大顶点数量static final DurationDefault timeout duration 默认超时时间 -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TexecuteWithTimeout(Callable<T> callable, String operationName) Execute a callable with timeout 带超时执行可调用任务static intGet current maximum depth 获取当前最大深度static intGet current maximum edge count 获取当前最大边数量static intGet current maximum vertex count 获取当前最大顶点数量static DurationGet current timeout 获取当前超时时间static voidReset all limits to defaults 重置所有限制为默认值static <V> voidsafeAddEdge(Graph<V> graph, V from, V to) Safely add an edge with limit check 安全添加边(带限制检查)static <V> voidsafeAddEdge(Graph<V> graph, V from, V to, double weight) Safely add an edge with weight and limit check 安全添加带权重的边(带限制检查)static <V> voidsafeAddVertex(Graph<V> graph, V vertex) Safely add a vertex with limit check 安全添加顶点(带限制检查)safeDijkstra(Graph<V> graph, V source) Safely compute Dijkstra distances with timeout 安全计算Dijkstra距离(带超时)static <V> List<V> safeShortestPath(Graph<V> graph, V source, V target) Safely compute shortest path with timeout 安全计算最短路径(带超时)static voidsetMaxDepth(int max) Configure maximum traversal depth 配置最大遍历深度static voidsetMaxEdges(int max) Configure maximum edge count 配置最大边数量static voidsetMaxVertices(int max) Configure maximum vertex count 配置最大顶点数量static voidsetTimeout(Duration duration) Configure timeout duration 配置超时时间static <V> booleanwouldExceedEdgeLimit(Graph<V> graph) Check if adding edge would exceed limit 检查添加边是否会超出限制static <V> booleanwouldExceedVertexLimit(Graph<V> graph) Check if adding vertex would exceed limit 检查添加顶点是否会超出限制
-
Field Details
-
DEFAULT_MAX_VERTICES
public static final int DEFAULT_MAX_VERTICESDefault maximum vertex count 默认最大顶点数量- See Also:
-
DEFAULT_MAX_EDGES
public static final int DEFAULT_MAX_EDGESDefault maximum edge count 默认最大边数量- See Also:
-
DEFAULT_MAX_DEPTH
public static final int DEFAULT_MAX_DEPTHDefault maximum traversal depth 默认最大遍历深度- See Also:
-
DEFAULT_TIMEOUT
Default timeout duration 默认超时时间
-
-
Method Details
-
setMaxVertices
public static void setMaxVertices(int max) Configure maximum vertex count 配置最大顶点数量- Parameters:
max- the maximum vertex count | 最大顶点数量
-
setMaxEdges
public static void setMaxEdges(int max) Configure maximum edge count 配置最大边数量- Parameters:
max- the maximum edge count | 最大边数量
-
setMaxDepth
public static void setMaxDepth(int max) Configure maximum traversal depth 配置最大遍历深度- Parameters:
max- the maximum depth | 最大深度
-
setTimeout
Configure timeout duration 配置超时时间- Parameters:
duration- the timeout duration | 超时时间
-
getMaxVertices
public static int getMaxVertices()Get current maximum vertex count 获取当前最大顶点数量- Returns:
- maximum vertex count | 最大顶点数量
-
getMaxEdges
public static int getMaxEdges()Get current maximum edge count 获取当前最大边数量- Returns:
- maximum edge count | 最大边数量
-
getMaxDepth
public static int getMaxDepth()Get current maximum depth 获取当前最大深度- Returns:
- maximum depth | 最大深度
-
getTimeout
-
safeAddVertex
Safely add a vertex with limit check 安全添加顶点(带限制检查)- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图vertex- the vertex to add | 要添加的顶点- Throws:
GraphLimitExceededException- if vertex limit exceeded | 如果超出顶点限制则抛出异常
-
safeAddEdge
Safely add an edge with limit check 安全添加边(带限制检查)- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图from- source vertex | 源顶点to- target vertex | 目标顶点- Throws:
GraphLimitExceededException- if edge limit exceeded | 如果超出边限制则抛出异常
-
safeAddEdge
Safely add an edge with weight and limit check 安全添加带权重的边(带限制检查)- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图from- source vertex | 源顶点to- target vertex | 目标顶点weight- edge weight | 边权重- Throws:
GraphLimitExceededException- if edge limit exceeded | 如果超出边限制则抛出异常InvalidEdgeException- if weight is invalid | 如果权重无效则抛出异常
-
safeShortestPath
Safely compute shortest path with timeout 安全计算最短路径(带超时)- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图source- the source vertex | 源顶点target- the target vertex | 目标顶点- Returns:
- list of vertices in the shortest path | 最短路径的顶点列表
- Throws:
GraphTimeoutException- if computation times out | 如果计算超时则抛出异常
-
safeDijkstra
Safely compute Dijkstra distances with timeout 安全计算Dijkstra距离(带超时)- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图source- the source vertex | 源顶点- Returns:
- map of vertex to shortest distance | 顶点到最短距离的映射
- Throws:
GraphTimeoutException- if computation times out | 如果计算超时则抛出异常
-
executeWithTimeout
Execute a callable with timeout 带超时执行可调用任务- Type Parameters:
T- the result type | 结果类型- Parameters:
callable- the callable to execute | 要执行的可调用任务operationName- the operation name for error messages | 用于错误消息的操作名称- Returns:
- the result | 结果
- Throws:
GraphTimeoutException- if execution times out | 如果执行超时则抛出异常
-
wouldExceedVertexLimit
Check if adding vertex would exceed limit 检查添加顶点是否会超出限制- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- true if adding would exceed limit | 如果添加会超出限制返回true
-
wouldExceedEdgeLimit
Check if adding edge would exceed limit 检查添加边是否会超出限制- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- true if adding would exceed limit | 如果添加会超出限制返回true
-
resetToDefaults
public static void resetToDefaults()Reset all limits to defaults 重置所有限制为默认值
-