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.
Downloading
Mango is programmed against guava-14.0.1 using Scala 2.11. 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 "http://oss.sonatype.org/content/repositories/snapshots",
"Sonatype Releases" at "http://oss.sonatype.org/content/repositories/releases"
)
libraryDependencies += "org.feijoas" %% "mango" % "0.11"
Examples
Suppliers
import org.feijoas.mango.common.base.Suppliers._
// a supplier is just a function () => Tval supplier = () =>3//> supplier : () => Int//= function0// convert to a Guava supplierval gSupplier = supplier.asJava //> gSupplier : com.google.common.base.Supplier[Int]//= AsGuavaSupplier(function0)// create s supplier that memoize its return// value for 10 secondsval memSupplier = memoizeWithExpiration(supplier, 10, TimeUnit.SECONDS)
//> memSupplier : () => Int//= Suppliers.memoizeWithExpiration(function0, 10, SECONDS)
Caches
import java.util.concurrent.TimeUnit
import org.feijoas.mango.common.cache._
// the function to cacheval expensiveFnc = (str: String) => str.length //> expensiveFnc : String => Int// create a cache with a maximum size of 100 and// exiration time of 10 minutesval cache = CacheBuilder.newBuilder()
.maximumSize(100)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(expensiveFnc) //> cache : LoadingCache[String,Int]
cache("MyString") //> res0: Int = 8
BloomFilter & Funnel
import org.feijoas.mango.common.hash.Funnel._
import org.feijoas.mango.common.hash.BloomFilter._
// A Funnel describes how to decompose a particular object type into primitive field values.// For example, if we hadcaseclass Person(id: Integer, firstName: String, lastName: String, birthYear: Int)
// our Funnel might look likeimplicitval personFunnel = new Funnel[Person] {
overridedef funnel(person: Person, into: PrimitiveSink) = {
into
.putInt(person.id)
.putString(person.firstName, Charsets.UTF_8)
.putString(person.lastName, Charsets.UTF_8)
.putInt(person.birthYear)
}
}
val friends: BloomFilter[Person] = BloomFilter.create(500, 0.01)
friendsList.foreach { case p: Person => friends.put(p) }
// much laterif (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.
Converter
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 com.google.common.base.Functionimport org.feijoas.mango.common.base.Functions._
val fnc = (str: String)=> str.length //> fnc : String => Int = function1
fnc.asJava //> res0: com.google.common.base.Function[String,Int]//= AsGuavaFunction(function1)
Build
Just clone the git repository and build Mango in the following way:
sbt update
sbt compile
Don't forget to test
{{
sbt test
}}
Help
Besides this Scaladoc there is an excelent user guide Guava Explained which should be sufficient for almost all questions.
The Mango library
This is the documentation for 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:
null
is in Guava to indicate the absence of a value we useOption
null
as arguments to library functions. The recommendation is to never usenull
at all.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.
Downloading
Mango is programmed against
guava-14.0.1
using Scala 2.11. If you want to run the tests you will also need theguava-testlib-14.0.1
.To use the Mango library in sbt add the following dependency to your project file:
Examples
Suppliers
Caches
BloomFilter & Funnel
Range, RangeSet & RangeMap
See the individual packages for more examples and documentation.
Converter
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:Build
Just clone the git repository and build Mango in the following way:
Don't forget to test {{ sbt test }}
Help
Besides this Scaladoc there is an excelent user guide Guava Explained which should be sufficient for almost all questions.
License
Apache License, Version 2.0