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 += "org.feijoas" %% "mango" % "0.8"




// 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

Range & RangeSet

  import math.Ordering.Int

  val range = Range.atLeast(6)                                   // Range[Int,math.Ordering.Int.type] = [6..inf)

  // Pattern match using extractor
  range match {
    case Range(FiniteBound(lower, lowerType), InfiniteBound) => ...

  // immutable range set:
  val rangeSet = RangeSet(, 3), Range.closed(4, 9)) // {(1,3), [4,9]}
  val subSet = rangeSet.subRangeSet(Range.closed(2, 6))         // union view {[2,3), [4,6]}

  // mutable range set:                                                
  val mutableRangeSet = mutable.RangeSet(Range.closed(1, 10))   // {[1, 10]}
  mutableRangeSet += Range.closedOpen(11, 15)                   // disconnected range: {[1, 10], [11, 15)}
  mutableRangeSet += Range.closedOpen(15, 20)                   // connected range; {[1, 10], [11, 20)}
  mutableRangeSet += Range.openClosed(0, 0)                     // empty range; {[1, 10], [11, 20)}
  mutableRangeSet -=, 10)                          // splits [1, 10]; {[1, 5], [10, 10], [11, 20)}

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.