Class SafeGraphOperations

java.lang.Object
cloud.opencode.base.graph.security.SafeGraphOperations

public final class SafeGraphOperations extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default maximum traversal depth 默认最大遍历深度
    static final int
    Default maximum edge count 默认最大边数量
    static final int
    Default maximum vertex count 默认最大顶点数量
    static final Duration
    Default timeout duration 默认超时时间
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    executeWithTimeout(Callable<T> callable, String operationName)
    Execute a callable with timeout 带超时执行可调用任务
    static int
    Get current maximum depth 获取当前最大深度
    static int
    Get current maximum edge count 获取当前最大边数量
    static int
    Get current maximum vertex count 获取当前最大顶点数量
    static Duration
    Get current timeout 获取当前超时时间
    static void
    Reset all limits to defaults 重置所有限制为默认值
    static <V> void
    safeAddEdge(Graph<V> graph, V from, V to)
    Safely add an edge with limit check 安全添加边(带限制检查)
    static <V> void
    safeAddEdge(Graph<V> graph, V from, V to, double weight)
    Safely add an edge with weight and limit check 安全添加带权重的边(带限制检查)
    static <V> void
    safeAddVertex(Graph<V> graph, V vertex)
    Safely add a vertex with limit check 安全添加顶点(带限制检查)
    static <V> Map<V,Double>
    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 void
    setMaxDepth(int max)
    Configure maximum traversal depth 配置最大遍历深度
    static void
    setMaxEdges(int max)
    Configure maximum edge count 配置最大边数量
    static void
    setMaxVertices(int max)
    Configure maximum vertex count 配置最大顶点数量
    static void
    setTimeout(Duration duration)
    Configure timeout duration 配置超时时间
    static <V> boolean
    Check if adding edge would exceed limit 检查添加边是否会超出限制
    static <V> boolean
    Check if adding vertex would exceed limit 检查添加顶点是否会超出限制

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_MAX_VERTICES

      public static final int DEFAULT_MAX_VERTICES
      Default maximum vertex count 默认最大顶点数量
      See Also:
    • DEFAULT_MAX_EDGES

      public static final int DEFAULT_MAX_EDGES
      Default maximum edge count 默认最大边数量
      See Also:
    • DEFAULT_MAX_DEPTH

      public static final int DEFAULT_MAX_DEPTH
      Default maximum traversal depth 默认最大遍历深度
      See Also:
    • DEFAULT_TIMEOUT

      public static final Duration 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

      public static void setTimeout(Duration duration)
      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

      public static Duration getTimeout()
      Get current timeout 获取当前超时时间
      Returns:
      timeout duration | 超时时间
    • safeAddVertex

      public static <V> void safeAddVertex(Graph<V> graph, V vertex)
      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

      public static <V> void safeAddEdge(Graph<V> graph, V from, V to)
      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

      public static <V> void safeAddEdge(Graph<V> graph, V from, V to, double weight)
      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

      public static <V> List<V> safeShortestPath(Graph<V> graph, V source, V target)
      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

      public static <V> Map<V,Double> safeDijkstra(Graph<V> graph, V source)
      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

      public static <T> T executeWithTimeout(Callable<T> callable, String operationName)
      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

      public static <V> boolean wouldExceedVertexLimit(Graph<V> graph)
      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

      public static <V> boolean wouldExceedEdgeLimit(Graph<V> graph)
      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 重置所有限制为默认值