Skip to content
The purpose of the Mango library is to provide Guava (Google's core libraries) functionalities to Scala
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

The Mango library

The purpose of the Mango library is to provide Guava (Google's core libraries) functionalities to Scala. This is primarily achieved through wrappers around Guava classes and converter between Java and Scala. Also the package structure is intended to mirror the one from Guava.

However there are differences from the Guava libraries:

  • Whenever null is in Guava to indicate the absence of a value we use Option
  • Mango is more restrictive when passing null as arguments to library functions. The recommendation is to never use null at all.
  • In rare cases the method names are changed to conform to the Scala standard library
  • The Mango library uses Type Classes when appropriate

This is a beta version of the library. Many modules are not published yet because they are either not implemented, the test coverage is too low or the documentation is not complete. We publish these modules as soon as they are ready.

We use Travis CI for continuous integration:


Mango is programmed against guava-14.0.1 using Scala 2.10. If you want to run the tests you will also need the guava-testlib-14.0.1.

To use the Mango library in sbt add the following dependency to your project file:

resolvers ++= Seq(
    "Sonatype Snapshots" at "",
    "Sonatype Releases" at ""

libraryDependencies += "" %% "mango" % "0.7"




// a supplier is just a function () => T
val supplier = () => 3                      //> supplier  : () => Int 
                                            //= function0
// convert to a Guava supplier
val gSupplier = supplier.asJava             //> gSupplier  :[Int] 
                                            //= AsGuavaSupplier(function0)

// create s supplier that memoize its return
// value for 10 seconds
val memSupplier = memoizeWithExpiration(supplier, 10, TimeUnit.SECONDS)
                                              //> memSupplier  : () => Int  
                                              //= Suppliers.memoizeWithExpiration(function0, 10, SECONDS)


import java.util.concurrent.TimeUnit

// the function to cache
val expensiveFnc = (str: String) => str.length  //> expensiveFnc  : String => Int 

// create a cache with a maximum size of 100 and 
// exiration time of 10 minutes
val cache = CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(expensiveFnc)              //> cache  : LoadingCache[String,Int]

cache("MyString")                 //> res0: Int = 8

BloomFilter & Funnel


// A Funnel describes how to decompose a particular object type into primitive field values.
// For example, if we had
case class Person(id: Integer, firstName: String, lastName: String, birthYear: Int)

// our Funnel might look like
implicit val personFunnel = new Funnel[Person] {
 override def funnel(person: Person, into: PrimitiveSink) = {
    .putString(person.firstName, Charsets.UTF_8)
    .putString(person.lastName, Charsets.UTF_8)

val friends: BloomFilter[Person] = BloomFilter.create(500, 0.01)
friendsList.foreach { case p: Person => friends.put(p) }

// much later
if (friends.mightContain(dude)) {
    // the probability that dude reached this place if he isn't a friend is 1%
    // we might, for example, start asynchronously loading things for dude while we do a more expensive exact check

See the individual packages for more examples and documentation.


Conversions to and from the Guava libraries are done with the .asJava and .asScala methods respectively. These methods are imported together with the utility functions of the class. For example:

// import converter for

val fnc = (str: String)=> str.length   //> fnc  : String => Int = function1
fnc.asJava                             //> res0:[String,Int] 
                                       //= AsGuavaFunction(function1)


Just clone the git repository and build Mango in the following way:

sbt update
sbt compile

Don't forget to test

sbt test


Besides the Scaladoc there is an excellent user guide Guava Explained which should be sufficient for almost all questions.


Apache License, Version 2.0

Something went wrong with that request. Please try again.