Record Class Edge<V>

java.lang.Object
java.lang.Record
cloud.opencode.base.graph.node.Edge<V>
Type Parameters:
V - the vertex type | 顶点类型
Record Components:
from - the source vertex | 源顶点
to - the target vertex | 目标顶点
weight - the edge weight | 边权重

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Yes (rejects null vertices, NaN and infinite weights) - 空值安全: 是(拒绝null顶点、NaN和无穷权重)

public record Edge<V>(V from, V to, double weight) extends Record
Edge 边

Immutable record representing a weighted edge in a graph.

表示图中加权边的不可变记录。

Features | 特性:

  • Immutable - 不可变
  • Supports weighted edges - 支持加权边
  • Default weight is 1.0 - 默认权重为1.0

Usage Examples | 使用示例:

// Unweighted edge
Edge<String> edge1 = new Edge<>("A", "B");

// Weighted edge
Edge<String> edge2 = new Edge<>("A", "B", 5.0);

// Access properties
String from = edge2.from();
String to = edge2.to();
double weight = edge2.weight();
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 double
    Default edge weight 默认边权重
  • Constructor Summary

    Constructors
    Constructor
    Description
    Edge(V from, V to)
    Create an unweighted edge (weight = 1.0) 创建无权重边(权重=1.0)
    Edge(V from, V to, double weight)
    Create an edge with validation 创建带验证的边
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    Returns the value of the from record component.
    final int
    Returns a hash code value for this object.
    boolean
    Check if this is a self-loop 检查是否为自环
    Create a reversed edge 创建反向边
    to()
    Returns the value of the to record component.
    Returns a string representation of this record class.
    double
    Returns the value of the weight record component.
    withWeight(double newWeight)
    Create an edge with a new weight 创建具有新权重的边

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • DEFAULT_WEIGHT

      public static final double DEFAULT_WEIGHT
      Default edge weight 默认边权重
      See Also:
  • Constructor Details

    • Edge

      public Edge(V from, V to)
      Create an unweighted edge (weight = 1.0) 创建无权重边(权重=1.0)
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
    • Edge

      public Edge(V from, V to, double weight)
      Create an edge with validation 创建带验证的边
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
      weight - the edge weight | 边权重
  • Method Details

    • reversed

      public Edge<V> reversed()
      Create a reversed edge 创建反向边
      Returns:
      the reversed edge | 反向边
    • withWeight

      public Edge<V> withWeight(double newWeight)
      Create an edge with a new weight 创建具有新权重的边
      Parameters:
      newWeight - the new weight | 新权重
      Returns:
      the new edge | 新边
    • isSelfLoop

      public boolean isSelfLoop()
      Check if this is a self-loop 检查是否为自环
      Returns:
      true if self-loop | 如果是自环返回true
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • from

      public V from()
      Returns the value of the from record component.
      Returns:
      the value of the from record component
    • to

      public V to()
      Returns the value of the to record component.
      Returns:
      the value of the to record component
    • weight

      public double weight()
      Returns the value of the weight record component.
      Returns:
      the value of the weight record component