summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2010-08-02 13:53:16 +0000
committerMartin Odersky <odersky@gmail.com>2010-08-02 13:53:16 +0000
commited3c7e54fc700c6fa92d68f091be3da68662bd44 (patch)
tree72e9d01e07e7a76e4ddeaa18c42e67e911ac2431
parentf9fe76375d36cc5355e0e4157ee67d93f8410a0d (diff)
downloadscala-ed3c7e54fc700c6fa92d68f091be3da68662bd44.tar.gz
scala-ed3c7e54fc700c6fa92d68f091be3da68662bd44.tar.bz2
scala-ed3c7e54fc700c6fa92d68f091be3da68662bd44.zip
Revamped ScalaConversions.
-rw-r--r--src/library/scala/collection/JavaConversions.scala165
-rwxr-xr-xsrc/library/scala/collection/JavaConverters.scala456
2 files changed, 581 insertions, 40 deletions
diff --git a/src/library/scala/collection/JavaConversions.scala b/src/library/scala/collection/JavaConversions.scala
index 80fc28e234..a19c690ba9 100644
--- a/src/library/scala/collection/JavaConversions.scala
+++ b/src/library/scala/collection/JavaConversions.scala
@@ -55,6 +55,16 @@ object JavaConversions {
import java.{ lang => jl, util => ju }
import java.util.{ concurrent => juc }
+ // Conversion decorator classes
+
+ class AsJava[C](op: => C) {
+ def asJava: C = op
+ }
+
+ class AsScala[C](op: => C) {
+ def asScala: C = op
+ }
+
// Scala => Java
/**
@@ -70,11 +80,14 @@ object JavaConversions {
* @param i The <code>Iterator</code> to be converted.
* @return A Java <code>Iterator</code> view of the argument.
*/
- implicit def asIterator[A](i : Iterator[A]): ju.Iterator[A] = i match {
+ implicit def asJavaIterator[A](i : Iterator[A]): ju.Iterator[A] = i match {
case JIteratorWrapper(wrapped) => wrapped
case _ => IteratorWrapper(i)
}
+ @deprecated("use asJavaIterator instead")
+ def asIterator[A](i : Iterator[A]): ju.Iterator[A] = asJavaIterator[A](i)
+
/**
* Implicitly converts a Scala <code>Iterator</code> to a Java <code>Enumeration</code>.
* The returned Java <code>Enumeration</code> is backed by the provided Scala
@@ -88,11 +101,14 @@ object JavaConversions {
* @param i The <code>Iterator</code> to be converted.
* @return A Java <code>Enumeration</code> view of the argument.
*/
- implicit def asEnumeration[A](i : Iterator[A]): ju.Enumeration[A] = i match {
+ implicit def asJavaEnumeration[A](i : Iterator[A]): ju.Enumeration[A] = i match {
case JEnumerationWrapper(wrapped) => wrapped
case _ => IteratorWrapper(i)
}
+ @deprecated("use asJavaEnmeration instead")
+ def asEnumeration[A](i : Iterator[A]): ju.Enumeration[A] = asJavaEnumeration[A](i)
+
/**
* Implicitly converts a Scala <code>Iterable</code> to a Java <code>Iterable</code>.
* The returned Java <code>Iterable</code> is backed by the provided Scala
@@ -106,11 +122,14 @@ object JavaConversions {
* @param i The <code>Iterable</code> to be converted.
* @return A Java <code>Iterable</code> view of the argument.
*/
- implicit def asIterable[A](i : Iterable[A]): jl.Iterable[A] = i match {
+ implicit def asJavaIterable[A](i : Iterable[A]): jl.Iterable[A] = i match {
case JIterableWrapper(wrapped) => wrapped
case _ => IterableWrapper(i)
}
+ @deprecated("use asJavaIterable instead")
+ def asIterable[A](i : Iterable[A]): jl.Iterable[A] = asJavaIterable[A](i)
+
/**
* Implicitly converts a Scala <code>Iterable</code> to an immutable Java
* <code>Collection</code>.
@@ -122,11 +141,14 @@ object JavaConversions {
* @param i The <code>SizedIterable</code> to be converted.
* @return A Java <code>Collection</code> view of the argument.
*/
- implicit def asCollection[A](i : Iterable[A]): ju.Collection[A] = i match {
+ implicit def asJavaCollection[A](i : Iterable[A]): ju.Collection[A] = i match {
case JCollectionWrapper(wrapped) => wrapped
case _ => new IterableWrapper(i)
}
+ @deprecated("use asJavaCollection instead")
+ def asCollection[A](i : Iterable[A]): ju.Collection[A] = asJavaCollection[A](i)
+
/**
* Implicitly converts a Scala mutable <code>Buffer</code> to a Java <code>List</code>.
* The returned Java <code>List</code> is backed by the provided Scala
@@ -140,11 +162,14 @@ object JavaConversions {
* @param b The <code>Buffer</code> to be converted.
* @return A Java <code>List</code> view of the argument.
*/
- implicit def asList[A](b : mutable.Buffer[A]): ju.List[A] = b match {
+ implicit def asJavaList[A](b : mutable.Buffer[A]): ju.List[A] = b match {
case JListWrapper(wrapped) => wrapped
case _ => new MutableBufferWrapper(b)
}
+ @deprecated("use asJavaList instead")
+ def asList[A](b : mutable.Buffer[A]): ju.List[A] = asJavaList[A](b)
+
/**
* Implicitly converts a Scala mutable <code>Seq</code> to a Java <code>List</code>.
* The returned Java <code>List</code> is backed by the provided Scala
@@ -158,11 +183,14 @@ object JavaConversions {
* @param b The <code>Seq</code> to be converted.
* @return A Java <code>List</code> view of the argument.
*/
- implicit def asList[A](b : mutable.Seq[A]): ju.List[A] = b match {
+ implicit def asJavaList[A](b : mutable.Seq[A]): ju.List[A] = b match {
case JListWrapper(wrapped) => wrapped
case _ => new MutableSeqWrapper(b)
}
+ @deprecated("use asJavaList instead")
+ def asList[A](b : mutable.Seq[A]): ju.List[A] = asJavaList[A](b)
+
/**
* Implicitly converts a Scala <code>Seq</code> to a Java <code>List</code>.
* The returned Java <code>List</code> is backed by the provided Scala
@@ -174,14 +202,16 @@ object JavaConversions {
* Java <code>List</code> will be returned.
*
* @param b The <code>Seq</code> to be converted.
- * @return A Java <code>List</co *
-de> view of the argument.
+ * @return A Java <code>List</code> view of the argument.
*/
- implicit def asList[A](b : Seq[A]): ju.List[A] = b match {
+ implicit def asJavaList[A](b : Seq[A]): ju.List[A] = b match {
case JListWrapper(wrapped) => wrapped
case _ => new SeqWrapper(b)
}
+ @deprecated("use asJavaList instead")
+ def asList[A](b : Seq[A]): ju.List[A] = asJavaList[A](b)
+
/**
* Implicitly converts a Scala mutable <code>Set</code> to a Java <code>Set</code>.
* The returned Java <code>Set</code> is backed by the provided Scala
@@ -195,11 +225,14 @@ de> view of the argument.
* @param s The <code>Set</code> to be converted.
* @return A Java <code>Set</code> view of the argument.
*/
- implicit def asSet[A](s : mutable.Set[A]): ju.Set[A] = s match {
+ implicit def asJavaSet[A](s : mutable.Set[A]): ju.Set[A] = s match {
case JSetWrapper(wrapped) => wrapped
case _ => new MutableSetWrapper(s)
}
+ @deprecated("use asJavaSet instead")
+ def asSet[A](s : mutable.Set[A]): ju.Set[A] = asJavaSet[A](s)
+
/**
* Implicitly converts a Scala <code>Set</code> to a Java <code>Set</code>.
* The returned Java <code>Set</code> is backed by the provided Scala
@@ -213,11 +246,14 @@ de> view of the argument.
* @param s The <code>Set</code> to be converted.
* @return A Java <code>Set</code> view of the argument.
*/
- implicit def asSet[A](s: Set[A]): ju.Set[A] = s match {
+ implicit def asJavaSet[A](s: Set[A]): ju.Set[A] = s match {
case JSetWrapper(wrapped) => wrapped
case _ => new SetWrapper(s)
}
+ @deprecated("use asJavaSet instead")
+ def asSet[A](s : Set[A]): ju.Set[A] = asJavaSet[A](s)
+
/**
* Implicitly converts a Scala mutable <code>Map</code> to a Java <code>Map</code>.
* The returned Java <code>Map</code> is backed by the provided Scala
@@ -231,12 +267,15 @@ de> view of the argument.
* @param m The <code>Map</code> to be converted.
* @return A Java <code>Map</code> view of the argument.
*/
- implicit def asMap[A, B](m : mutable.Map[A, B]): ju.Map[A, B] = m match {
+ implicit def asJavaMap[A, B](m : mutable.Map[A, B]): ju.Map[A, B] = m match {
//case JConcurrentMapWrapper(wrapped) => wrapped
case JMapWrapper(wrapped) => wrapped
case _ => new MutableMapWrapper(m)
}
+ @deprecated("use asJavaMap instead")
+ def asMap[A, B](m : mutable.Map[A, B]): ju.Map[A, B] = asJavaMap[A, B](m)
+
/**
* Implicitly converts a Scala mutable <code>Map</code> to a Java <code>Dictionary</code>.
* The returned Java <code>Dictionary</code> is backed by the provided Scala
@@ -250,24 +289,14 @@ de> view of the argument.
* @param m The <code>Map</code> to be converted.
* @return A Java <code>Dictionary</code> view of the argument.
*/
- implicit def asDictionary[A, B](m : mutable.Map[A, B]): ju.Dictionary[A, B] = m match {
+ implicit def asJavaDictionary[A, B](m : mutable.Map[A, B]): ju.Dictionary[A, B] = m match {
//case JConcurrentMapWrapper(wrapped) => wrapped
case JDictionaryWrapper(wrapped) => wrapped
case _ => new DictionaryWrapper(m)
}
- /**
- * Implicitly converts a Java <code>Properties</code> to a Scala mutable <code>Map[String, String]</code>.
- * The returned Scala <code>Map[String, String]</code> is backed by the provided Java
- * <code>Properties</code> 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</code> to be converted.
- * @return A Scala mutable <code>Map[String, String]</code> view of the argument.
- */
- implicit def asMap(p: ju.Properties): mutable.Map[String, String] = p match {
- case _ => new JPropertiesWrapper(p)
- }
+ @deprecated("use asJavaDictionary instead")
+ def asDictionary[A, B](m : mutable.Map[A, B]): ju.Dictionary[A, B] = asJavaDictionary[A, B](m)
/**
* Implicitly converts a Scala <code>Map</code> to a Java <code>Map</code>.
@@ -282,12 +311,15 @@ de> view of the argument.
* @param m The <code>Map</code> to be converted.
* @return A Java <code>Map</code> view of the argument.
*/
- implicit def asMap[A, B](m : Map[A, B]): ju.Map[A, B] = m match {
+ implicit def asJavaMap[A, B](m : Map[A, B]): ju.Map[A, B] = m match {
//case JConcurrentMapWrapper(wrapped) => wrapped
case JMapWrapper(wrapped) => wrapped
case _ => new MapWrapper(m)
}
+ @deprecated("use asJavaMap instead")
+ def asMap[A, B](m : Map[A, B]): ju.Map[A, B] = asJavaMap[A, B](m)
+
/**
* Implicitly converts a Scala mutable `ConcurrentMap` to a Java `ConcurrentMap`.
* The returned Java `ConcurrentMap` is backed by the provided Scala `ConcurrentMap`
@@ -297,12 +329,18 @@ de> view of the argument.
* If the Scala <code>ConcurrentMap</code> was previously obtained from an implicit or
* explicit call of <code>asConcurrentMap(java.util.concurrect.ConcurrentMap)</code> then the original
* Java <code>ConcurrentMap</code> will be returned.
+ *
+ * @param m The <code>ConcurrentMap</code> to be converted.
+ * @return A Java <code>ConcurrentMap</code> view of the argument.
*/
- implicit def asConcurrentMap[A, B](m: mutable.ConcurrentMap[A, B]): juc.ConcurrentMap[A, B] = m match {
+ implicit def asJavaConcurrentMap[A, B](m: mutable.ConcurrentMap[A, B]): juc.ConcurrentMap[A, B] = m match {
case JConcurrentMapWrapper(wrapped) => wrapped
case _ => new ConcurrentMapWrapper(m)
}
+ @deprecated("use asJavaConcurrentMap instead")
+ def asConcurrentMap[A, B](m: mutable.ConcurrentMap[A, B]): juc.ConcurrentMap[A, B] = asJavaConcurrentMap[A, B](m)
+
// Java => Scala
/**
@@ -318,11 +356,14 @@ de> view of the argument.
* @param i The <code>Iterator</code> to be converted.
* @return A Scala <code>Iterator</code> view of the argument.
*/
- implicit def asIterator[A](i : ju.Iterator[A]): Iterator[A] = i match {
+ implicit def asScalaIterator[A](i : ju.Iterator[A]): Iterator[A] = i match {
case IteratorWrapper(wrapped) => wrapped
case _ => JIteratorWrapper(i)
}
+ @deprecated("use asScalaIterator instead")
+ def asIterator[A](i : ju.Iterator[A]): Iterator[A] = asScalaIterator[A](i)
+
/**
* Implicitly converts a Java <code>Enumeration</code> to a Scala <code>Iterator</code>.
* The returned Scala <code>Iterator</code> is backed by the provided Java
@@ -336,11 +377,14 @@ de> view of the argument.
* @param i The <code>Enumeration</code> to be converted.
* @return A Scala <code>Iterator</code> view of the argument.
*/
- implicit def asIterator[A](i : ju.Enumeration[A]): Iterator[A] = i match {
+ implicit def enumerationAsScalaIterator[A](i : ju.Enumeration[A]): Iterator[A] = i match {
case IteratorWrapper(wrapped) => wrapped
case _ => JEnumerationWrapper(i)
}
+ @deprecated("use enumerationAsScalaIterator instead")
+ def asIterator[A](i : ju.Enumeration[A]): Iterator[A] = enumerationAsScalaIterator[A](i)
+
/**
* Implicitly converts a Java <code>Iterable</code> to a Scala <code>Iterable</code>.
* The returned Scala <code>Iterable</code> is backed by the provided Java
@@ -354,11 +398,14 @@ de> view of the argument.
* @param i The <code>Iterable</code> to be converted.
* @return A Scala <code>Iterable</code> view of the argument.
*/
- implicit def asIterable[A](i : jl.Iterable[A]): Iterable[A] = i match {
+ implicit def asScalaIterable[A](i : jl.Iterable[A]): Iterable[A] = i match {
case IterableWrapper(wrapped) => wrapped
case _ => JIterableWrapper(i)
}
+ @deprecated("use asScalaIterable instead")
+ def asIterable[A](i : jl.Iterable[A]): Iterable[A] = asScalaIterable[A](i)
+
/**
* Implicitly converts a Java <code>Collection</code> to an Scala <code>Iterable</code>.
* <p>
@@ -369,11 +416,14 @@ de> view of the argument.
* @param i The <code>Collection</code> to be converted.
* @return A Scala <code>SizedIterable</code> view of the argument.
*/
- implicit def asIterable[A](i : ju.Collection[A]): Iterable[A] = i match {
+ implicit def asScalaIterable[A](i : ju.Collection[A]): Iterable[A] = i match {
case IterableWrapper(wrapped) => wrapped
case _ => JCollectionWrapper(i)
}
+ @deprecated("use asScalaIterable instead")
+ def asIterable[A](i : ju.Collection[A]): Iterable[A] = asScalaIterable[A](i)
+
/**
* Implicitly converts a Java <code>List</code> to a Scala mutable <code>Buffer</code>.
* The returned Scala <code>Buffer</code> is backed by the provided Java
@@ -387,11 +437,14 @@ de> view of the argument.
* @param l The <code>List</code> to be converted.
* @return A Scala mutable <code>Buffer</code> view of the argument.
*/
- implicit def asBuffer[A](l : ju.List[A]): mutable.Buffer[A] = l match {
+ implicit def asScalaBuffer[A](l : ju.List[A]): mutable.Buffer[A] = l match {
case MutableBufferWrapper(wrapped) => wrapped
case _ =>new JListWrapper(l)
}
+ @deprecated("use asScalaBuffer instead")
+ def asBuffer[A](l : ju.List[A]): mutable.Buffer[A] = asScalaBuffer[A](l)
+
/**
* Implicitly converts a Java <code>Set</code> to a Scala mutable <code>Set</code>.
* The returned Scala <code>Set</code> is backed by the provided Java
@@ -405,11 +458,14 @@ de> view of the argument.
* @param s The <code>Set</code> to be converted.
* @return A Scala mutable <code>Set</code> view of the argument.
*/
- implicit def asSet[A](s : ju.Set[A]): mutable.Set[A] = s match {
+ implicit def asScalaSet[A](s : ju.Set[A]): mutable.Set[A] = s match {
case MutableSetWrapper(wrapped) => wrapped
case _ =>new JSetWrapper(s)
}
+ @deprecated("use asScalaSet instead")
+ def asSet[A](s : ju.Set[A]): mutable.Set[A] = asScalaSet[A](s)
+
/**
* Implicitly converts a Java <code>Map</code> to a Scala mutable <code>Map</code>.
* The returned Scala <code>Map</code> is backed by the provided Java
@@ -423,12 +479,15 @@ de> view of the argument.
* @param m The <code>Map</code> to be converted.
* @return A Scala mutable <code>Map</code> view of the argument.
*/
- implicit def asMap[A, B](m : ju.Map[A, B]): mutable.Map[A, B] = m match {
+ implicit def asScalaMap[A, B](m : ju.Map[A, B]): mutable.Map[A, B] = m match {
//case ConcurrentMapWrapper(wrapped) => wrapped
case MutableMapWrapper(wrapped) => wrapped
case _ => new JMapWrapper(m)
}
+ @deprecated("use asScalaMap instead")
+ def asMap[A, B](m : ju.Map[A, B]): mutable.Map[A, B] = asScalaMap[A, B](m)
+
/**
* Implicitly converts a Java <code>ConcurrentMap</code> to a Scala mutable <code>ConcurrentMap</code>.
* The returned Scala <code>ConcurrentMap</code> is backed by the provided Java
@@ -442,11 +501,14 @@ de> view of the argument.
* @param m The <code>ConcurrentMap</code> to be converted.
* @return A Scala mutable <code>ConcurrrentMap</code> view of the argument.
*/
- implicit def asConcurrentMap[A, B](m: juc.ConcurrentMap[A, B]): mutable.ConcurrentMap[A, B] = m match {
+ implicit def asScalaConcurrentMap[A, B](m: juc.ConcurrentMap[A, B]): mutable.ConcurrentMap[A, B] = m match {
case cmw: ConcurrentMapWrapper[a, b] => cmw.underlying
case _ => new JConcurrentMapWrapper(m)
}
+ @deprecated("use asScalaConcurrentMap instead")
+ def asConcurrentMap[A, B](m: juc.ConcurrentMap[A, B]): mutable.ConcurrentMap[A, B] = asScalaConcurrentMap[A, B](m)
+
/**
* Implicitly converts a Java <code>Dictionary</code> to a Scala mutable <code>Map[String, String]</code>.
* The returned Scala <code>Map[String, String]</code> is backed by the provided Java
@@ -456,12 +518,31 @@ de> view of the argument.
* @param m The <code>Dictionary</code> to be converted.
* @return A Scala mutable <code>Map[String, String]</code> view of the argument.
*/
- implicit def asMap[A, B](p: ju.Dictionary[A, B]): mutable.Map[A, B] = p match {
+ implicit def dictionaryAsScalaMap[A, B](p: ju.Dictionary[A, B]): mutable.Map[A, B] = p match {
case DictionaryWrapper(wrapped) => wrapped
case _ => new JDictionaryWrapper(p)
}
- // Private implementations ...
+ @deprecated("use dictionaryAsScalaMap instead")
+ def asMap[A, B](p: ju.Dictionary[A, B]): mutable.Map[A, B] = dictionaryAsScalaMap[A, B](p)
+
+ /**
+ * Implicitly converts a Java <code>Properties</code> to a Scala mutable <code>Map[String, String]</code>.
+ * The returned Scala <code>Map[String, String]</code> is backed by the provided Java
+ * <code>Properties</code> 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</code> to be converted.
+ * @return A Scala mutable <code>Map[String, String]</code> view of the argument.
+ */
+ implicit def asScalaMap(p: ju.Properties): mutable.Map[String, String] = p match {
+ case _ => new JPropertiesWrapper(p)
+ }
+
+ @deprecated("use asScalaMap instead")
+ def asMap(p: ju.Properties): mutable.Map[String, String] = asScalaMap(p)
+
+ // Private implementations (shared by JavaConverters) ...
case class IteratorWrapper[A](underlying : Iterator[A]) extends ju.Iterator[A] with ju.Enumeration[A] {
def hasNext = underlying.hasNext
@@ -471,6 +552,10 @@ de> view of the argument.
def remove = throw new UnsupportedOperationException
}
+ class ToIteratorWrapper[A](underlying : Iterator[A]) {
+ def asJava = new IteratorWrapper(underlying)
+ }
+
case class JIteratorWrapper[A](underlying : ju.Iterator[A]) extends Iterator[A] {
def hasNext = underlying.hasNext
def next = underlying.next
@@ -751,8 +836,8 @@ de> view of the argument.
extends ju.Dictionary[A, B] {
def size: Int = underlying.size
def isEmpty: Boolean = underlying.isEmpty
- def keys: ju.Enumeration[A] = asEnumeration(underlying.keysIterator)
- def elements: ju.Enumeration[B] = asEnumeration(underlying.valuesIterator)
+ def keys: ju.Enumeration[A] = asJavaEnumeration(underlying.keysIterator)
+ def elements: ju.Enumeration[B] = asJavaEnumeration(underlying.valuesIterator)
def get(key: AnyRef) = try {
underlying.get(key.asInstanceOf[A]) match {
case None => null.asInstanceOf[B]
@@ -800,7 +885,7 @@ de> view of the argument.
if (r != null) Some(r) else None
}
- def iterator = asIterator(underlying.keys) map (k => (k, underlying get k))
+ def iterator = enumerationAsScalaIterator(underlying.keys) map (k => (k, underlying get k))
override def clear() = underlying.clear()
}
diff --git a/src/library/scala/collection/JavaConverters.scala b/src/library/scala/collection/JavaConverters.scala
new file mode 100755
index 0000000000..cb4c56fd59
--- /dev/null
+++ b/src/library/scala/collection/JavaConverters.scala
@@ -0,0 +1,456 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2006-2010, 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><code>scala.collection.Iterable</code> <=> <code>java.lang.Iterable</code></li>
+ * <li><code>scala.collection.Iterator</code> <=> <code>java.util.Iterator</code></li>
+ * <li><code>scala.collection.mutable.Buffer</code> <=> <code>java.util.List</code></li>
+ * <li><code>scala.collection.mutable.Set</code> <=> <code>java.util.Set</code></li>
+ * <li><code>scala.collection.mutable.Map</code> <=> <code>java.util.Map</code></li>
+ * <li><code>scala.collection.mutable.ConcurrentMap</code> <=> <code>java.util.concurrent.ConcurrentMap</code></li>
+ * </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</b> scala.collection.JavaConverters._
+ *
+ * <b>val</b> sl = <b>new</b> scala.collection.mutable.ListBuffer[Int]
+ * <b>val</b> jl : java.util.List[Int] = sl.asJava
+ * <b>val</b> 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><code>scala.collection.Iterable</code> <=> <code>java.util.Collection</code></li>
+ * <li><code>scala.collection.Iterator</code> <=> <code>java.util.Enumeration</code></li>
+ * <li><code>scala.collection.mutable.Map</code> <=> <code>java.util.Dictionary</code></li>
+ * </ul>
+ * In addition, the following one way conversions are provided via `asJava`:
+ * </p>
+ * <ul>
+ * <li><code>scala.collection.Seq => <code>java.util.List }</code></li>
+ * <li><code>scala.collection.mutable.Seq => <code>java.util.List</code></li>
+ * <li><code>scala.collection.Set</code> => <code>java.util.Set</code></li>
+ * <li><code>scala.collection.Map</code> => <code>java.util.Map</code></li>
+ * </ul>
+ *
+ * @author Martin Odersky
+ * @since 2.8.1
+ */
+object JavaConverters {
+ import java.{ lang => jl, util => ju }
+ import java.util.{ concurrent => juc }
+ import JavaConversions._
+
+ // 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</code> to a Java <code>Iterator</code>.
+ * The returned Java <code>Iterator</code> is backed by the provided Scala
+ * <code>Iterator</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asIterator(java.util.Iterator)</code> then the original
+ * Java <code>Iterator</code> will be returned by the `asJava` method.
+ *
+ * @param i The <code>Iterator</code> to be converted.
+ * @return An object with an `asJava` method that returns a Java <code>Iterator</code> 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</code> to a Java <code>Enumeration</code>.
+ * The returned Java <code>Enumeration</code> is backed by the provided Scala
+ * <code>Iterator</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asIterator(java.util.Enumeration)</code> then the
+ * original Java <code>Enumeration</code> will be returned.
+ *
+ * @param i The <code>Iterator</code> to be converted.
+ * @return An object with an `asJavaEnumeration` method that returns a Java <code>Enumeration</code> 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</code> to a Java <code>Iterable</code>.
+ * The returned Java <code>Iterable</code> is backed by the provided Scala
+ * <code>Iterable</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asIterable(java.lang.Iterable)</code> then the original
+ * Java <code>Iterable</code> will be returned.
+ *
+ * @param i The <code>Iterable</code> to be converted.
+ * @return An object with an `asJavaCollection` method that returns a Java <code>Iterable</code> 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</code> to an immutable Java
+ * <code>Collection</code>.
+ * <p>
+ * If the Scala <code>Iterable</code> was previously obtained from an implicit or
+ * explicit call of <code>asSizedIterable(java.util.Collection)</code> then the original
+ * Java <code>Collection</code> will be returned.
+ *
+ * @param i The <code>SizedIterable</code> to be converted.
+ * @return An object with an `asJava` method that returns a Java <code>Collection</code> 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</code> to a Java <code>List</code>.
+ * The returned Java <code>List</code> is backed by the provided Scala
+ * <code>Buffer</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asBuffer(java.util.List)</code> then the original
+ * Java <code>List</code> will be returned.
+ *
+ * @param b The <code>Buffer</code> to be converted.
+ * @return An object with an `asJava` method that returns a Java <code>List</code> view of the argument.
+ */
+ implicit def asJavaListConverter[A](b : mutable.Buffer[A]): AsJava[ju.List[A]] =
+ new AsJava(asJavaList(b))
+
+ /**
+ * Adds an `asJava` method that implicitly converts a Scala mutable <code>Seq</code> to a Java <code>List</code>.
+ * The returned Java <code>List</code> is backed by the provided Scala
+ * <code>Seq</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asSeq(java.util.List)</code> then the original
+ * Java <code>List</code> will be returned.
+ *
+ * @param b The <code>Seq</code> to be converted.
+ * @return An object with an `asJava` method that returns a Java <code>List</code> view of the argument.
+ */
+ implicit def asJavaListConverter[A](b : mutable.Seq[A]): AsJava[ju.List[A]] =
+ new AsJava(asJavaList(b))
+
+ /**
+ * Adds an `asJava` method that implicitly converts a Scala <code>Seq</code> to a Java <code>List</code>.
+ * The returned Java <code>List</code> is backed by the provided Scala
+ * <code>Seq</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asSeq(java.util.List)</code> then the original
+ * Java <code>List</code> will be returned.
+ *
+ * @param b The <code>Seq</code> to be converted.
+ * @return An object with an `asJava` method that returns a Java <code>List</code> view of the argument.
+ */
+ implicit def asJavaListConverter[A](b : Seq[A]): AsJava[ju.List[A]] =
+ new AsJava(asJavaList(b))
+
+ /**
+ * Adds an `asJava` method that implicitly converts a Scala mutable <code>Set</code> to a Java <code>Set</code>.
+ * The returned Java <code>Set</code> is backed by the provided Scala
+ * <code>Set</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asSet(java.util.Set)</code> then the original
+ * Java <code>Set</code> will be returned.
+ *
+ * @param s The <code>Set</code> to be converted.
+ * @return An object with an `asJava` method that returns a Java <code>Set</code> view of the argument.
+ */
+ implicit def asJavaSetConverter[A](s : mutable.Set[A]): AsJava[ju.Set[A]] =
+ new AsJava(asJavaSet(s))
+
+ /**
+ * Adds an `asJava` method that implicitly converts a Scala <code>Set</code> to a Java <code>Set</code>.
+ * The returned Java <code>Set</code> is backed by the provided Scala
+ * <code>Set</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asSet(java.util.Set)</code> then the original
+ * Java <code>Set</code> will be returned.
+ *
+ * @param s The <code>Set</code> to be converted.
+ * @return An object with an `asJava` method that returns a Java <code>Set</code> view of the argument.
+ */
+ implicit def asJavaSetConverter[A](s : Set[A]): AsJava[ju.Set[A]] =
+ new AsJava(asJavaSet(s))
+
+ /**
+ * Adds an `asJava` method that implicitly converts a Scala mutable <code>Map</code> to a Java <code>Map</code>.
+ * The returned Java <code>Map</code> is backed by the provided Scala
+ * <code>Map</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asMap(java.util.Map)</code> then the original
+ * Java <code>Map</code> will be returned.
+ *
+ * @param m The <code>Map</code> to be converted.
+ * @return An object with an `asJava` method that returns a Java <code>Map</code> view of the argument.
+ */
+ implicit def asJavaMapConverter[A, B](m : mutable.Map[A, B]): AsJava[ju.Map[A, B]] =
+ new AsJava(asJavaMap(m))
+
+ /**
+ * Adds an `asJavaDictionary` method that implicitly converts a Scala mutable <code>Map</code> to a Java <code>Dictionary</code>.
+ * The returned Java <code>Dictionary</code> is backed by the provided Scala
+ * <code>Dictionary</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asMap(java.util.Dictionary)</code> then the original
+ * Java <code>Dictionary</code> will be returned.
+ *
+ * @param m The <code>Map</code> to be converted.
+ * @return An object with an `asJavaDictionary` method that returns a Java <code>Dictionary</code> 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</code> to a Java <code>Map</code>.
+ * The returned Java <code>Map</code> is backed by the provided Scala
+ * <code>Map</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asMap(java.util.Map)</code> then the original
+ * Java <code>Map</code> will be returned.
+ *
+ * @param m The <code>Map</code> to be converted.
+ * @return An object with an `asJava` method that returns a Java <code>Map</code> view of the argument.
+ */
+ implicit def asJavaMapConverter[A, B](m : Map[A, B]): AsJava[ju.Map[A, B]] =
+ new AsJava(asJavaMap(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</code> was previously obtained from an implicit or
+ * explicit call of <code>asConcurrentMap(java.util.concurrect.ConcurrentMap)</code> then the original
+ * Java <code>ConcurrentMap</code> will be returned.
+ *
+ * @param m The <code>ConcurrentMap</code> to be converted.
+ * @return An object with an `asJava` method that returns a Java <code>ConcurrentMap</code> 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</code> to a Scala <code>Iterator</code>.
+ * The returned Scala <code>Iterator</code> is backed by the provided Java
+ * <code>Iterator</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asIterator(scala.collection.Iterator)</code> then the original
+ * Scala <code>Iterator</code> will be returned.
+ *
+ * @param i The <code>Iterator</code> to be converted.
+ * @return An object with an `asScala` method that returns a Scala <code>Iterator</code> 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</code> to a Scala <code>Iterator</code>.
+ * The returned Scala <code>Iterator</code> is backed by the provided Java
+ * <code>Enumeration</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asEnumeration(scala.collection.Iterator)</code> then the
+ * original Scala <code>Iterator</code> will be returned.
+ *
+ * @param i The <code>Enumeration</code> to be converted.
+ * @return An object with an `asScala` method that returns a Scala <code>Iterator</code> 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</code> to a Scala <code>Iterable</code>.
+ * The returned Scala <code>Iterable</code> is backed by the provided Java
+ * <code>Iterable</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asIterable(scala.collection.Iterable)</code> then the original
+ * Scala <code>Iterable</code> will be returned.
+ *
+ * @param i The <code>Iterable</code> to be converted.
+ * @return An object with an `asScala` method that returns a Scala <code>Iterable</code> view of the argument.
+ */
+ implicit def asScalaIterableConverter[A](i : jl.Iterable[A]): AsScala[Iterable[A]] =
+ new AsScala(asScalaIterable(i))
+
+ /**
+ * Adds an `asScala` method that implicitly converts a Java <code>Collection</code> to an Scala <code>Iterable</code>.
+ * <p>
+ * If the Java <code>Collection</code> was previously obtained from an implicit or
+ * explicit call of <code>asCollection(scala.collection.SizedIterable)</code> then
+ * the original Scala <code>SizedIterable</code> will be returned.
+ *
+ * @param i The <code>Collection</code> to be converted.
+ * @return An object with an `asScala` method that returns a Scala <code>SizedIterable</code> view of the argument.
+ */
+ implicit def asScalaIterableConverter[A](i : ju.Collection[A]): AsScala[Iterable[A]] =
+ new AsScala(asScalaIterable(i))
+
+ /**
+ * Adds an `asScala` method that implicitly converts a Java <code>List</code> to a Scala mutable <code>Buffer</code>.
+ * The returned Scala <code>Buffer</code> is backed by the provided Java
+ * <code>List</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asList(scala.collection.mutable.Buffer)</code> then the original
+ * Scala <code>Buffer</code> will be returned.
+ *
+ * @param l The <code>List</code> to be converted.
+ * @return An object with an `asScala` method that returns a Scala mutable <code>Buffer</code> 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</code> to a Scala mutable <code>Set</code>.
+ * The returned Scala <code>Set</code> is backed by the provided Java
+ * <code>Set</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asSet(scala.collection.mutable.Set)</code> then the original
+ * ScalaThe reported problems have to do with dependent method types, which is currently an experimental feature in Scala and is still under development. We emphasize that these problems are related to type-inference and, as stated in the paper, it is possible to run and type-check the programs with additional annotations. <code>Set</code> will be returned.
+ *
+ * @param s The <code>Set</code> to be converted.
+ * @return An object with an `asScala` method that returns a Scala mutable <code>Set</code> 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</code> to a Scala mutable <code>Map</code>.
+ * The returned Scala <code>Map</code> is backed by the provided Java
+ * <code>Map</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asMap(scala.collection.mutable.Map)</code> then the original
+ * Scala <code>Map</code> will be returned.
+ *
+ * @param m The <code>Map</code> to be converted.
+ * @return An object with an `asScala` method that returns a Scala mutable <code>Map</code> view of the argument.
+ */
+ implicit def asScalaMapConverter[A, B](m : ju.Map[A, B]): AsScala[mutable.Map[A, B]] =
+ new AsScala(asScalaMap(m))
+
+ /**
+ * Adds an `asScala` method that implicitly converts a Java <code>ConcurrentMap</code> to a Scala mutable <code>ConcurrentMap</code>.
+ * The returned Scala <code>ConcurrentMap</code> is backed by the provided Java
+ * <code>ConcurrentMap</code> 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</code> was previously obtained from an implicit or
+ * explicit call of <code>asConcurrentMap(scala.collection.mutable.ConcurrentMap)</code> then the original
+ * Scala <code>ConcurrentMap</code> will be returned.
+ *
+ * @param m The <code>ConcurrentMap</code> to be converted.
+ * @return An object with an `asScala` method that returns a Scala mutable <code>ConcurrrentMap</code> 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</code> to a Scala mutable <code>Map[String, String]</code>.
+ * The returned Scala <code>Map[String, String]</code> is backed by the provided Java
+ * <code>Dictionary</code> 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</code> to be converted.
+ * @return An object with an `asScala` method that returns a Scala mutable <code>Map[String, String]</code> 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</code> to a Scala mutable <code>Map[String, String]</code>.
+ * The returned Scala <code>Map[String, String]</code> is backed by the provided Java
+ * <code>Properties</code> 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</code> to be converted.
+ * @return An object with an `asScala` method that returns a Scala mutable <code>Map[String, String]</code> view of the argument.
+ */
+ implicit def asScalaMapConverter(p: ju.Properties): AsScala[mutable.Map[String, String]] =
+ new AsScala(asScalaMap(p))
+
+}