
com.twitter.util.javainterop.Scala Maven / Gradle / Ivy
package com.twitter.util.javainterop;
import scala.Option;
import scala.Tuple2;
import scala.collection.JavaConverters;
/**
* A collection of conversions from Java collections to their immutable
* Scala variants.
*
* See scala.collection.JavaConverters if you do not need immutable
* collections.
*/
public final class Scala {
private Scala() { throw new IllegalStateException(); }
/**
* Converts a {@link java.util.List} to an immutable Scala Seq.
*
* See scala.collection.JavaConverters.asScalaBuffer if you do
* not need the returned Seq to be immutable.
*
* @return an empty Seq if the input is null.
*/
@SuppressWarnings("unchecked")
public static scala.collection.immutable.Seq asImmutableSeq(
java.util.List jList
) {
if (jList == null) {
return scala.collection.immutable.Seq$.MODULE$.empty();
} else {
return JavaConverters.asScalaBuffer(jList).toList();
}
}
/**
* Converts a {@link java.util.Set} to an immutable Scala Set.
*
* See scala.collection.JavaConverters.asScalaSet if you do
* not need the returned Set to be immutable.
*
* @return an empty Set if the input is null.
*/
@SuppressWarnings("unchecked")
public static scala.collection.immutable.Set asImmutableSet(
java.util.Set jSet
) {
if (jSet == null) {
return scala.collection.immutable.Set$.MODULE$.empty();
} else {
return JavaConverters.asScalaSet(jSet).toSet();
}
}
/**
* Converts a {@link java.util.Map} to an immutable Scala Map.
*
* See scala.collection.JavaConverters.asScalaMap if you do
* not need the returned Map to be immutable.
*
* @return an empty Map if the input is null.
*/
public static scala.collection.immutable.Map asImmutableMap(
java.util.Map jMap
) {
scala.collection.immutable.Map sMap = scala.collection.immutable.Map$.MODULE$.empty();
if (jMap != null) {
for (java.util.Map.Entry entry : jMap.entrySet()) {
sMap = sMap.$plus(new Tuple2(entry.getKey(), entry.getValue()));
}
}
return sMap;
}
/**
* Converts a {@link java.util.Optional} to a Scala Option.
*/
public static Option asOption(
java.util.Optional optional
) {
if (optional.isPresent()) return Option.apply(optional.get());
else return Option.empty();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy