I'm using the following interface from a Java library:
// This is a Java class from a library I'm using
public interface Listener {
void receiveConfigInfo(final String configInfo);
}
And I'm extending it as follows, in Scala:
//MyClass needs to extends the Listener interface
final class MyClass extends Listener {
private var config = Option.empty
// Implement the the Listener interface
override def receiveConfigInfo(configInfo: String): Unit = {
try {
config = decode[Map[String, String]](configInfo) match {
case Right(config) => Some(config)
case Left(_) => None
}
} catch {
case _: Throwable => nacosConfig = None
}
}
override def getConfig():Option[Map[String, String]] = nacosConfig
}
receiveConfigInfo
will be called in automatically whenever relevant.
getConfig
returns the latest value of configuration.
Is there a way to make config
into a val
, rather than a mutable var
? I cannot change the signature of receiveConfigInfo
, as it needs to respect the signature of parent class.
The objective is whenever I call getConfig
, I should get latest config value. However my current implementation has a var
which is not good, is there any way to change this code to make it val or another way if possible?