Skip to content

jozic/scalaj

Repository files navigation

ScalaJ Converters Build Status Coverage Status

When JavaConverters is not enough...

If you work on a Java/Scala mixed project you can find yourself converting java collections and/or primitive wrappers to/from corresponding scala classes or vice versa. JavaConverters is your friend here, but it's not always good enough.

If you are tired of doing something like this

import scala.jdk.CollectionConverters._

def iTakeInt(i: Int) = { ... }

val something = someJavaListOfJavaIntegers.asScala.map(iTakeInt(_))

or this

import scala.jdk.CollectionConverters._

val something: mutable.Map[java.lang.Long, Buffer] = 
  someJavaMapOfJavaLongsToJavaLists.asScala.mapValues(_.asScala)

look no more!
Now you can do

import com.daodecode.scalaj.collection._

def iTakeInt(i: Int) = { ... }

val something = someJavaListOfJavaIntegers.deepAsScala.map(iTakeInt)

and

import com.daodecode.scalaj.collection._

val something: mutable.Map[Long, Buffer] = 
  someJavaMapOfJavaLongsToJavaLists.deepAsScala

ScalaJ Converters will go all the way down converting every nested collection or primitive type.
Of course you should be ready to pay some cost for all these conversions.

Import import com.daodecode.scalaj.collection._ aslo brings standard JavaConverters._ in scope, so you can use plain asJava/asScala if you don't have nested collections or collections of primitives.

Having scalaj-googloptional in classpath you can add guava Optionals to your funky data structures and convert between them and scala versions all the way down and back.

val foo: java.util.Set[Optional[java.lang.Double] = ...

import com.daodecode.scalaj.googleoptional._

val scalaFoo: mutable.Set[Option[Double]] = foo.deepAsScala

If you want you scala collections well-done immutable, you can do it as well

val boo: java.util.Set[Optional[java.util.List[java.lang.Double]] = ...

import com.daodecode.scalaj.googleoptional._
import com.daodecode.scalaj.collection.immutable._

val immutableScalaBoo: Set[Option[immutable.Seq[Double]]] = boo.deepAsScalaImmutable

Latest stable release

scalaj-collection

2.10 2.11 2.12 2.13 3
Maven Central Maven Central Maven Central Maven Central Maven Central

sbt

libraryDependencies += "com.daodecode" %% "scalaj-collection" % "0.3.2"

maven

set <scala.binary.version> property to scala version you need, like

<properties>
    <scala.binary.version>2.13</scala.binary.version>
</properties>

and then in dependencies add

<dependency>
    <groupId>com.daodecode</groupId>
    <artifactId>scalaj-collection_${scala.binary.version}</artifactId>
    <version>0.3.2</version>
</dependency>

scalaj-googleoptional

2.10 2.11 2.12 2.13 3
Maven Central Maven Central Maven Central Maven Central Maven Central

sbt

libraryDependencies += "com.daodecode" %% "scalaj-googleoptional" % "0.3.2"

maven

<properties>
    <scala.binary.version>2.13</scala.binary.version>
</properties>

and then in dependencies add

<dependency>
    <groupId>com.daodecode</groupId>
    <artifactId>scalaj-googleoptional_${scala.binary.version}</artifactId>
    <version>0.3.2</version>
</dependency>

Latest snapshot

First add sonatype snapshots repository to your settings

sbt

resolvers += Resolver.sonatypeRepo("snapshots")

maven

<repository>
    <id>snapshots-repo</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    <releases><enabled>false</enabled></releases>
    <snapshots><enabled>true</enabled></snapshots>
</repository>

then add snapshot as a dependency

scalaj-collection

sbt

libraryDependencies += "com.daodecode" %% "scalaj-collection" % "0.3.3-SNAPSHOT"

maven

<properties>
    <scala.binary.version>2.13</scala.binary.version>
</properties>

and then in dependencies add

<dependency>
    <groupId>com.daodecode</groupId>
    <artifactId>scalaj-collection_${scala.binary.version}</artifactId>
    <version>0.3.3-SNAPSHOT</version>
</dependency>

scalaj-googleoptional

sbt

libraryDependencies += "com.daodecode" %% "scalaj-googleoptional" % "0.3.3-SNAPSHOT"

maven

<properties>
    <scala.binary.version>2.13</scala.binary.version>
</properties>

and then in dependencies add

<dependency>
    <groupId>com.daodecode</groupId>
    <artifactId>scalaj-googleoptional_${scala.binary.version}</artifactId>
    <version>0.3.3-SNAPSHOT</version>
</dependency>

Related projects

https://github.com/softprops/guavapants

https://github.com/scalaj/scalaj-collection