From 6882315c50f56f5abba27af9063ad5061c53b3c2 Mon Sep 17 00:00:00 2001 From: Brian Frank Date: Fri, 6 Sep 2024 08:23:45 -0400 Subject: [PATCH] concurrent: add ActorMsg class --- src/concurrent/fan/ActorMsg.fan | 140 ++++++++++++++++++++++++++ src/doc/docIntro/doc/ChangeLog.fandoc | 3 +- 2 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 src/concurrent/fan/ActorMsg.fan diff --git a/src/concurrent/fan/ActorMsg.fan b/src/concurrent/fan/ActorMsg.fan new file mode 100644 index 000000000..395efac8f --- /dev/null +++ b/src/concurrent/fan/ActorMsg.fan @@ -0,0 +1,140 @@ +// +// Copyright (c) 2024, Brian Frank and Andy Frank +// Licensed under the Academic Free License version 3.0 +// +// History: +// 6 Sep 24 Brian Frank Copy from HxMsg +// + +** +** ActorMsg provides simple immutable tuple to use for actor messages. +** +const class ActorMsg +{ + ** Constructor with zero arguments + new make0(Str id) + { + this.id = id + } + + ** Constructor with one argument + new make1(Str id, Obj? a) + { + this.id = id + this.a = a + } + + ** Constructor with two arguments + new make2(Str id, Obj? a, Obj? b) + { + this.id = id + this.a = a + this.b = b + } + + ** Constructor with three arguments + new make3(Str id, Obj? a, Obj? b, Obj? c) + { + this.id = id + this.a = a + this.b = b + this.c = c + } + + ** Constructor with four arguments + new make4(Str id, Obj? a, Obj? b, Obj? c, Obj? d) + { + this.id = id + this.a = a + this.b = b + this.c = c + this.d = d + } + + ** Constructor with five arguments + new make5(Str id, Obj? a, Obj? b, Obj? c, Obj? d, Obj? e) + { + this.id = id + this.a = a + this.b = b + this.c = c + this.d = d + this.e = e + } + + ** Message identifier key + const Str id + + ** Argument a + const Obj? a + + ** Argument b + const Obj? b + + ** Argument c + const Obj? c + + ** Argument d + const Obj? d + + ** Argument e + const Obj? e + + ** Hash is based on id and arguments + override Int hash() + { + hash := id.hash + if (a != null) hash = hash.xor(a.hash) + if (b != null) hash = hash.xor(b.hash) + if (c != null) hash = hash.xor(c.hash) + if (d != null) hash = hash.xor(d.hash) + if (e != null) hash = hash.xor(e.hash) + return hash + } + + ** Equality is based on id and arguments + override Bool equals(Obj? that) + { + m := that as ActorMsg + if (m == null) return false + return id == m.id && + a == m.a && + b == m.b && + c == m.c && + d == m.d && + e == m.e + } + + ** Return debug string representation + override Str toStr() + { + toDebugStr("ActorMsg", id, a, b, c, d, e) + } + + ** Format actor msg tuple as "type(id, a=a, b=b, ...)" + @NoDoc static Str toDebugStr(Str type, Obj? id, Obj? a, Obj? b := null, Obj? c := null, Obj? d := null, Obj? e := null) + { + s := StrBuf() + s.add(type).add("(").add(id) + toDebugArg(s, "a", a) + toDebugArg(s, "b", b) + toDebugArg(s, "c", c) + toDebugArg(s, "d", d) + toDebugArg(s, "e", e) + return s.add(")").toStr + } + + private static Void toDebugArg(StrBuf b, Str name, Obj? arg) + { + if (arg == null) return + b.addChar(' ').add(name).addChar('=') + try + { + s := arg.toStr + if (s.size <= 64) b.add(s) + else b.add(s[0..64]).add("...") + } + catch (Err e) b.add(e.toStr) + } +} + diff --git a/src/doc/docIntro/doc/ChangeLog.fandoc b/src/doc/docIntro/doc/ChangeLog.fandoc index ca765adfb..c9696f5ec 100644 --- a/src/doc/docIntro/doc/ChangeLog.fandoc +++ b/src/doc/docIntro/doc/ChangeLog.fandoc @@ -17,7 +17,8 @@ - Add double checked locking into ClassType.java reflect and finish - Add sql escape syntax for "@" and "\" - Deprecate old, undocumented sql escape syntax (This is a breaking change) -- add sql::Statement.executeBatch method +- Add sql::Statement.executeBatch method +- Add concurrent::ActorMsg *Build 1.0.80 (23 Apr 2024)* - New ECMA class-based JavaScript design