From 5d555ef90f443e20d2e46c668e456df0a643dae8 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sat, 17 Mar 2012 21:12:51 -0700 Subject: Overhaul of JavaConver{sions,ters}. Initially motivated by SI-5580, then just motivated. I broke up the opaquely named JavaConversions and JavaConverters into the following traits encapsulating some permutation of { to java, to scala, bidirectional } { wrappers, decorators } I named everything consistently in terms of either Wrappers or Decorators. Decorators install those asJava/asScala methods onto collections of the right kind; Wrappers hide the process. JavaConversions then reduces to an object which (ill-advisedly) extends both WrapAsJava and WrapAsScala. And JavaConverters is an object extending DecorateAsScala and DecorateAsJava. However other more clearly named vals exist in the newly created scala.collection.convert package object. val decorateAsJava = new DecorateAsJava { } val decorateAsScala = new DecorateAsScala { } val decorateAll = new DecorateAsJava with DecorateAsScala { } val wrapAsJava = new WrapAsJava { } val wrapAsScala = new WrapAsScala { } val wrapAll = new WrapAsJava with WrapAsScala { } So for instance to import asScala decorators, and only those: scala> import scala.collection.convert.decorateAsScala._ import scala.collection.convert.decorateAsScala._ scala> new java.util.ArrayList[String].asScala groupBy (x => x) res0: scala.collection.immutable.Map[String,scala.collection.mutable.Buffer[String]] = Map() I propose we put those vals or a subset of them in the scala package object rather than way down in scala.collection.convert. --- .../scala/collection/convert/DecorateAsJava.scala | 296 +++++++++++++++ .../scala/collection/convert/DecorateAsScala.scala | 189 +++++++++ .../scala/collection/convert/Decorators.scala | 46 +++ .../scala/collection/convert/WrapAsJava.scala | 256 +++++++++++++ .../scala/collection/convert/WrapAsScala.scala | 193 ++++++++++ .../scala/collection/convert/Wrappers.scala | 422 +++++++++++++++++++++ src/library/scala/collection/convert/package.scala | 18 + 7 files changed, 1420 insertions(+) create mode 100644 src/library/scala/collection/convert/DecorateAsJava.scala create mode 100644 src/library/scala/collection/convert/DecorateAsScala.scala create mode 100644 src/library/scala/collection/convert/Decorators.scala create mode 100644 src/library/scala/collection/convert/WrapAsJava.scala create mode 100644 src/library/scala/collection/convert/WrapAsScala.scala create mode 100644 src/library/scala/collection/convert/Wrappers.scala create mode 100644 src/library/scala/collection/convert/package.scala (limited to 'src/library/scala/collection/convert') diff --git a/src/library/scala/collection/convert/DecorateAsJava.scala b/src/library/scala/collection/convert/DecorateAsJava.scala new file mode 100644 index 0000000000..76837d937c --- /dev/null +++ b/src/library/scala/collection/convert/DecorateAsJava.scala @@ -0,0 +1,296 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.collection +package convert + +import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } +import Decorators._ +import WrapAsJava._ + +/** A collection of decorators that allow to convert between + * Scala and Java collections using `asScala` and `asJava` methods. + * + * The following conversions are supported via `asJava`, `asScala` + * + * - `scala.collection.Iterable` <=> `java.lang.Iterable` + * - `scala.collection.Iterator` <=> `java.util.Iterator` + * - `scala.collection.mutable.Buffer` <=> `java.util.List` + * - `scala.collection.mutable.Set` <=> `java.util.Set` + * - `scala.collection.mutable.Map` <=> `java.util.Map` + * - `scala.collection.mutable.ConcurrentMap` <=> `java.util.concurrent.ConcurrentMap` + * + * In all cases, converting from a source type to a target type and back + * again will return the original source object, e.g. + * {{{ + * import scala.collection.JavaConverters._ + * + * val sl = new scala.collection.mutable.ListBuffer[Int] + * val jl : java.util.List[Int] = sl.asJava + * val sl2 : scala.collection.mutable.Buffer[Int] = jl.asScala + * assert(sl eq sl2) + * }}} + * The following conversions also are supported, but the + * direction Scala to Java is done my a more specifically named method: + * `asJavaCollection`, `asJavaEnumeration`, `asJavaDictionary`. + * + * - `scala.collection.Iterable` <=> `java.util.Collection` + * - `scala.collection.Iterator` <=> `java.util.Enumeration` + * - `scala.collection.mutable.Map` <=> `java.util.Dictionary` + * + * In addition, the following one way conversions are provided via `asJava`: + * + * - `scala.collection.Seq` => `java.util.List` + * - `scala.collection.mutable.Seq` => `java.util.List` + * - `scala.collection.Set` => `java.util.Set` + * - `scala.collection.Map` => `java.util.Map` + * + * @author Martin Odersky + * @since 2.8.1 + */ + +trait DecorateAsJava { + /** + * Adds an `asJava` method that implicitly converts a Scala `Iterator` to a + * Java `Iterator`. The returned Java `Iterator` is backed by the provided Scala + * `Iterator` and any side-effects of using it via the Java interface will + * be visible via the Scala interface and vice versa. + * + * If the Scala `Iterator` was previously obtained from an implicit or explicit + * call of `asIterator(java.util.Iterator)` then the original Java `Iterator` + * will be returned by the `asJava` method. + * + * @param i The `Iterator` to be converted. + * @return An object with an `asJava` method that returns a Java `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 + * `Iterator` to a Java `Enumeration`. The returned Java `Enumeration` is + * backed by the provided Scala `Iterator` and any side-effects of using + * it via the Java interface will be visible via the Scala interface and + * vice versa. + * + * If the Scala `Iterator` was previously obtained from an implicit or + * explicit call of `asIterator(java.util.Enumeration)` then the + * original Java `Enumeration` will be returned. + * + * @param i The `Iterator` to be converted. + * @return An object with an `asJavaEnumeration` method that returns a Java + * `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 `Iterable` to + * a Java `Iterable`. + * + * The returned Java `Iterable` is backed by the provided Scala `Iterable` + * and any side-effects of using it via the Java interface will be visible + * via the Scala interface and vice versa. + * + * If the Scala `Iterable` was previously obtained from an implicit or + * explicit call of `asIterable(java.lang.Iterable)` then the original + * Java `Iterable` will be returned. + * + * @param i The `Iterable` to be converted. + * @return An object with an `asJavaCollection` method that returns a Java + * `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 + * `Iterable` to an immutable Java `Collection`. + * + * If the Scala `Iterable` was previously obtained from an implicit or + * explicit call of `asSizedIterable(java.util.Collection)` then the + * original Java `Collection` will be returned. + * + * @param i The `SizedIterable` to be converted. + * @return An object with an `asJava` method that returns a Java + * `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 `Buffer` + * to a Java `List`. + * + * The returned Java `List` is backed by the provided Scala `Buffer` and any + * side-effects of using it via the Java interface will be visible via the + * Scala interface and vice versa. + * + * If the Scala `Buffer` was previously obtained from an implicit or explicit + * call of `asBuffer(java.util.List)` then the original Java `List` will be + * returned. + * + * @param b The `Buffer` to be converted. + * @return An object with an `asJava` method that returns a Java `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 `Seq` + * to a Java `List`. + * + * The returned Java `List` is backed by the provided Scala `Seq` and any + * side-effects of using it via the Java interface will be visible via the + * Scala interface and vice versa. + * + * If the Scala `Seq` was previously obtained from an implicit or explicit + * call of `asSeq(java.util.List)` then the original Java `List` will be + * returned. + * + * @param b The `Seq` to be converted. + * @return An object with an `asJava` method that returns a Java `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 `Seq` to a + * Java `List`. + * + * The returned Java `List` is backed by the provided Scala `Seq` and any + * side-effects of using it via the Java interface will be visible via the + * Scala interface and vice versa. + * + * If the Scala `Seq` was previously obtained from an implicit or explicit + * call of `asSeq(java.util.List)` then the original Java `List` will be + * returned. + * + * @param b The `Seq` to be converted. + * @return An object with an `asJava` method that returns a Java `List` + * view of the argument. + */ + implicit def seqAsJavaListConverter[A](b : Seq[A]): AsJava[ju.List[A]] = + new AsJava(seqAsJavaList(b)) + + /** + * Adds an `asJava` method that implicitly converts a Scala mutable `Set`> + * to a Java `Set`. + * + * The returned Java `Set` is backed by the provided Scala `Set` and any + * side-effects of using it via the Java interface will be visible via + * the Scala interface and vice versa. + * + * If the Scala `Set` was previously obtained from an implicit or explicit + * call of `asSet(java.util.Set)` then the original Java `Set` will be + * returned. + * + * @param s The `Set` to be converted. + * @return An object with an `asJava` method that returns a Java `Set` view + * of the argument. + */ + implicit def mutableSetAsJavaSetConverter[A](s : mutable.Set[A]): AsJava[ju.Set[A]] = + new AsJava(mutableSetAsJavaSet(s)) + + /** + * Adds an `asJava` method that implicitly converts a Scala `Set` to a + * Java `Set`. + * + * The returned Java `Set` is backed by the provided Scala `Set` and any + * side-effects of using it via the Java interface will be visible via + * the Scala interface and vice versa. + * + * If the Scala `Set` was previously obtained from an implicit or explicit + * call of `asSet(java.util.Set)` then the original Java `Set` will be + * returned. + * + * @param s The `Set` to be converted. + * @return An object with an `asJava` method that returns a Java `Set` view + * of the argument. + */ + implicit def setAsJavaSetConverter[A](s : Set[A]): AsJava[ju.Set[A]] = + new AsJava(setAsJavaSet(s)) + + /** + * Adds an `asJava` method that implicitly converts a Scala mutable `Map` + * to a Java `Map`. + * + * The returned Java `Map` is backed by the provided Scala `Map` and any + * side-effects of using it via the Java interface will be visible via the + * Scala interface and vice versa. + * + * If the Scala `Map` was previously obtained from an implicit or explicit + * call of `asMap(java.util.Map)` then the original Java `Map` will be + * returned. + * + * @param m The `Map` to be converted. + * @return An object with an `asJava` method that returns a Java `Map` view + * of the argument. + */ + implicit def mutableMapAsJavaMapConverter[A, B](m : mutable.Map[A, B]): AsJava[ju.Map[A, B]] = + new AsJava(mutableMapAsJavaMap(m)) + + /** + * Adds an `asJavaDictionary` method that implicitly converts a Scala + * mutable `Map` to a Java `Dictionary`. + * + * The returned Java `Dictionary` is backed by the provided Scala + * `Dictionary` and any side-effects of using it via the Java interface + * will be visible via the Scala interface and vice versa. + * + * If the Scala `Dictionary` was previously obtained from an implicit or + * explicit call of `asMap(java.util.Dictionary)` then the original + * Java `Dictionary` will be returned. + * + * @param m The `Map` to be converted. + * @return An object with an `asJavaDictionary` method that returns a + * Java `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 `Map` to + * a Java `Map`. + * + * The returned Java `Map` is backed by the provided Scala `Map` and any + * side-effects of using it via the Java interface will be visible via + * the Scala interface and vice versa. + * + * If the Scala `Map` was previously obtained from an implicit or explicit + * call of `asMap(java.util.Map)` then the original Java `Map` will be + * returned. + * + * @param m The `Map` to be converted. + * @return An object with an `asJava` method that returns a Java `Map` view + * of the argument. + */ + implicit def mapAsJavaMapConverter[A, B](m : Map[A, B]): AsJava[ju.Map[A, B]] = + new AsJava(mapAsJavaMap(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. + * + * If the Scala `ConcurrentMap` was previously obtained from an implicit or + * explicit call of `asConcurrentMap(java.util.concurrect.ConcurrentMap)` + * then the original Java `ConcurrentMap` will be returned. + * + * @param m The `ConcurrentMap` to be converted. + * @return An object with an `asJava` method that returns a Java + * `ConcurrentMap` view of the argument. + */ + implicit def asJavaConcurrentMapConverter[A, B](m: mutable.ConcurrentMap[A, B]): AsJava[juc.ConcurrentMap[A, B]] = + new AsJava(asJavaConcurrentMap(m)) +} diff --git a/src/library/scala/collection/convert/DecorateAsScala.scala b/src/library/scala/collection/convert/DecorateAsScala.scala new file mode 100644 index 0000000000..bb14228e67 --- /dev/null +++ b/src/library/scala/collection/convert/DecorateAsScala.scala @@ -0,0 +1,189 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.collection +package convert + +import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } +import Decorators._ +import WrapAsScala._ + +trait DecorateAsScala { + /** + * Adds an `asScala` method that implicitly converts a Java `Iterator` to + * a Scala `Iterator`. + * + * The returned Scala `Iterator` is backed by the provided Java `Iterator` + * and any side-effects of using it via the Scala interface will be visible + * via the Java interface and vice versa. + * + * If the Java `Iterator` was previously obtained from an implicit or + * explicit call of `asIterator(scala.collection.Iterator)` then the + * original Scala `Iterator` will be returned. + * + * @param i The `Iterator` to be converted. + * @return An object with an `asScala` method that returns a Scala + * `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 `Enumeration` + * to a Scala `Iterator`. + * + * The returned Scala `Iterator` is backed by the provided Java + * `Enumeration` and any side-effects of using it via the Scala interface + * will be visible via the Java interface and vice versa. + * + * If the Java `Enumeration` was previously obtained from an implicit or + * explicit call of `asEnumeration(scala.collection.Iterator)` then the + * original Scala `Iterator` will be returned. + * + * @param i The `Enumeration` to be converted. + * @return An object with an `asScala` method that returns a Scala + * `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 `Iterable` to + * a Scala `Iterable`. + * + * The returned Scala `Iterable` is backed by the provided Java `Iterable` + * and any side-effects of using it via the Scala interface will be visible + * via the Java interface and vice versa. + * + * If the Java `Iterable` was previously obtained from an implicit or + * explicit call of `asIterable(scala.collection.Iterable)` then the original + * Scala `Iterable` will be returned. + * + * @param i The `Iterable` to be converted. + * @return An object with an `asScala` method that returns a Scala `Iterable` + * view of the argument. + */ + implicit def iterableAsScalaIterableConverter[A](i : jl.Iterable[A]): AsScala[Iterable[A]] = + new AsScala(iterableAsScalaIterable(i)) + + /** + * Adds an `asScala` method that implicitly converts a Java `Collection` to + * an Scala `Iterable`. + * + * If the Java `Collection` was previously obtained from an implicit or + * explicit call of `asCollection(scala.collection.SizedIterable)` then + * the original Scala `SizedIterable` will be returned. + * + * @param i The `Collection` to be converted. + * @return An object with an `asScala` method that returns a Scala + * `SizedIterable` view of the argument. + */ + implicit def collectionAsScalaIterableConverter[A](i : ju.Collection[A]): AsScala[Iterable[A]] = + new AsScala(collectionAsScalaIterable(i)) + + /** + * Adds an `asScala` method that implicitly converts a Java `List` to a + * Scala mutable `Buffer`. + * + * The returned Scala `Buffer` is backed by the provided Java `List` and + * any side-effects of using it via the Scala interface will be visible via + * the Java interface and vice versa. + * + * If the Java `List` was previously obtained from an implicit or explicit + * call of `asList(scala.collection.mutable.Buffer)` then the original + * Scala `Buffer` will be returned. + * + * @param l The `List` to be converted. + * @return An object with an `asScala` method that returns a Scala mutable + * `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 `Set` to a + * Scala mutable `Set`. + * + * The returned Scala `Set` is backed by the provided Java `Set` and any + * side-effects of using it via the Scala interface will be visible via + * the Java interface and vice versa. + * + * If the Java `Set` was previously obtained from an implicit or explicit + * call of `asSet(scala.collection.mutable.Set)` then the original + * Scala `Set` will be returned. + * + * @param s The `Set` to be converted. + * @return An object with an `asScala` method that returns a Scala mutable + * `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 `Map` to a Scala + * mutable `Map`. The returned Scala `Map` is backed by the provided Java + * `Map` and any side-effects of using it via the Scala interface will + * be visible via the Java interface and vice versa. + * + * If the Java `Map` was previously obtained from an implicit or explicit + * call of `asMap(scala.collection.mutable.Map)` then the original + * Scala `Map` will be returned. + * + * @param m The `Map` to be converted. + * @return An object with an `asScala` method that returns a Scala mutable + * `Map` view of the argument. + */ + implicit def mapAsScalaMapConverter[A, B](m : ju.Map[A, B]): AsScala[mutable.Map[A, B]] = + new AsScala(mapAsScalaMap(m)) + + /** + * Adds an `asScala` method that implicitly converts a Java `ConcurrentMap` + * to a Scala mutable `ConcurrentMap`. The returned Scala `ConcurrentMap` is + * backed by the provided Java `ConcurrentMap` and any side-effects of using + * it via the Scala interface will be visible via the Java interface and + * vice versa. + * + * If the Java `ConcurrentMap` was previously obtained from an implicit or + * explicit call of `asConcurrentMap(scala.collection.mutable.ConcurrentMap)` + * then the original Scala `ConcurrentMap` will be returned. + * + * @param m The `ConcurrentMap` to be converted. + * @return An object with an `asScala` method that returns a Scala mutable + * `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 `Dictionary` + * to a Scala mutable `Map[String, String]`. The returned Scala + * `Map[String, String]` is backed by the provided Java `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 `Dictionary` to be converted. + * @return An object with an `asScala` method that returns a Scala mutable + * `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 `Properties` + * to a Scala mutable `Map[String, String]`. The returned Scala + * `Map[String, String]` is backed by the provided Java `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 `Properties` to be converted. + * @return An object with an `asScala` method that returns a Scala mutable + * `Map[String, String]` view of the argument. + */ + implicit def propertiesAsScalaMapConverter(p: ju.Properties): AsScala[mutable.Map[String, String]] = + new AsScala(propertiesAsScalaMap(p)) +} diff --git a/src/library/scala/collection/convert/Decorators.scala b/src/library/scala/collection/convert/Decorators.scala new file mode 100644 index 0000000000..3bdd9a0f1c --- /dev/null +++ b/src/library/scala/collection/convert/Decorators.scala @@ -0,0 +1,46 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.collection +package convert + +import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } + +private[collection] trait Decorators { + /** Generic class containing the `asJava` converter method */ + class AsJava[A](op: => A) { + /** Converts a Scala collection to the corresponding Java collection */ + def asJava: A = op + } + + /** Generic class containing the `asScala` converter method */ + class AsScala[A](op: => A) { + /** Converts a Java collection to the corresponding Scala collection */ + def asScala: A = 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) + } +} + +private[collection] object Decorators extends Decorators diff --git a/src/library/scala/collection/convert/WrapAsJava.scala b/src/library/scala/collection/convert/WrapAsJava.scala new file mode 100644 index 0000000000..6274518d1a --- /dev/null +++ b/src/library/scala/collection/convert/WrapAsJava.scala @@ -0,0 +1,256 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.collection +package convert + +import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } +import Wrappers._ + +trait WrapAsJava { + /** + * Implicitly converts a Scala Iterator to a Java Iterator. + * The returned Java Iterator is backed by the provided Scala + * Iterator and any side-effects of using it via the Java interface will + * be visible via the Scala interface and vice versa. + * + * If the Scala Iterator was previously obtained from an implicit or + * explicit call of `asIterator(java.util.Iterator)` then the original + * Java Iterator will be returned. + * + * @param i The Iterator to be converted. + * @return A Java Iterator view of the argument. + */ + implicit def asJavaIterator[A](it: Iterator[A]): ju.Iterator[A] = it match { + case JIteratorWrapper(wrapped) => wrapped.asInstanceOf[ju.Iterator[A]] + case _ => IteratorWrapper(it) + } + + /** + * Implicitly converts a Scala Iterator to a Java Enumeration. + * The returned Java Enumeration is backed by the provided Scala + * Iterator and any side-effects of using it via the Java interface will + * be visible via the Scala interface and vice versa. + * + * If the Scala Iterator was previously obtained from an implicit or + * explicit call of `asIterator(java.util.Enumeration)` then the + * original Java Enumeration will be returned. + * + * @param i The Iterator to be converted. + * @return A Java Enumeration view of the argument. + */ + implicit def asJavaEnumeration[A](it: Iterator[A]): ju.Enumeration[A] = it match { + case JEnumerationWrapper(wrapped) => wrapped.asInstanceOf[ju.Enumeration[A]] + case _ => IteratorWrapper(it) + } + + /** + * Implicitly converts a Scala Iterable to a Java Iterable. + * The returned Java Iterable is backed by the provided Scala + * Iterable and any side-effects of using it via the Java interface will + * be visible via the Scala interface and vice versa. + * + * If the Scala Iterable was previously obtained from an implicit or + * explicit call of `asIterable(java.lang.Iterable)` then the original + * Java Iterable will be returned. + * + * @param i The Iterable to be converted. + * @return A Java Iterable view of the argument. + */ + implicit def asJavaIterable[A](i: Iterable[A]): jl.Iterable[A] = i match { + case JIterableWrapper(wrapped) => wrapped.asInstanceOf[jl.Iterable[A]] + case _ => IterableWrapper(i) + } + + /** + * Implicitly converts a Scala Iterable to an immutable Java + * Collection. + * + * If the Scala Iterable was previously obtained from an implicit or + * explicit call of `asSizedIterable(java.util.Collection)` then the original + * Java Collection will be returned. + * + * @param i The SizedIterable to be converted. + * @return A Java Collection view of the argument. + */ + implicit def asJavaCollection[A](it: Iterable[A]): ju.Collection[A] = it match { + case JCollectionWrapper(wrapped) => wrapped.asInstanceOf[ju.Collection[A]] + case _ => new IterableWrapper(it) + } + + /** + * Implicitly converts a Scala mutable Buffer to a Java List. + * The returned Java List is backed by the provided Scala + * Buffer and any side-effects of using it via the Java interface will + * be visible via the Scala interface and vice versa. + * + * If the Scala Buffer was previously obtained from an implicit or + * explicit call of `asBuffer(java.util.List)` then the original + * Java List will be returned. + * + * @param b The Buffer to be converted. + * @return A Java List view of the argument. + */ + implicit def bufferAsJavaList[A](b: mutable.Buffer[A]): ju.List[A] = b match { + case JListWrapper(wrapped) => wrapped + case _ => new MutableBufferWrapper(b) + } + + /** + * Implicitly converts a Scala mutable Seq to a Java List. + * The returned Java List is backed by the provided Scala + * Seq and any side-effects of using it via the Java interface will + * be visible via the Scala interface and vice versa. + * + * If the Scala Seq was previously obtained from an implicit or + * explicit call of `asSeq(java.util.List)` then the original + * Java List will be returned. + * + * @param b The Seq to be converted. + * @return A Java List view of the argument. + */ + implicit def mutableSeqAsJavaList[A](seq: mutable.Seq[A]): ju.List[A] = seq match { + case JListWrapper(wrapped) => wrapped + case _ => new MutableSeqWrapper(seq) + } + + /** + * Implicitly converts a Scala Seq to a Java List. + * The returned Java List is backed by the provided Scala + * Seq and any side-effects of using it via the Java interface will + * be visible via the Scala interface and vice versa. + * + * If the Scala Seq was previously obtained from an implicit or + * explicit call of `asSeq(java.util.List)` then the original + * Java List will be returned. + * + * @param b The Seq to be converted. + * @return A Java List view of the argument. + */ + implicit def seqAsJavaList[A](seq: Seq[A]): ju.List[A] = seq match { + case JListWrapper(wrapped) => wrapped.asInstanceOf[ju.List[A]] + case _ => new SeqWrapper(seq) + } + + /** + * Implicitly converts a Scala mutable Set to a Java Set. + * The returned Java Set is backed by the provided Scala + * Set and any side-effects of using it via the Java interface will + * be visible via the Scala interface and vice versa. + * + * If the Scala Set was previously obtained from an implicit or + * explicit call of `asSet(java.util.Set)` then the original + * Java Set will be returned. + * + * @param s The Set to be converted. + * @return A Java Set view of the argument. + */ + implicit def mutableSetAsJavaSet[A](s: mutable.Set[A]): ju.Set[A] = s match { + case JSetWrapper(wrapped) => wrapped + case _ => new MutableSetWrapper(s) + } + + /** + * Implicitly converts a Scala Set to a Java Set. + * The returned Java Set is backed by the provided Scala + * Set and any side-effects of using it via the Java interface will + * be visible via the Scala interface and vice versa. + * + * If the Scala Set was previously obtained from an implicit or + * explicit call of asSet(java.util.Set) then the original + * Java Set will be returned. + * + * @param s The Set to be converted. + * @return A Java Set view of the argument. + */ + implicit def setAsJavaSet[A](s: Set[A]): ju.Set[A] = s match { + case JSetWrapper(wrapped) => wrapped + case _ => new SetWrapper(s) + } + + /** + * Implicitly converts a Scala mutable Map to a Java Map. + * The returned Java Map is backed by the provided Scala + * Map and any side-effects of using it via the Java interface will + * be visible via the Scala interface and vice versa. + * + * If the Scala Map was previously obtained from an implicit or + * explicit call of `asMap(java.util.Map)` then the original + * Java Map will be returned. + * + * @param m The Map to be converted. + * @return A Java Map view of the argument. + */ + implicit def mutableMapAsJavaMap[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) + } + + /** + * Implicitly converts a Scala mutable `Map` to a Java `Dictionary`. + * + * The returned Java `Dictionary` is backed by the provided Scala + * `Dictionary` and any side-effects of using it via the Java interface + * will be visible via the Scala interface and vice versa. + * + * If the Scala `Dictionary` was previously obtained from an implicit or + * explicit call of `asMap(java.util.Dictionary)` then the original + * Java Dictionary will be returned. + * + * @param m The `Map` to be converted. + * @return A Java `Dictionary` view of the argument. + */ + 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 Scala `Map` to a Java `Map`. + * + * The returned Java `Map` is backed by the provided Scala `Map` and + * any side-effects of using it via the Java interface will be visible + * via the Scala interface and vice versa. + * + * If the Scala `Map` was previously obtained from an implicit or + * explicit call of `asMap(java.util.Map)` then the original + * Java `Map` will be returned. + * + * @param m The `Map` to be converted. + * @return A Java `Map` view of the argument. + */ + implicit def mapAsJavaMap[A, B](m: Map[A, B]): ju.Map[A, B] = m match { + //case JConcurrentMapWrapper(wrapped) => wrapped + case JMapWrapper(wrapped) => wrapped.asInstanceOf[ju.Map[A, B]] + case _ => new MapWrapper(m) + } + + /** + * 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. + * + * If the Scala `ConcurrentMap` was previously obtained from an implicit or + * explicit call of `asConcurrentMap(java.util.concurrect.ConcurrentMap)` + * then the original Java ConcurrentMap will be returned. + * + * @param m The `ConcurrentMap` to be converted. + * @return A Java `ConcurrentMap` view of the argument. + */ + implicit def asJavaConcurrentMap[A, B](m: mutable.ConcurrentMap[A, B]): juc.ConcurrentMap[A, B] = m match { + case JConcurrentMapWrapper(wrapped) => wrapped + case _ => new ConcurrentMapWrapper(m) + } +} + +object WrapAsJava extends WrapAsJava { } diff --git a/src/library/scala/collection/convert/WrapAsScala.scala b/src/library/scala/collection/convert/WrapAsScala.scala new file mode 100644 index 0000000000..02b58f55a4 --- /dev/null +++ b/src/library/scala/collection/convert/WrapAsScala.scala @@ -0,0 +1,193 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.collection +package convert + +import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } +import Wrappers._ + +trait WrapAsScala { + /** + * Implicitly converts a Java `Iterator` to a Scala `Iterator`. + * + * The returned Scala `Iterator` is backed by the provided Java `Iterator` + * and any side-effects of using it via the Scala interface will be visible + * via the Java interface and vice versa. + * + * If the Java `Iterator` was previously obtained from an implicit or + * explicit call of `asIterator(scala.collection.Iterator)` then the + * original Scala `Iterator` will be returned. + * + * @param i The `Iterator` to be converted. + * @return A Scala `Iterator` view of the argument. + */ + implicit def asScalaIterator[A](it: ju.Iterator[A]): Iterator[A] = it match { + case IteratorWrapper(wrapped) => wrapped + case _ => JIteratorWrapper(it) + } + + /** + * Implicitly converts a Java Enumeration to a Scala Iterator. + * The returned Scala Iterator is backed by the provided Java + * Enumeration and any side-effects of using it via the Scala interface will + * be visible via the Java interface and vice versa. + * + * If the Java Enumeration was previously obtained from an implicit or + * explicit call of `enumerationAsScalaIterator(scala.collection.Iterator)` + * then the original Scala Iterator will be returned. + * + * @param i The Enumeration to be converted. + * @return A Scala Iterator view of the argument. + */ + implicit def enumerationAsScalaIterator[A](i: ju.Enumeration[A]): Iterator[A] = i match { + case IteratorWrapper(wrapped) => wrapped + case _ => JEnumerationWrapper(i) + } + + /** + * Implicitly converts a Java `Iterable` to a Scala `Iterable`. + * + * The returned Scala `Iterable` is backed by the provided Java `Iterable` + * and any side-effects of using it via the Scala interface will be visible + * via the Java interface and vice versa. + * + * If the Java `Iterable` was previously obtained from an implicit or + * explicit call of `iterableAsScalaIterable(scala.collection.Iterable)` + * then the original Scala Iterable will be returned. + * + * @param i The Iterable to be converted. + * @return A Scala Iterable view of the argument. + */ + implicit def iterableAsScalaIterable[A](i: jl.Iterable[A]): Iterable[A] = i match { + case IterableWrapper(wrapped) => wrapped + case _ => JIterableWrapper(i) + } + + /** + * Implicitly converts a Java `Collection` to an Scala `Iterable`. + * + * If the Java `Collection` was previously obtained from an implicit or + * explicit call of `collectionAsScalaIterable(scala.collection.SizedIterable)` + * then the original Scala `Iterable` will be returned. + * + * @param i The Collection to be converted. + * @return A Scala Iterable view of the argument. + */ + implicit def collectionAsScalaIterable[A](i: ju.Collection[A]): Iterable[A] = i match { + case IterableWrapper(wrapped) => wrapped + case _ => JCollectionWrapper(i) + } + + /** + * Implicitly converts a Java `List` to a Scala mutable `Buffer`. + * + * The returned Scala `Buffer` is backed by the provided Java `List` + * and any side-effects of using it via the Scala interface will + * be visible via the Java interface and vice versa. + * + * If the Java `List` was previously obtained from an implicit or + * explicit call of `asScalaBuffer(scala.collection.mutable.Buffer)` + * then the original Scala `Buffer` will be returned. + * + * @param l The `List` to be converted. + * @return A Scala mutable `Buffer` view of the argument. + */ + implicit def asScalaBuffer[A](l: ju.List[A]): mutable.Buffer[A] = l match { + case MutableBufferWrapper(wrapped) => wrapped + case _ =>new JListWrapper(l) + } + + /** + * Implicitly converts a Java Set to a Scala mutable Set. + * The returned Scala Set is backed by the provided Java + * Set and any side-effects of using it via the Scala interface will + * be visible via the Java interface and vice versa. + * + * If the Java Set was previously obtained from an implicit or + * explicit call of `asScalaSet(scala.collection.mutable.Set)` then + * the original Scala Set will be returned. + * + * @param s The Set to be converted. + * @return A Scala mutable Set view of the argument. + */ + implicit def asScalaSet[A](s: ju.Set[A]): mutable.Set[A] = s match { + case MutableSetWrapper(wrapped) => wrapped + case _ =>new JSetWrapper(s) + } + + /** + * Implicitly converts a Java `Map` to a Scala mutable `Map`. + * + * The returned Scala `Map` is backed by the provided Java `Map` and any + * side-effects of using it via the Scala interface will be visible via + * the Java interface and vice versa. + * + * If the Java `Map` was previously obtained from an implicit or + * explicit call of `mapAsScalaMap(scala.collection.mutable.Map)` then + * the original Scala Map will be returned. + * + * @param m The Map to be converted. + * @return A Scala mutable Map view of the argument. + */ + implicit def mapAsScalaMap[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) + } + + /** + * Implicitly converts a Java ConcurrentMap to a Scala mutable ConcurrentMap. + * The returned Scala ConcurrentMap is backed by the provided Java + * ConcurrentMap and any side-effects of using it via the Scala interface will + * be visible via the Java interface and vice versa. + * + * If the Java ConcurrentMap was previously obtained from an implicit or + * explicit call of `asConcurrentMap(scala.collection.mutable.ConcurrentMap)` + * then the original Scala ConcurrentMap will be returned. + * + * @param m The ConcurrentMap to be converted. + * @return A Scala mutable ConcurrentMap view of the argument. + */ + 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) + } + + /** + * Implicitly converts a Java `Dictionary` to a Scala mutable + * `Map[String, String]`. + * + * The returned Scala `Map[String, String]` is backed by the provided Java + * `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 Dictionary to be converted. + * @return A Scala mutable Map[String, String] view of the argument. + */ + implicit def dictionaryAsScalaMap[A, B](p: ju.Dictionary[A, B]): mutable.Map[A, B] = p match { + case DictionaryWrapper(wrapped) => wrapped + case _ => new JDictionaryWrapper(p) + } + + /** + * Implicitly converts a Java `Properties` to a Scala `mutable Map[String, String]`. + * + * The returned Scala `Map[String, String]` is backed by the provided Java + * `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 Properties to be converted. + * @return A Scala mutable Map[String, String] view of the argument. + */ + implicit def propertiesAsScalaMap(p: ju.Properties): mutable.Map[String, String] = p match { + case _ => new JPropertiesWrapper(p) + } +} + +object WrapAsScala extends WrapAsScala { } diff --git a/src/library/scala/collection/convert/Wrappers.scala b/src/library/scala/collection/convert/Wrappers.scala new file mode 100644 index 0000000000..8136e462cb --- /dev/null +++ b/src/library/scala/collection/convert/Wrappers.scala @@ -0,0 +1,422 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.collection +package convert + +import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } +import WrapAsScala._ +import WrapAsJava._ + +/** Don't put the implementations in the same scope as the implicits + * which utilize them, or they will stow away into every scope which + * extends one of those implementations. See SI-5580. + */ +private[collection] trait Wrappers { + trait IterableWrapperTrait[A] extends ju.AbstractCollection[A] { + val underlying: Iterable[A] + def size = underlying.size + override def iterator = IteratorWrapper(underlying.iterator) + override def isEmpty = underlying.isEmpty + } + + case class IteratorWrapper[A](underlying: Iterator[A]) extends ju.Iterator[A] with ju.Enumeration[A] { + def hasNext = underlying.hasNext + def next() = underlying.next + def hasMoreElements = underlying.hasNext + def nextElement() = underlying.next + 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 AbstractIterator[A] with Iterator[A] { + def hasNext = underlying.hasNext + def next() = underlying.next + } + + case class JEnumerationWrapper[A](underlying: ju.Enumeration[A]) extends AbstractIterator[A] with Iterator[A] { + def hasNext = underlying.hasMoreElements + def next() = underlying.nextElement + } + + case class IterableWrapper[A](underlying: Iterable[A]) extends ju.AbstractCollection[A] with IterableWrapperTrait[A] { } + + case class JIterableWrapper[A](underlying: jl.Iterable[A]) extends AbstractIterable[A] with Iterable[A] { + def iterator = underlying.iterator + def newBuilder[B] = new mutable.ArrayBuffer[B] + } + + case class JCollectionWrapper[A](underlying: ju.Collection[A]) extends AbstractIterable[A] with Iterable[A] { + def iterator = underlying.iterator + override def size = underlying.size + override def isEmpty = underlying.isEmpty + def newBuilder[B] = new mutable.ArrayBuffer[B] + } + + case class SeqWrapper[A](underlying: Seq[A]) extends ju.AbstractList[A] with IterableWrapperTrait[A] { + def get(i: Int) = underlying(i) + } + + case class MutableSeqWrapper[A](underlying: mutable.Seq[A]) extends ju.AbstractList[A] with IterableWrapperTrait[A] { + def get(i: Int) = underlying(i) + override def set(i: Int, elem: A) = { + val p = underlying(i) + underlying(i) = elem + p + } + } + + case class MutableBufferWrapper[A](underlying: mutable.Buffer[A]) extends ju.AbstractList[A] with IterableWrapperTrait[A] { + def get(i: Int) = underlying(i) + override def set(i: Int, elem: A) = { val p = underlying(i); underlying(i) = elem; p } + override def add(elem: A) = { underlying append elem; true } + override def remove(i: Int) = underlying remove i + } + + case class JListWrapper[A](val underlying: ju.List[A]) extends mutable.AbstractBuffer[A] with mutable.Buffer[A] { + def length = underlying.size + override def isEmpty = underlying.isEmpty + override def iterator: Iterator[A] = underlying.iterator + def apply(i: Int) = underlying.get(i) + def update(i: Int, elem: A) = underlying.set(i, elem) + def +=:(elem: A) = { underlying.subList(0, 0) add elem; this } + def +=(elem: A): this.type = { underlying add elem; this } + def insertAll(i: Int, elems: Traversable[A]) = { + val ins = underlying.subList(0, i) + elems.seq.foreach(ins.add(_)) + } + def remove(i: Int) = underlying.remove(i) + def clear() = underlying.clear() + def result = this + } + + class SetWrapper[A](underlying: Set[A]) extends ju.AbstractSet[A] { + self => + def size = underlying.size + def iterator = new ju.Iterator[A] { + val ui = underlying.iterator + var prev: Option[A] = None + def hasNext = ui.hasNext + def next = { val e = ui.next; prev = Some(e); e } + def remove = prev match { + case Some(e) => + underlying match { + case ms: mutable.Set[a] => + ms remove e + prev = None + case _ => + throw new UnsupportedOperationException("remove") + } + case _ => + throw new IllegalStateException("next must be called at least once before remove") + } + } + } + + case class MutableSetWrapper[A](underlying: mutable.Set[A]) extends SetWrapper[A](underlying) { + override def add(elem: A) = { + val sz = underlying.size + underlying += elem + sz < underlying.size + } + override def remove(elem: AnyRef) = + try underlying remove elem.asInstanceOf[A] + catch { case ex: ClassCastException => false } + override def clear() = underlying.clear() + } + + case class JSetWrapper[A](underlying: ju.Set[A]) extends mutable.AbstractSet[A] with mutable.Set[A] with mutable.SetLike[A, JSetWrapper[A]] { + + override def size = underlying.size + + def iterator = underlying.iterator + + def contains(elem: A): Boolean = underlying.contains(elem) + + def +=(elem: A): this.type = { underlying add elem; this } + def -=(elem: A): this.type = { underlying remove elem; this } + + override def add(elem: A): Boolean = underlying add elem + override def remove(elem: A): Boolean = underlying remove elem + override def clear() = underlying.clear() + + override def empty = JSetWrapper(new ju.HashSet[A]) + } + + class MapWrapper[A, B](underlying: Map[A, B]) extends ju.AbstractMap[A, B] { self => + override def size = underlying.size + + override def get(key: AnyRef): B = try { + underlying get key.asInstanceOf[A] match { + case None => null.asInstanceOf[B] + case Some(v) => v + } + } catch { + case ex: ClassCastException => null.asInstanceOf[B] + } + + override def entrySet: ju.Set[ju.Map.Entry[A, B]] = new ju.AbstractSet[ju.Map.Entry[A, B]] { + def size = self.size + + def iterator = new ju.Iterator[ju.Map.Entry[A, B]] { + val ui = underlying.iterator + var prev : Option[A] = None + + def hasNext = ui.hasNext + + def next() = { + val (k, v) = ui.next + prev = Some(k) + new ju.Map.Entry[A, B] { + def getKey = k + def getValue = v + def setValue(v1 : B) = self.put(k, v1) + override def hashCode = k.hashCode + v.hashCode + override def equals(other: Any) = other match { + case e: ju.Map.Entry[_, _] => k == e.getKey && v == e.getValue + case _ => false + } + } + } + + def remove() { + prev match { + case Some(k) => + underlying match { + case mm: mutable.Map[a, _] => + mm remove k + prev = None + case _ => + throw new UnsupportedOperationException("remove") + } + case _ => + throw new IllegalStateException("next must be called at least once before remove") + } + } + } + } + } + + case class MutableMapWrapper[A, B](underlying: mutable.Map[A, B]) extends MapWrapper[A, B](underlying) { + override def put(k: A, v: B) = underlying.put(k, v) match { + case Some(v1) => v1 + case None => null.asInstanceOf[B] + } + + override def remove(k: AnyRef): B = try { + underlying remove k.asInstanceOf[A] match { + case None => null.asInstanceOf[B] + case Some(v) => v + } + } catch { + case ex: ClassCastException => null.asInstanceOf[B] + } + + override def clear() = underlying.clear() + } + + trait JMapWrapperLike[A, B, +Repr <: mutable.MapLike[A, B, Repr] with mutable.Map[A, B]] extends mutable.Map[A, B] with mutable.MapLike[A, B, Repr] { + def underlying: ju.Map[A, B] + + override def size = underlying.size + + def get(k: A) = { + val v = underlying get k + if (v != null) + Some(v) + else if (underlying containsKey k) + Some(null.asInstanceOf[B]) + else + None + } + + def +=(kv: (A, B)): this.type = { underlying.put(kv._1, kv._2); this } + def -=(key: A): this.type = { underlying remove key; this } + + override def put(k: A, v: B): Option[B] = { + val r = underlying.put(k, v) + if (r != null) Some(r) else None + } + + override def update(k: A, v: B) { underlying.put(k, v) } + + override def remove(k: A): Option[B] = { + val r = underlying remove k + if (r != null) Some(r) else None + } + + def iterator: Iterator[(A, B)] = new AbstractIterator[(A, B)] { + val ui = underlying.entrySet.iterator + def hasNext = ui.hasNext + def next() = { val e = ui.next(); (e.getKey, e.getValue) } + } + + override def clear() = underlying.clear() + + override def empty: Repr = null.asInstanceOf[Repr] + } + + case class JMapWrapper[A, B](val underlying : ju.Map[A, B]) extends mutable.AbstractMap[A, B] with JMapWrapperLike[A, B, JMapWrapper[A, B]] { + override def empty = JMapWrapper(new ju.HashMap[A, B]) + } + + class ConcurrentMapWrapper[A, B](override val underlying: mutable.ConcurrentMap[A, B]) extends MutableMapWrapper[A, B](underlying) with juc.ConcurrentMap[A, B] { + + def putIfAbsent(k: A, v: B) = underlying.putIfAbsent(k, v) match { + case Some(v) => v + case None => null.asInstanceOf[B] + } + + def remove(k: AnyRef, v: AnyRef) = try { + underlying.remove(k.asInstanceOf[A], v.asInstanceOf[B]) + } catch { + case ex: ClassCastException => + false + } + + def replace(k: A, v: B): B = underlying.replace(k, v) match { + case Some(v) => v + case None => null.asInstanceOf[B] + } + + def replace(k: A, oldval: B, newval: B) = underlying.replace(k, oldval, newval) + } + + case class JConcurrentMapWrapper[A, B](val underlying: juc.ConcurrentMap[A, B]) extends mutable.AbstractMap[A, B] with JMapWrapperLike[A, B, JConcurrentMapWrapper[A, B]] with mutable.ConcurrentMap[A, B] { + override def get(k: A) = { + val v = underlying get k + if (v != null) Some(v) + else None + } + + override def empty = new JConcurrentMapWrapper(new juc.ConcurrentHashMap[A, B]) + + def putIfAbsent(k: A, v: B): Option[B] = { + val r = underlying.putIfAbsent(k, v) + if (r != null) Some(r) else None + } + + def remove(k: A, v: B): Boolean = underlying.remove(k, v) + + def replace(k: A, v: B): Option[B] = { + val prev = underlying.replace(k, v) + if (prev != null) Some(prev) else None + } + + def replace(k: A, oldvalue: B, newvalue: B): Boolean = + underlying.replace(k, oldvalue, newvalue) + } + + case class DictionaryWrapper[A, B](underlying: mutable.Map[A, B]) extends ju.Dictionary[A, B] { + def size: Int = underlying.size + def isEmpty: Boolean = underlying.isEmpty + 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] + case Some(v) => v + } + } catch { + case ex: ClassCastException => null.asInstanceOf[B] + } + def put(key: A, value: B): B = underlying.put(key, value) match { + case Some(v) => v + case None => null.asInstanceOf[B] + } + override def remove(key: AnyRef) = try { + underlying remove key.asInstanceOf[A] match { + case None => null.asInstanceOf[B] + case Some(v) => v + } + } catch { + case ex: ClassCastException => null.asInstanceOf[B] + } + } + + case class JDictionaryWrapper[A, B](underlying: ju.Dictionary[A, B]) extends mutable.AbstractMap[A, B] with mutable.Map[A, B] { + override def size: Int = underlying.size + + def get(k: A) = { + val v = underlying get k + if (v != null) Some(v) else None + } + + def +=(kv: (A, B)): this.type = { underlying.put(kv._1, kv._2); this } + def -=(key: A): this.type = { underlying remove key; this } + + override def put(k: A, v: B): Option[B] = { + val r = underlying.put(k, v) + if (r != null) Some(r) else None + } + + override def update(k: A, v: B) { underlying.put(k, v) } + + override def remove(k: A): Option[B] = { + val r = underlying remove k + if (r != null) Some(r) else None + } + + def iterator = enumerationAsScalaIterator(underlying.keys) map (k => (k, underlying get k)) + + override def clear() = underlying.clear() + } + + case class JPropertiesWrapper(underlying: ju.Properties) extends mutable.AbstractMap[String, String] + with mutable.Map[String, String] + with mutable.MapLike[String, String, JPropertiesWrapper] { + + override def size = underlying.size + + def get(k: String) = { + val v = underlying get k + if (v != null) Some(v.asInstanceOf[String]) else None + } + + def +=(kv: (String, String)): this.type = { underlying.put(kv._1, kv._2); this } + def -=(key: String): this.type = { underlying remove key; this } + + override def put(k: String, v: String): Option[String] = { + val r = underlying.put(k, v) + if (r != null) Some(r.asInstanceOf[String]) else None + } + + override def update(k: String, v: String) { underlying.put(k, v) } + + override def remove(k: String): Option[String] = { + val r = underlying remove k + if (r != null) Some(r.asInstanceOf[String]) else None + } + + def iterator: Iterator[(String, String)] = new AbstractIterator[(String, String)] { + val ui = underlying.entrySet.iterator + def hasNext = ui.hasNext + def next() = { + val e = ui.next() + (e.getKey.asInstanceOf[String], e.getValue.asInstanceOf[String]) + } + } + + override def clear() = underlying.clear() + + override def empty = JPropertiesWrapper(new ju.Properties) + + def getProperty(key: String) = underlying.getProperty(key) + + def getProperty(key: String, defaultValue: String) = + underlying.getProperty(key, defaultValue) + + def setProperty(key: String, value: String) = + underlying.setProperty(key, value) + } +} + +object Wrappers extends Wrappers diff --git a/src/library/scala/collection/convert/package.scala b/src/library/scala/collection/convert/package.scala new file mode 100644 index 0000000000..2f8bca1e1f --- /dev/null +++ b/src/library/scala/collection/convert/package.scala @@ -0,0 +1,18 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.collection + +package object convert { + val decorateAsJava = new DecorateAsJava { } + val decorateAsScala = new DecorateAsScala { } + val decorateAll = new DecorateAsJava with DecorateAsScala { } + val wrapAsJava = new WrapAsJava { } + val wrapAsScala = new WrapAsScala { } + val wrapAll = new WrapAsJava with WrapAsScala { } +} -- cgit v1.2.3