diff --git a/akka-actor-tests/src/test/java/akka/actor/AddressTest.java b/akka-actor-tests/src/test/java/akka/actor/AddressTest.java new file mode 100644 index 0000000000..d5410b4cef --- /dev/null +++ b/akka-actor-tests/src/test/java/akka/actor/AddressTest.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2020 Lightbend Inc. + */ + +package akka.actor; +/* + * Copyright (C) 2009-2020 Lightbend Inc. + */ + +import org.junit.Test; +import org.scalatest.junit.JUnitSuite; + +import java.util.Optional; + +import static org.junit.Assert.assertEquals; + +public class AddressTest extends JUnitSuite { + + @Test + public void portAddressAccessible() { + Address address = new Address("akka", "MySystem", "localhost", 2525); + assertEquals(Optional.of(2525), address.getPort()); + assertEquals(Optional.of("localhost"), address.getHost()); + } +} diff --git a/akka-actor/src/main/scala/akka/actor/Address.scala b/akka-actor/src/main/scala/akka/actor/Address.scala index 6482e26bef..6b57f7c4be 100644 --- a/akka-actor/src/main/scala/akka/actor/Address.scala +++ b/akka-actor/src/main/scala/akka/actor/Address.scala @@ -6,11 +6,12 @@ package akka.actor import java.net.URI import java.net.URISyntaxException import java.net.MalformedURLException +import java.util.Optional import scala.annotation.tailrec import scala.collection.immutable - import akka.annotation.InternalApi +import scala.compat.java8.OptionConverters._ /** * The address specifies the physical location under which an Actor can be @@ -31,6 +32,16 @@ final case class Address private (protocol: String, system: String, host: Option def this(protocol: String, system: String) = this(protocol, system, None, None) def this(protocol: String, system: String, host: String, port: Int) = this(protocol, system, Option(host), Some(port)) + /** + * Java API: The hostname if specified or empty optional if not + */ + def getHost(): Optional[String] = host.asJava + + /** + * Java API: The port if specified or empty optional if not + */ + def getPort(): Optional[Integer] = port.asJava.asInstanceOf[Optional[Integer]] + /** * Returns true if this Address is only defined locally. It is not safe to send locally scoped addresses to remote * hosts. See also [[akka.actor.Address#hasGlobalScope]].