Skip to content

Commit

Permalink
chore: Drop scala java8 compat (#4418)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-alfers authored Sep 9, 2024
1 parent 3af8378 commit a38c01e
Show file tree
Hide file tree
Showing 50 changed files with 308 additions and 289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import akka.http.caching.scaladsl.Cache
import akka.http.impl.util.JavaMapping.Implicits._
import akka.http.caching.CacheJavaMapping.Implicits._

import scala.compat.java8.FutureConverters._
import scala.compat.java8.FunctionConverters._
import scala.jdk.FunctionConverters._
import scala.jdk.FutureConverters._

@ApiMayChange
object LfuCache {
Expand Down Expand Up @@ -91,19 +91,19 @@ object LfuCache {
}

def toJavaMappingFunction[K, V](genValue: () => Future[V]): BiFunction[K, Executor, CompletableFuture[V]] =
asJavaBiFunction[K, Executor, CompletableFuture[V]]((k, e) => genValue().toJava.toCompletableFuture)
((k: K, e: Executor) => genValue().asJava.toCompletableFuture).asJavaBiFunction

def toJavaMappingFunction[K, V](loadValue: K => Future[V]): BiFunction[K, Executor, CompletableFuture[V]] =
asJavaBiFunction[K, Executor, CompletableFuture[V]]((k, e) => loadValue(k).toJava.toCompletableFuture)
((k: K, e: Executor) => loadValue(k).asJava.toCompletableFuture).asJavaBiFunction
}

/** INTERNAL API */
@InternalApi
private[caching] class LfuCache[K, V](val store: AsyncCache[K, V]) extends Cache[K, V] {

def get(key: K): Option[Future[V]] = Option(store.getIfPresent(key)).map(_.toScala)
def get(key: K): Option[Future[V]] = Option(store.getIfPresent(key)).map(_.asScala)

def apply(key: K, genValue: () => Future[V]): Future[V] = store.get(key, toJavaMappingFunction[K, V](genValue)).toScala
def apply(key: K, genValue: () => Future[V]): Future[V] = store.get(key, toJavaMappingFunction[K, V](genValue)).asScala

/**
* Multiple call to put method for the same key may result in a race condition,
Expand All @@ -114,16 +114,16 @@ private[caching] class LfuCache[K, V](val store: AsyncCache[K, V]) extends Cache

previouslyCacheValue match {
case None =>
store.put(key, toJava(mayBeValue).toCompletableFuture)
store.put(key, mayBeValue.asJava.toCompletableFuture)
mayBeValue
case _ => mayBeValue.map { value =>
store.put(key, toJava(Future.successful(value)).toCompletableFuture)
store.put(key, Future.successful(value).asJava.toCompletableFuture)
value
}
}
}

def getOrLoad(key: K, loadValue: K => Future[V]): Future[V] = store.get(key, toJavaMappingFunction[K, V](loadValue)).toScala
def getOrLoad(key: K, loadValue: K => Future[V]): Future[V] = store.get(key, toJavaMappingFunction[K, V](loadValue)).asScala

def remove(key: K): Unit = store.synchronous().invalidate(key)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import akka.japi.{ Creator, Procedure }

import scala.collection.JavaConverters._
import scala.collection.immutable
import scala.compat.java8.FutureConverters.{ toJava => futureToJava, toScala => futureToScala }
import scala.concurrent.{ ExecutionContext, Future, Promise }
import scala.jdk.FutureConverters._

/**
* API MAY CHANGE
Expand Down Expand Up @@ -56,7 +56,7 @@ abstract class Cache[K, V] extends akka.http.caching.javadsl.Cache[K, V] {
*/
def get(key: K): Option[Future[V]]
override def getOptional(key: K): Optional[CompletionStage[V]] =
Optional.ofNullable(get(key).map(f => futureToJava(f)).orNull)
Optional.ofNullable(get(key).map(f => f.asJava).orNull)

/**
* Cache the given future if not cached previously.
Expand Down Expand Up @@ -86,20 +86,20 @@ abstract class Cache[K, V] extends akka.http.caching.javadsl.Cache[K, V] {
override def getKeys: java.util.Set[K] = keys.asJava

final override def getFuture(key: K, genValue: Creator[CompletionStage[V]]): CompletionStage[V] =
futureToJava(apply(key, () => futureToScala(genValue.create())))
apply(key, () => genValue.create().asScala).asJava

final override def getOrFulfil(key: K, f: Procedure[CompletableFuture[V]]): CompletionStage[V] =
futureToJava(apply(key, promise => {
apply(key, promise => {
val completableFuture = new CompletableFuture[V]
f(completableFuture)
promise.completeWith(futureToScala(completableFuture))
}))
promise.completeWith(completableFuture.asScala)
}).asJava

/**
* Returns either the cached CompletionStage for the given key or the given value as a CompletionStage
*/
override def getOrCreateStrict(key: K, block: Creator[V]): CompletionStage[V] =
futureToJava(get(key, () => block.create()))
get(key, () => block.create()).asJava

/**
* Returns the upper bound for the number of currently cached entries.
Expand Down
3 changes: 2 additions & 1 deletion akka-http-core/src/main/java/akka/http/impl/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

package akka.http.impl.util;

import scala.compat.java8.OptionConverters;
import scala.None$;
import scala.collection.immutable.Map$;
import scala.collection.immutable.Seq;
import scala.jdk.javaapi.OptionConverters;

import akka.stream.scaladsl.Source;
import akka.http.ccompat.MapHelpers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import java.util.Optional;
import java.util.OptionalLong;
import scala.compat.java8.OptionConverters;

import scala.Option;

public abstract class ContentRange {
public abstract boolean isByteContentRange();
Expand All @@ -30,7 +31,7 @@ public static ContentRange create(long first, long last, long instanceLength) {
}
@SuppressWarnings("unchecked")
public static ContentRange create(long first, long last, OptionalLong instanceLength) {
return ContentRange$.MODULE$.apply(first, last, OptionConverters.toScala(instanceLength));
return ContentRange$.MODULE$.apply(first, last, (instanceLength.isPresent() ? Option.apply(instanceLength.getAsLong()) : Option.empty()));
}
public static ContentRange createUnsatisfiable(long length) {
return new akka.http.scaladsl.model.ContentRange.Unsatisfiable(length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import java.util.Optional;

import akka.http.javadsl.model.headers.HttpEncodingRanges;
import scala.compat.java8.OptionConverters;

import scala.jdk.javaapi.OptionConverters;

public abstract class RemoteAddress {
public abstract boolean isUnknown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

package akka.http.javadsl.model.headers;

import scala.compat.java8.OptionConverters;
import scala.Some;
import scala.jdk.javaapi.OptionConverters;

import java.util.Optional;
import java.util.OptionalLong;
Expand All @@ -19,7 +20,7 @@ public static CacheDirective MAX_STALE() {
return new akka.http.scaladsl.model.headers.CacheDirectives.max$minusstale(OptionConverters.toScala(Optional.empty()));
}
public static CacheDirective MAX_STALE(long deltaSeconds) {
return new akka.http.scaladsl.model.headers.CacheDirectives.max$minusstale(OptionConverters.toScala(OptionalLong.of(deltaSeconds)));
return new akka.http.scaladsl.model.headers.CacheDirectives.max$minusstale(new Some<>(deltaSeconds));
}
public static CacheDirective MIN_FRESH(long deltaSeconds) {
return new akka.http.scaladsl.model.headers.CacheDirectives.min$minusfresh(deltaSeconds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import akka.annotation.DoNotInherit;
import akka.http.javadsl.model.DateTime;
import akka.http.impl.util.Util;
import scala.compat.java8.OptionConverters;

import scala.jdk.javaapi.OptionConverters;

import java.util.Optional;
import java.util.OptionalLong;
Expand Down Expand Up @@ -62,7 +63,7 @@ public static HttpCookie create(
return new akka.http.scaladsl.model.headers.HttpCookie(
name, value,
Util.<DateTime, akka.http.scaladsl.model.DateTime>convertOptionalToScala(expires),
OptionConverters.toScala(maxAge),
OptionConverters.toScala(maxAge).map(age -> (Object)age),
OptionConverters.toScala(domain),
OptionConverters.toScala(path),
secure,
Expand All @@ -85,7 +86,7 @@ public static HttpCookie create(
return new akka.http.scaladsl.model.headers.HttpCookie(
name, value,
Util.<DateTime, akka.http.scaladsl.model.DateTime>convertOptionalToScala(expires),
OptionConverters.toScala(maxAge),
OptionConverters.toScala(maxAge).map(age -> (Object)age),
OptionConverters.toScala(domain),
OptionConverters.toScala(path),
secure,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@

import java.util.Optional;
import java.util.OptionalInt;

import scala.jdk.javaapi.OptionConverters;
import scala.Option;
import static scala.compat.java8.OptionConverters.toScala;

/**
* Representation of a server-sent event. According to the specification, an empty data field
* designates an event which is to be ignored which is useful for heartbeats.
*/
public abstract class ServerSentEvent {

private static final Option<String> stringNone = toScala(Optional.empty());
private static final Option<String> stringNone = Option.empty();

private static final Option<Object> intNone = toScala(OptionalInt.empty());
private static final Option<Object> intNone = Option.empty();


/**
Expand Down Expand Up @@ -92,7 +93,7 @@ public static ServerSentEvent create(String data,
Optional<String> id,
OptionalInt retry) {
return akka.http.scaladsl.model.sse.ServerSentEvent.apply(
data, toScala(type), toScala(id), toScala(retry)
data, OptionConverters.toScala(type), OptionConverters.toScala(id), OptionConverters.toScala(retry).map(retryInt -> (Object) retryInt)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ private[akka] object OutgoingConnectionBuilderImpl {
new JavaAdapter(actual.logTo(logger).asInstanceOf[Impl])

private def javaFlow(flow: Flow[HttpRequest, HttpResponse, Future[OutgoingConnection]]): JFlow[javadsl.model.HttpRequest, javadsl.model.HttpResponse, CompletionStage[javadsl.OutgoingConnection]] = {
import scala.compat.java8.FutureConverters.toJava
javaFlowKeepMatVal(flow.mapMaterializedValue(f => toJava(f.map(oc => new javadsl.OutgoingConnection(oc))(ExecutionContexts.parasitic))))
import scala.jdk.FutureConverters._
javaFlowKeepMatVal(flow.mapMaterializedValue(f => f.map(oc => new javadsl.OutgoingConnection(oc))(ExecutionContexts.parasitic).asJava))
}

private def javaFlowKeepMatVal[M](flow: Flow[HttpRequest, HttpResponse, M]): JFlow[javadsl.model.HttpRequest, javadsl.model.HttpResponse, M] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import akka.japi.Pair
import akka.stream.{ FlowShape, Graph, javadsl, scaladsl }

import scala.collection.immutable
import scala.compat.java8.{ FutureConverters, OptionConverters }
import scala.jdk.FutureConverters._
import scala.jdk.OptionConverters._
import scala.reflect.ClassTag
import akka.NotUsed
import akka.annotation.InternalApi
Expand Down Expand Up @@ -115,8 +116,8 @@ private[http] object JavaMapping {
}
implicit def option[_J, _S](implicit mapping: JavaMapping[_J, _S]): JavaMapping[Optional[_J], Option[_S]] =
new JavaMapping[Optional[_J], Option[_S]] {
def toScala(javaObject: Optional[_J]): Option[_S] = OptionConverters.toScala(javaObject).map(mapping.toScala)
def toJava(scalaObject: Option[_S]): Optional[_J] = OptionConverters.toJava(scalaObject.map(mapping.toJava))
def toScala(javaObject: Optional[_J]): Option[_S] = javaObject.toScala.map(mapping.toScala)
def toJava(scalaObject: Option[_S]): Optional[_J] = scalaObject.map(mapping.toJava).toJava
}

implicit def flowMapping[JIn, SIn, JOut, SOut, JM, SM](implicit inMapping: JavaMapping[JIn, SIn], outMapping: JavaMapping[JOut, SOut], matValueMapping: JavaMapping[JM, SM]): JavaMapping[javadsl.Flow[JIn, JOut, JM], scaladsl.Flow[SIn, SOut, SM]] =
Expand Down Expand Up @@ -161,8 +162,8 @@ private[http] object JavaMapping {

implicit def futureMapping[_J, _S](implicit mapping: JavaMapping[_J, _S], ec: ExecutionContext): JavaMapping[CompletionStage[_J], Future[_S]] =
new JavaMapping[CompletionStage[_J], Future[_S]] {
def toJava(scalaObject: Future[_S]): CompletionStage[_J] = FutureConverters.toJava(scalaObject.map(mapping.toJava))
def toScala(javaObject: CompletionStage[_J]): Future[_S] = FutureConverters.toScala(javaObject).map(mapping.toScala)
def toJava(scalaObject: Future[_S]): CompletionStage[_J] = scalaObject.map(mapping.toJava).asJava
def toScala(javaObject: CompletionStage[_J]): Future[_S] = javaObject.asScala.map(mapping.toScala)
}

implicit object StringIdentity extends Identity[String]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ object ClientTransport {
* to an [[InetSocketAddress]]
*/
def withCustomResolver(lookup: BiFunction[String, Int, CompletionStage[InetSocketAddress]]): ClientTransport = {
import scala.compat.java8.FutureConverters._
scaladsl.ClientTransport.withCustomResolver((host, port) => lookup.apply(host, port).toScala).asJava
import scala.jdk.FutureConverters._
scaladsl.ClientTransport.withCustomResolver((host, port) => lookup.apply(host, port).asScala).asJava
}

def fromScala(scalaTransport: scaladsl.ClientTransport): ClientTransport =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package akka.http.javadsl
import java.util.Locale
import java.util.Optional

import scala.compat.java8.OptionConverters._
import scala.jdk.OptionConverters._

import akka.annotation.{ DoNotInherit, InternalApi }
import akka.http.javadsl.model.Uri
Expand All @@ -21,11 +21,11 @@ abstract class ConnectHttp {
def connectionContext: Optional[HttpsConnectionContext]

final def effectiveHttpsConnectionContext(fallbackContext: HttpsConnectionContext): HttpsConnectionContext =
connectionContext.asScala
connectionContext.toScala
.getOrElse(fallbackContext)

final def effectiveConnectionContext(fallbackContext: ConnectionContext): ConnectionContext =
connectionContext.asScala // Optional doesn't deal well with covariance
connectionContext.toScala // Optional doesn't deal well with covariance
.getOrElse(fallbackContext)

override def toString = s"ConnectHttp($host,$port,$isHttps,$connectionContext)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@

package akka.http.javadsl

import java.util.{ Optional, Collection => JCollection }
import akka.annotation.{ ApiMayChange, DoNotInherit }
import akka.http.scaladsl
import akka.japi.Util
import akka.stream.TLSClientAuth

import javax.net.ssl.{ SSLContext, SSLEngine }
import scala.compat.java8.OptionConverters

object ConnectionContext {
//#https-server-context-creation
Expand Down
Loading

0 comments on commit a38c01e

Please sign in to comment.