|
Scala example source code file (JavaConverters.scala)
This example Scala source code file (JavaConverters.scala) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Scala JavaConverters.scala source code
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.collection
/** <p>
* A collection of decorators that allow to convert between
* Scala and Java collections using `asScala` and `asJava` methods.
* </p>
* <p>
* The following conversions are supported via `asJava`, `asScala`
* </p>
* <ul>
* <li>scala.collection.Iterable <=> java.lang.Iterable
* <li>scala.collection.Iterator <=> java.util.Iterator
* <li>scala.collection.mutable.Buffer <=> java.util.List
* <li>scala.collection.mutable.Set <=> java.util.Set
* <li>scala.collection.mutable.Map <=> java.util.Map
* <li>scala.collection.mutable.ConcurrentMap <=> java.util.concurrent.ConcurrentMap
* </ul>
* <p>
* In all cases, converting from a source type to a target type and back
* again will return the original source object, e.g.
* </p>
* <pre>
* <b>import scala.collection.JavaConverters._
*
* <b>val sl = new scala.collection.mutable.ListBuffer[Int]
* <b>val jl : java.util.List[Int] = sl.asJava
* <b>val sl2 : scala.collection.mutable.Buffer[Int] = jl.asScala
* assert(sl eq sl2)g</pre>
* <p>
* <p>
* The following conversions also are supported, but the
* direction Scala to Java is done my a more specifically named method:
* `asJavaCollection`, `asJavaEnumeration`, `asJavaDictionary`.
* </p>
* <ul>
* <li>scala.collection.Iterable <=> java.util.Collection
* <li>scala.collection.Iterator <=> java.util.Enumeration
* <li>scala.collection.mutable.Map <=> java.util.Dictionary
* </ul>
* In addition, the following one way conversions are provided via `asJava`:
* </p>
* <ul>
* <li>scala.collection.Seq => java.util.List }
* <li>scala.collection.mutable.Seq => java.util.List
* <li>scala.collection.Set => java.util.Set
* <li>scala.collection.Map => java.util.Map
* </ul>
*
* @author Martin Odersky
* @since 2.8.1
*/
object JavaConverters {
import java.{ lang => jl, util => ju }
import java.util.{ concurrent => juc }
import JavaConversions._
// TODO: I cleaned all this documentation up in JavaConversions, but the
// documentation in here is basically the pre-cleaned-up version with minor
// additions. Would be nice to have in one place.
// Conversion decorator classes
/** Generic class containing the `asJava` converter method */
class AsJava[C](op: => C) {
/** Converts a Scala collection to the corresponding Java collection */
def asJava: C = op
}
/** Generic class containing the `asScala` converter method */
class AsScala[C](op: => C) {
/** Converts a Java collection to the corresponding Scala collection */
def asScala: C = op
}
/** Generic class containing the `asJavaCollection` converter method */
class AsJavaCollection[A](i: Iterable[A]) {
/** Converts a Scala `Iterable` to a Java `Collection` */
def asJavaCollection: ju.Collection[A] = JavaConversions.asJavaCollection(i)
}
/** Generic class containing the `asJavaEnumeration` converter method */
class AsJavaEnumeration[A](i: Iterator[A]) {
/** Converts a Scala `Iterator` to a Java `Enumeration` */
def asJavaEnumeration: ju.Enumeration[A] = JavaConversions.asJavaEnumeration(i)
}
/** Generic class containing the `asJavaDictionary` converter method */
class AsJavaDictionary[A, B](m : mutable.Map[A, B]) {
/** Converts a Scala `Map` to a Java `Dictionary` */
def asJavaDictionary: ju.Dictionary[A, B] = JavaConversions.asJavaDictionary(m)
}
// Scala => Java
/**
* Adds an `asJava` method that implicitly converts a Scala <code>Iterator to a Java Iterator .
* The returned Java <code>Iterator is backed by the provided Scala
* <code>Iterator and any side-effects of using it via the Java interface will
* be visible via the Scala interface and vice versa.
* <p>
* If the Scala <code>Iterator was previously obtained from an implicit or
* explicit call of <code>asIterator(java.util.Iterator) then the original
* Java <code>Iterator will be returned by the `asJava` method.
*
* @param i The <code>Iterator to be converted.
* @return An object with an `asJava` method that returns a Java <code>Iterator view of the argument.
*/
implicit def asJavaIteratorConverter[A](i : Iterator[A]): AsJava[ju.Iterator[A]] =
new AsJava(asJavaIterator(i))
/**
* Adds an `asJavaEnumeration` method that implicitly converts a Scala <code>Iterator to a Java Enumeration .
* The returned Java <code>Enumeration is backed by the provided Scala
* <code>Iterator and any side-effects of using it via the Java interface will
* be visible via the Scala interface and vice versa.
* <p>
* If the Scala <code>Iterator was previously obtained from an implicit or
* explicit call of <code>asIterator(java.util.Enumeration) then the
* original Java <code>Enumeration will be returned.
*
* @param i The <code>Iterator to be converted.
* @return An object with an `asJavaEnumeration` method that returns a Java <code>Enumeration view of the argument.
*/
implicit def asJavaEnumerationConverter[A](i : Iterator[A]): AsJavaEnumeration[A] =
new AsJavaEnumeration(i)
/**
* Adds an `asJava` method that implicitly converts a Scala <code>Iterable to a Java Iterable .
* The returned Java <code>Iterable is backed by the provided Scala
* <code>Iterable and any side-effects of using it via the Java interface will
* be visible via the Scala interface and vice versa.
* <p>
* If the Scala <code>Iterable was previously obtained from an implicit or
* explicit call of <code>asIterable(java.lang.Iterable) then the original
* Java <code>Iterable will be returned.
*
* @param i The <code>Iterable to be converted.
* @return An object with an `asJavaCollection` method that returns a Java <code>Iterable view of the argument.
*/
implicit def asJavaIterableConverter[A](i : Iterable[A]): AsJava[jl.Iterable[A]] =
new AsJava(asJavaIterable(i))
/**
* Adds an `asJavaCollection` method that implicitly converts a Scala <code>Iterable to an immutable Java
* <code>Collection.
* <p>
* If the Scala <code>Iterable was previously obtained from an implicit or
* explicit call of <code>asSizedIterable(java.util.Collection) then the original
* Java <code>Collection will be returned.
*
* @param i The <code>SizedIterable to be converted.
* @return An object with an `asJava` method that returns a Java <code>Collection view of the argument.
*/
implicit def asJavaCollectionConverter[A](i : Iterable[A]): AsJavaCollection[A] =
new AsJavaCollection(i)
/**
* Adds an `asJava` method that implicitly converts a Scala mutable <code>Buffer to a Java List .
* The returned Java <code>List is backed by the provided Scala
* <code>Buffer and any side-effects of using it via the Java interface will
* be visible via the Scala interface and vice versa.
* <p>
* If the Scala <code>Buffer was previously obtained from an implicit or
* explicit call of <code>asBuffer(java.util.List) then the original
* Java <code>List will be returned.
*
* @param b The <code>Buffer to be converted.
* @return An object with an `asJava` method that returns a Java <code>List view of the argument.
*/
implicit def bufferAsJavaListConverter[A](b : mutable.Buffer[A]): AsJava[ju.List[A]] =
new AsJava(bufferAsJavaList(b))
/**
* Adds an `asJava` method that implicitly converts a Scala mutable <code>Seq to a Java List .
* The returned Java <code>List is backed by the provided Scala
* <code>Seq and any side-effects of using it via the Java interface will
* be visible via the Scala interface and vice versa.
* <p>
* If the Scala <code>Seq was previously obtained from an implicit or
* explicit call of <code>asSeq(java.util.List) then the original
* Java <code>List will be returned.
*
* @param b The <code>Seq to be converted.
* @return An object with an `asJava` method that returns a Java <code>List view of the argument.
*/
implicit def mutableSeqAsJavaListConverter[A](b : mutable.Seq[A]): AsJava[ju.List[A]] =
new AsJava(mutableSeqAsJavaList(b))
/**
* Adds an `asJava` method that implicitly converts a Scala <code>Seq to a Java List .
* The returned Java <code>List is backed by the provided Scala
* <code>Seq and any side-effects of using it via the Java interface will
* be visible via the Scala interface and vice versa.
* <p>
* If the Scala <code>Seq was previously obtained from an implicit or
* explicit call of <code>asSeq(java.util.List) then the original
* Java <code>List will be returned.
*
* @param b The <code>Seq to be converted.
* @return An object with an `asJava` method that returns a Java <code>List view of the argument.
*/
implicit def seqAsJavaListConverter[A](b : Seq[A]): AsJava[ju.List[A]] =
new AsJava(seqAsJavaList(b))
@deprecated("Use bufferAsJavaListConverter instead", "2.9.0")
def asJavaListConverter[A](b : mutable.Buffer[A]): AsJava[ju.List[A]] = bufferAsJavaListConverter(b)
@deprecated("Use mutableSeqAsJavaListConverter instead", "2.9.0")
def asJavaListConverter[A](b : mutable.Seq[A]): AsJava[ju.List[A]] = mutableSeqAsJavaListConverter(b)
@deprecated("Use seqAsJavaListConverter instead", "2.9.0")
def asJavaListConverter[A](b : Seq[A]): AsJava[ju.List[A]] = seqAsJavaListConverter(b)
/**
* Adds an `asJava` method that implicitly converts a Scala mutable <code>Set to a Java Set .
* The returned Java <code>Set is backed by the provided Scala
* <code>Set and any side-effects of using it via the Java interface will
* be visible via the Scala interface and vice versa.
* <p>
* If the Scala <code>Set was previously obtained from an implicit or
* explicit call of <code>asSet(java.util.Set) then the original
* Java <code>Set will be returned.
*
* @param s The <code>Set to be converted.
* @return An object with an `asJava` method that returns a Java <code>Set view of the argument.
*/
implicit def mutableSetAsJavaSetConverter[A](s : mutable.Set[A]): AsJava[ju.Set[A]] =
new AsJava(mutableSetAsJavaSet(s))
@deprecated("Use mutableSetAsJavaSetConverter instead", "2.9.0")
def asJavaSetConverter[A](s : mutable.Set[A]): AsJava[ju.Set[A]] = mutableSetAsJavaSetConverter(s)
/**
* Adds an `asJava` method that implicitly converts a Scala <code>Set to a Java Set .
* The returned Java <code>Set is backed by the provided Scala
* <code>Set and any side-effects of using it via the Java interface will
* be visible via the Scala interface and vice versa.
* <p>
* If the Scala <code>Set was previously obtained from an implicit or
* explicit call of <code>asSet(java.util.Set) then the original
* Java <code>Set will be returned.
*
* @param s The <code>Set to be converted.
* @return An object with an `asJava` method that returns a Java <code>Set view of the argument.
*/
implicit def setAsJavaSetConverter[A](s : Set[A]): AsJava[ju.Set[A]] =
new AsJava(setAsJavaSet(s))
@deprecated("Use setAsJavaSetConverter instead", "2.9.0")
def asJavaSetConverter[A](s : Set[A]): AsJava[ju.Set[A]] = setAsJavaSetConverter(s)
/**
* Adds an `asJava` method that implicitly converts a Scala mutable <code>Map to a Java Map .
* The returned Java <code>Map is backed by the provided Scala
* <code>Map and any side-effects of using it via the Java interface will
* be visible via the Scala interface and vice versa.
* <p>
* If the Scala <code>Map was previously obtained from an implicit or
* explicit call of <code>asMap(java.util.Map) then the original
* Java <code>Map will be returned.
*
* @param m The <code>Map to be converted.
* @return An object with an `asJava` method that returns a Java <code>Map view of the argument.
*/
implicit def mutableMapAsJavaMapConverter[A, B](m : mutable.Map[A, B]): AsJava[ju.Map[A, B]] =
new AsJava(mutableMapAsJavaMap(m))
@deprecated("use mutableMapAsJavaMapConverter instead", "2.9.0")
def asJavaMapConverter[A, B](m : mutable.Map[A, B]): AsJava[ju.Map[A, B]] = mutableMapAsJavaMapConverter(m)
/**
* Adds an `asJavaDictionary` method that implicitly converts a Scala mutable <code>Map to a Java Dictionary .
* The returned Java <code>Dictionary is backed by the provided Scala
* <code>Dictionary and any side-effects of using it via the Java interface will
* be visible via the Scala interface and vice versa.
* <p>
* If the Scala <code>Dictionary was previously obtained from an implicit or
* explicit call of <code>asMap(java.util.Dictionary) then the original
* Java <code>Dictionary will be returned.
*
* @param m The <code>Map to be converted.
* @return An object with an `asJavaDictionary` method that returns a Java <code>Dictionary view of the argument.
*/
implicit def asJavaDictionaryConverter[A, B](m : mutable.Map[A, B]): AsJavaDictionary[A, B] =
new AsJavaDictionary(m)
/**
* Adds an `asJava` method that implicitly converts a Scala <code>Map to a Java Map .
* The returned Java <code>Map is backed by the provided Scala
* <code>Map and any side-effects of using it via the Java interface will
* be visible via the Scala interface and vice versa.
* <p>
* If the Scala <code>Map was previously obtained from an implicit or
* explicit call of <code>asMap(java.util.Map) then the original
* Java <code>Map will be returned.
*
* @param m The <code>Map to be converted.
* @return An object with an `asJava` method that returns a Java <code>Map view of the argument.
*/
implicit def mapAsJavaMapConverter[A, B](m : Map[A, B]): AsJava[ju.Map[A, B]] =
new AsJava(mapAsJavaMap(m))
@deprecated("Use mapAsJavaMapConverter instead", "2.9.0")
def asJavaMapConverter[A, B](m : Map[A, B]): AsJava[ju.Map[A, B]] = mapAsJavaMapConverter(m)
/**
* Adds an `asJava` method that implicitly converts a Scala mutable `ConcurrentMap` to a Java `ConcurrentMap`.
* The returned Java `ConcurrentMap` is backed by the provided Scala `ConcurrentMap`
* and any side-effects of using it via the Java interface will be visible
* via the Scala interface and vice versa.
* <p>
* If the Scala <code>ConcurrentMap was previously obtained from an implicit or
* explicit call of <code>asConcurrentMap(java.util.concurrect.ConcurrentMap) then the original
* Java <code>ConcurrentMap will be returned.
*
* @param m The <code>ConcurrentMap to be converted.
* @return An object with an `asJava` method that returns a Java <code>ConcurrentMap view of the argument.
*/
implicit def asJavaConcurrentMapConverter[A, B](m: mutable.ConcurrentMap[A, B]): AsJava[juc.ConcurrentMap[A, B]] =
new AsJava(asJavaConcurrentMap(m))
/**
* Adds an `asScala` method that implicitly converts a Java <code>Iterator to a Scala Iterator .
* The returned Scala <code>Iterator is backed by the provided Java
* <code>Iterator and any side-effects of using it via the Scala interface will
* be visible via the Java interface and vice versa.
* <p>
* If the Java <code>Iterator was previously obtained from an implicit or
* explicit call of <code>asIterator(scala.collection.Iterator) then the original
* Scala <code>Iterator will be returned.
*
* @param i The <code>Iterator to be converted.
* @return An object with an `asScala` method that returns a Scala <code>Iterator view of the argument.
*/
implicit def asScalaIteratorConverter[A](i : ju.Iterator[A]): AsScala[Iterator[A]] =
new AsScala(asScalaIterator(i))
/**
* Adds an `asScala` method that implicitly converts a Java <code>Enumeration to a Scala Iterator .
* The returned Scala <code>Iterator is backed by the provided Java
* <code>Enumeration and any side-effects of using it via the Scala interface will
* be visible via the Java interface and vice versa.
* <p>
* If the Java <code>Enumeration was previously obtained from an implicit or
* explicit call of <code>asEnumeration(scala.collection.Iterator) then the
* original Scala <code>Iterator will be returned.
*
* @param i The <code>Enumeration to be converted.
* @return An object with an `asScala` method that returns a Scala <code>Iterator view of the argument.
*/
implicit def enumerationAsScalaIteratorConverter[A](i : ju.Enumeration[A]): AsScala[Iterator[A]] =
new AsScala(enumerationAsScalaIterator(i))
/**
* Adds an `asScala` method that implicitly converts a Java <code>Iterable to a Scala Iterable .
* The returned Scala <code>Iterable is backed by the provided Java
* <code>Iterable and any side-effects of using it via the Scala interface will
* be visible via the Java interface and vice versa.
* <p>
* If the Java <code>Iterable was previously obtained from an implicit or
* explicit call of <code>asIterable(scala.collection.Iterable) then the original
* Scala <code>Iterable will be returned.
*
* @param i The <code>Iterable to be converted.
* @return An object with an `asScala` method that returns a Scala <code>Iterable view of the argument.
*/
implicit def iterableAsScalaIterableConverter[A](i : jl.Iterable[A]): AsScala[Iterable[A]] =
new AsScala(iterableAsScalaIterable(i))
@deprecated("Use iterableAsScalaIterableConverter instead", "2.9.0")
def asScalaIterableConverter[A](i : jl.Iterable[A]): AsScala[Iterable[A]] = iterableAsScalaIterableConverter(i)
/**
* Adds an `asScala` method that implicitly converts a Java <code>Collection to an Scala Iterable .
* <p>
* If the Java <code>Collection was previously obtained from an implicit or
* explicit call of <code>asCollection(scala.collection.SizedIterable) then
* the original Scala <code>SizedIterable will be returned.
*
* @param i The <code>Collection to be converted.
* @return An object with an `asScala` method that returns a Scala <code>SizedIterable view of the argument.
*/
implicit def collectionAsScalaIterableConverter[A](i : ju.Collection[A]): AsScala[Iterable[A]] =
new AsScala(collectionAsScalaIterable(i))
@deprecated("Use collectionAsScalaIterableConverter instead", "2.9.0")
def asScalaIterableConverter[A](i : ju.Collection[A]): AsScala[Iterable[A]] = collectionAsScalaIterableConverter(i)
/**
* Adds an `asScala` method that implicitly converts a Java <code>List to a Scala mutable Buffer .
* The returned Scala <code>Buffer is backed by the provided Java
* <code>List and any side-effects of using it via the Scala interface will
* be visible via the Java interface and vice versa.
* <p>
* If the Java <code>List was previously obtained from an implicit or
* explicit call of <code>asList(scala.collection.mutable.Buffer) then the original
* Scala <code>Buffer will be returned.
*
* @param l The <code>List to be converted.
* @return An object with an `asScala` method that returns a Scala mutable <code>Buffer view of the argument.
*/
implicit def asScalaBufferConverter[A](l : ju.List[A]): AsScala[mutable.Buffer[A]] =
new AsScala(asScalaBuffer(l))
/**
* Adds an `asScala` method that implicitly converts a Java <code>Set to a Scala mutable Set .
* The returned Scala <code>Set is backed by the provided Java
* <code>Set and any side-effects of using it via the Scala interface will
* be visible via the Java interface and vice versa.
* <p>
* If the Java <code>Set was previously obtained from an implicit or
* explicit call of <code>asSet(scala.collection.mutable.Set) then the original
* Scala <code>Set will be returned.
*
* @param s The <code>Set to be converted.
* @return An object with an `asScala` method that returns a Scala mutable <code>Set view of the argument.
*/
implicit def asScalaSetConverter[A](s : ju.Set[A]): AsScala[mutable.Set[A]] =
new AsScala(asScalaSet(s))
/**
* Adds an `asScala` method that implicitly converts a Java <code>Map to a Scala mutable Map .
* The returned Scala <code>Map is backed by the provided Java
* <code>Map and any side-effects of using it via the Scala interface will
* be visible via the Java interface and vice versa.
* <p>
* If the Java <code>Map was previously obtained from an implicit or
* explicit call of <code>asMap(scala.collection.mutable.Map) then the original
* Scala <code>Map will be returned.
*
* @param m The <code>Map to be converted.
* @return An object with an `asScala` method that returns a Scala mutable <code>Map view of the argument.
*/
implicit def mapAsScalaMapConverter[A, B](m : ju.Map[A, B]): AsScala[mutable.Map[A, B]] =
new AsScala(mapAsScalaMap(m))
@deprecated("Use mapAsScalaMapConverter instead", "2.9.0")
def asScalaMapConverter[A, B](m : ju.Map[A, B]): AsScala[mutable.Map[A, B]] = mapAsScalaMapConverter(m)
/**
* Adds an `asScala` method that implicitly converts a Java <code>ConcurrentMap to a Scala mutable ConcurrentMap .
* The returned Scala <code>ConcurrentMap is backed by the provided Java
* <code>ConcurrentMap and any side-effects of using it via the Scala interface will
* be visible via the Java interface and vice versa.
* <p>
* If the Java <code>ConcurrentMap was previously obtained from an implicit or
* explicit call of <code>asConcurrentMap(scala.collection.mutable.ConcurrentMap) then the original
* Scala <code>ConcurrentMap will be returned.
*
* @param m The <code>ConcurrentMap to be converted.
* @return An object with an `asScala` method that returns a Scala mutable <code>ConcurrentMap view of the argument.
*/
implicit def asScalaConcurrentMapConverter[A, B](m: juc.ConcurrentMap[A, B]): AsScala[mutable.ConcurrentMap[A, B]] =
new AsScala(asScalaConcurrentMap(m))
/**
* Adds an `asScala` method that implicitly converts a Java <code>Dictionary to a Scala mutable Map[String, String] .
* The returned Scala <code>Map[String, String] is backed by the provided Java
* <code>Dictionary and any side-effects of using it via the Scala interface will
* be visible via the Java interface and vice versa.
*
* @param m The <code>Dictionary to be converted.
* @return An object with an `asScala` method that returns a Scala mutable <code>Map[String, String] view of the argument.
*/
implicit def dictionaryAsScalaMapConverter[A, B](p: ju.Dictionary[A, B]): AsScala[mutable.Map[A, B]] =
new AsScala(dictionaryAsScalaMap(p))
/**
* Adds an `asScala` method that implicitly converts a Java <code>Properties to a Scala mutable Map[String, String] .
* The returned Scala <code>Map[String, String] is backed by the provided Java
* <code>Properties and any side-effects of using it via the Scala interface will
* be visible via the Java interface and vice versa.
*
* @param m The <code>Properties to be converted.
* @return An object with an `asScala` method that returns a Scala mutable <code>Map[String, String] view of the argument.
*/
implicit def propertiesAsScalaMapConverter(p: ju.Properties): AsScala[mutable.Map[String, String]] =
new AsScala(propertiesAsScalaMap(p))
@deprecated("Use propertiesAsScalaMapConverter instead", "2.9.0")
def asScalaMapConverter(p: ju.Properties): AsScala[mutable.Map[String, String]] =
propertiesAsScalaMapConverter(p)
}
Other Scala examples (source code examples)
Here is a short list of links related to this Scala JavaConverters.scala source code file:
|