summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Zenger <mzenger@gmail.com>2003-06-12 10:44:35 +0000
committerMatthias Zenger <mzenger@gmail.com>2003-06-12 10:44:35 +0000
commit1ab2519887ae12ac77c27b5e3b8547762a74f5c4 (patch)
treeffa93c7d1c329bb46090dfc233c16047223b6eb2
parentca1cba5b0642f334f174f86ece90ffa3e9647052 (diff)
downloadscala-1ab2519887ae12ac77c27b5e3b8547762a74f5c4.tar.gz
scala-1ab2519887ae12ac77c27b5e3b8547762a74f5c4.tar.bz2
scala-1ab2519887ae12ac77c27b5e3b8547762a74f5c4.zip
Cleaned up and optimized the code.
-rw-r--r--sources/scala/Monitor.scala16
-rw-r--r--sources/scala/NativeLoop.java22
-rw-r--r--sources/scala/NativeMonitor.java30
-rw-r--r--sources/scala/Predef.scala121
-rw-r--r--sources/scala/SynchronizedSet.scala79
5 files changed, 197 insertions, 71 deletions
diff --git a/sources/scala/Monitor.scala b/sources/scala/Monitor.scala
index 98e3a5e330..c03c7bd2ef 100644
--- a/sources/scala/Monitor.scala
+++ b/sources/scala/Monitor.scala
@@ -1,9 +1,17 @@
-package scala;
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+** $Id$
+\* */
-class Monitor() extends NativeMonitor() {
+package scala;
- def synchronized[a](def p: a): a = synchronised(p);
+trait Monitor {
- def await(def cond: Boolean) = while (!cond) { this.wait() }
+ def synchronized[a](def p: a): a = NativeMonitor.synchronised(this, p);
+ def await(def cond: Boolean) = while (!cond) { this.wait() }
}
diff --git a/sources/scala/NativeLoop.java b/sources/scala/NativeLoop.java
new file mode 100644
index 0000000000..8751c15cb7
--- /dev/null
+++ b/sources/scala/NativeLoop.java
@@ -0,0 +1,22 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+** $Id$
+\* */
+
+package scala;
+
+
+public class NativeLoop {
+
+ /** @meta method [?A] (def scala.Boolean, def ?A) scala.Unit;
+ */
+ public static void loopWhile(scala.Function0 cond, scala.Function0 body) {
+ while (((scala.Boolean)cond.apply()).asBoolean()) {
+ body.apply();
+ }
+ }
+}
diff --git a/sources/scala/NativeMonitor.java b/sources/scala/NativeMonitor.java
index 25fca6b98a..ade1a10110 100644
--- a/sources/scala/NativeMonitor.java
+++ b/sources/scala/NativeMonitor.java
@@ -1,14 +1,34 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+** $Id$
+\* */
+
package scala;
+
public class NativeMonitor {
/** @meta method [?A] (def ?A) ?A;
*/
public java.lang.Object synchronised(scala.Function0 p) {
- java.lang.Object result;
- synchronized(this) {
- result = p.apply();
- }
- return result;
+ java.lang.Object result;
+ synchronized(this) {
+ result = p.apply();
+ }
+ return result;
+ }
+
+ /** @meta method [?A] (scala.AnyRef, def ?A) ?A;
+ */
+ public static java.lang.Object synchronised(java.lang.Object any, scala.Function0 p) {
+ java.lang.Object result;
+ synchronized(any) {
+ result = p.apply();
+ }
+ return result;
}
}
diff --git a/sources/scala/Predef.scala b/sources/scala/Predef.scala
index 6626c91189..8556c2f21b 100644
--- a/sources/scala/Predef.scala
+++ b/sources/scala/Predef.scala
@@ -2,75 +2,72 @@ package scala;
object Predef {
- type byte = scala.Byte;
- type short = scala.Short;
- type char = scala.Char;
- type int = scala.Int;
- type long = scala.Long;
- type float = scala.Float;
- type double = scala.Double;
- type boolean = scala.Boolean;
- type unit = scala.Unit;
-
- def List[A](x: A*): List[A] = x as List[A];
- val List = scala.List;
-
- def Set[A](es: A*): Set[A] = {
- val set = new HashSet[A];
- set.addSet(es);
- set;
- }
-
- def ListSet[A](es: A*): Set[A] = {
- val set = new ListSet[A];
- set.addSet(es);
- set;
- }
-
- def HashSet[A](es: A*): Set[A] = {
- val set = new HashSet[A];
- set.addSet(es);
- set;
- }
-
- def Map[A, B](mappings: Pair[A, B]*): Map[A, B] = {
- val map = new HashMap[A, B];
- map.putMap(mappings);
- map;
- }
-
- def error(x: String): All = new java.lang.RuntimeException(x).throw;
-
- def try[a](def block: a): Except[a] =
- new Except(scala.runtime.ResultOrException.tryBlock(block));
-
- def while(def condition: Boolean)(def command: Unit): Unit =
- if (condition) {
- command; while(condition)(command)
- } else {
+ type byte = scala.Byte;
+ type short = scala.Short;
+ type char = scala.Char;
+ type int = scala.Int;
+ type long = scala.Long;
+ type float = scala.Float;
+ type double = scala.Double;
+ type boolean = scala.Boolean;
+ type unit = scala.Unit;
+
+ def List[A](x: A*): List[A] = x as List[A];
+ val List = scala.List;
+
+ def Set[A](es: A*): Set[A] = {
+ val set = new HashSet[A];
+ set.addSet(es);
+ set;
}
- trait Until {
- def until(def condition: Boolean): Unit
- }
-
- def repeat(def command: Unit): Until =
- new Until {
- def until(def condition: Boolean): Unit = {
- command ;
- if (condition) {}
- else until(condition)
- }
+ def ListSet[A](es: A*): Set[A] = {
+ val set = new ListSet[A];
+ set.addSet(es);
+ set;
}
- type Pair = Tuple2;
- def Pair[a, b](x: a, y: b) = Tuple2(x, y);
+ def HashSet[A](es: A*): Set[A] = {
+ val set = new HashSet[A];
+ set.addSet(es);
+ set;
+ }
- type Triple = Tuple3;
- def Triple[a, b, c](x: a, y: b, z: c) = Tuple3(x, y, z);
-}
+ def Map[A, B](mappings: Pair[A, B]*): Map[A, B] = {
+ val map = new HashMap[A, B];
+ map.putMap(mappings);
+ map;
+ }
+ def error(x: String): All = new java.lang.RuntimeException(x).throw;
+ def exit: scala.Unit = System.exit(0);
+ def synchronized[A](obj: AnyRef)(def body: A) = NativeMonitor.synchronised(obj, body);
+ def try[a](def block: a): Except[a] =
+ new Except(scala.runtime.ResultOrException.tryBlock(block));
+ def while(def cond: Boolean)(def body: Unit): Unit = NativeLoop.loopWhile(cond, body);
+ /* if (cond) {
+ command; while(cond)(body)
+ } */
+
+ trait Until {
+ def until(def condition: Boolean): Unit
+ }
+
+ def repeat(def command: Unit): Until =
+ new Until {
+ def until(def condition: Boolean): Unit = {
+ command ;
+ if (condition) {} else until(condition)
+ }
+ }
+
+ type Pair = Tuple2;
+ def Pair[a, b](x: a, y: b) = Tuple2(x, y);
+
+ type Triple = Tuple3;
+ def Triple[a, b, c](x: a, y: b, z: c) = Tuple3(x, y, z);
+}
diff --git a/sources/scala/SynchronizedSet.scala b/sources/scala/SynchronizedSet.scala
new file mode 100644
index 0000000000..6c78ebddf8
--- /dev/null
+++ b/sources/scala/SynchronizedSet.scala
@@ -0,0 +1,79 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+** $Id$
+\* */
+
+package scala;
+
+/** I promise, there will be some documentation soon! :-) Matthias
+ */
+trait SynchronizedSet[A] extends Set[A] with Monitor {
+
+ override def size: Int = synchronized {
+ super.size
+ }
+
+ override def isEmpty: Boolean = synchronized {
+ super.isEmpty
+ }
+
+ override def contains(elem: A) = synchronized {
+ super.contains(elem);
+ }
+
+ override def add(elem: A): Unit = synchronized {
+ super.add(elem);
+ }
+
+ override def addAll(elems: A*): Unit = synchronized {
+ super.addSet(elems);
+ }
+
+ override def addSet(that: Iterable[A]) = synchronized {
+ super.addSet(that);
+ }
+
+ override def remove(elem: A): Unit = synchronized {
+ super.remove(elem);
+ }
+
+ override def removeAll(elems: A*): Unit = synchronized {
+ super.removeSet(elems);
+ }
+
+ override def removeSet(that: Iterable[A]) = synchronized {
+ super.removeSet(that);
+ }
+
+ override def intersect(that: Set[A]) = synchronized {
+ super.intersect(that);
+ }
+
+ override def clear: Unit = synchronized {
+ super.clear;
+ }
+
+ override def subsetOf(that: Set[A]) = synchronized {
+ super.subsetOf(that);
+ }
+
+ override def foreach(f: A => Unit) = synchronized {
+ super.foreach(f);
+ }
+
+ override def filter(p: A => Boolean) = synchronized {
+ super.filter(p);
+ }
+
+ override def toList: List[A] = synchronized {
+ super.toList;
+ }
+
+ override def toString() = synchronized {
+ super.toString();
+ }
+}