This is an automated email from the ASF dual-hosted git repository.

wenjin272 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/flink-agents.git


The following commit(s) were added to refs/heads/main by this push:
     new cc238a1d [api][java] Include the memory type in MemoryRef equality and 
reject null fields (#1153)
cc238a1d is described below

commit cc238a1d36db9eb80fa54d07dd088de82eb840ea
Author: Sangkyoon Nam <[email protected]>
AuthorDate: Mon Sep 28 15:30:46 2026 +0900

    [api][java] Include the memory type in MemoryRef equality and reject null 
fields (#1153)
    
    Generated-by: Claude Code 2.1.280 (Claude Fable 5.1)
---
 .../apache/flink/agents/api/context/MemoryRef.java | 20 ++++++++++----
 .../flink/agents/runtime/memory/MemoryRefTest.java | 32 ++++++++++++++++++++++
 2 files changed, 47 insertions(+), 5 deletions(-)

diff --git 
a/api/src/main/java/org/apache/flink/agents/api/context/MemoryRef.java 
b/api/src/main/java/org/apache/flink/agents/api/context/MemoryRef.java
index 062865f3..61498545 100644
--- a/api/src/main/java/org/apache/flink/agents/api/context/MemoryRef.java
+++ b/api/src/main/java/org/apache/flink/agents/api/context/MemoryRef.java
@@ -58,12 +58,20 @@ public final class MemoryRef implements Serializable {
     }
 
     /**
-     * Creates a new MemoryRef instance with the given path.
+     * Creates a reference to the slot at {@code path} in the memory of the 
given type.
      *
-     * @param path The absolute path of the data in memory.
+     * @param type The memory the referenced data lives in.
+     * @param path The absolute path of the data in that memory.
      * @return A new MemoryRef instance.
+     * @throws IllegalArgumentException if {@code type} or {@code path} is null
      */
     public static MemoryRef create(MemoryObject.MemoryType type, String path) {
+        if (type == null) {
+            throw new IllegalArgumentException("MemoryRef requires a memory 
type.");
+        }
+        if (path == null) {
+            throw new IllegalArgumentException("MemoryRef requires a path.");
+        }
         return new MemoryRef(type, path);
     }
 
@@ -142,16 +150,18 @@ public final class MemoryRef implements Serializable {
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;
         MemoryRef memoryRef = (MemoryRef) o;
-        return path.equals(memoryRef.path);
+        // A reference names a slot in one memory: the same path in sensory 
and short-term memory
+        // is two different slots.
+        return type == memoryRef.type && path.equals(memoryRef.path);
     }
 
     @Override
     public int hashCode() {
-        return Objects.hash(path);
+        return Objects.hash(type, path);
     }
 
     @Override
     public String toString() {
-        return "MemoryRef{" + "path='" + path + '\'' + '}';
+        return "MemoryRef{type=" + type + ", path='" + path + '\'' + '}';
     }
 }
diff --git 
a/runtime/src/test/java/org/apache/flink/agents/runtime/memory/MemoryRefTest.java
 
b/runtime/src/test/java/org/apache/flink/agents/runtime/memory/MemoryRefTest.java
index e9eaadea..c062e873 100644
--- 
a/runtime/src/test/java/org/apache/flink/agents/runtime/memory/MemoryRefTest.java
+++ 
b/runtime/src/test/java/org/apache/flink/agents/runtime/memory/MemoryRefTest.java
@@ -292,4 +292,36 @@ public class MemoryRefTest {
         assertTrue(refSet.contains(ref1));
         assertTrue(refSet.contains(ref3));
     }
+
+    /**
+     * A reference names a slot in one memory: the same path in sensory and 
short-term memory is two
+     * different slots, so the references must not compare equal.
+     */
+    @Test
+    void testRefEqualityDistinguishesMemoryType() {
+        MemoryRef sensory = MemoryRef.create(MemoryObject.MemoryType.SENSORY, 
"a.b");
+        MemoryRef shortTerm = 
MemoryRef.create(MemoryObject.MemoryType.SHORT_TERM, "a.b");
+
+        assertNotEquals(sensory, shortTerm);
+
+        Set<MemoryRef> refSet = new HashSet<>(Arrays.asList(sensory, 
shortTerm));
+        assertEquals(2, refSet.size());
+        assertTrue(refSet.contains(sensory));
+        assertTrue(refSet.contains(shortTerm));
+    }
+
+    @Test
+    void testToStringNamesMemoryType() {
+        MemoryRef ref = MemoryRef.create(MemoryObject.MemoryType.SENSORY, 
"a.b");
+        assertTrue(ref.toString().contains("SENSORY"), ref.toString());
+        assertTrue(ref.toString().contains("a.b"), ref.toString());
+    }
+
+    @Test
+    void testCreateRejectsNullTypeOrPath() {
+        assertThrows(IllegalArgumentException.class, () -> 
MemoryRef.create(null, "a.b"));
+        assertThrows(
+                IllegalArgumentException.class,
+                () -> MemoryRef.create(MemoryObject.MemoryType.SENSORY, null));
+    }
 }

Reply via email to