Update to ScalaSTM 0.5 (which includes Java API)
This commit is contained in:
parent
59d6e93af4
commit
3fbd18f0fa
11 changed files with 27 additions and 346 deletions
|
|
@ -8,10 +8,10 @@ package akka.docs.transactor;
|
|||
import akka.actor.*;
|
||||
import akka.transactor.*;
|
||||
import scala.concurrent.stm.Ref;
|
||||
import scala.concurrent.stm.japi.Stm;
|
||||
import scala.concurrent.stm.japi.STM;
|
||||
|
||||
public class CoordinatedCounter extends UntypedActor {
|
||||
private Ref.View<Integer> count = Stm.newRef(0);
|
||||
private Ref.View<Integer> count = STM.newRef(0);
|
||||
|
||||
public void onReceive(Object incoming) throws Exception {
|
||||
if (incoming instanceof Coordinated) {
|
||||
|
|
@ -24,7 +24,7 @@ public class CoordinatedCounter extends UntypedActor {
|
|||
}
|
||||
coordinated.atomic(new Runnable() {
|
||||
public void run() {
|
||||
Stm.increment(count, 1);
|
||||
STM.increment(count, 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ package akka.docs.transactor;
|
|||
//#class
|
||||
import akka.transactor.*;
|
||||
import scala.concurrent.stm.Ref;
|
||||
import scala.concurrent.stm.japi.Stm;
|
||||
import scala.concurrent.stm.japi.STM;
|
||||
|
||||
public class Counter extends UntypedTransactor {
|
||||
Ref.View<Integer> count = Stm.newRef(0);
|
||||
Ref.View<Integer> count = STM.newRef(0);
|
||||
|
||||
public void atomically(Object message) {
|
||||
if (message instanceof Increment) {
|
||||
Stm.increment(count, 1);
|
||||
STM.increment(count, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,10 +9,10 @@ import akka.actor.*;
|
|||
import akka.transactor.*;
|
||||
import java.util.Set;
|
||||
import scala.concurrent.stm.Ref;
|
||||
import scala.concurrent.stm.japi.Stm;
|
||||
import scala.concurrent.stm.japi.STM;
|
||||
|
||||
public class FriendlyCounter extends UntypedTransactor {
|
||||
Ref.View<Integer> count = Stm.newRef(0);
|
||||
Ref.View<Integer> count = STM.newRef(0);
|
||||
|
||||
@Override public Set<SendTo> coordinate(Object message) {
|
||||
if (message instanceof Increment) {
|
||||
|
|
@ -25,7 +25,7 @@ public class FriendlyCounter extends UntypedTransactor {
|
|||
|
||||
public void atomically(Object message) {
|
||||
if (message instanceof Increment) {
|
||||
Stm.increment(count, 1);
|
||||
STM.increment(count, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -609,11 +609,11 @@ Java
|
|||
|
||||
For Java there is a special helper object with Java-friendly methods::
|
||||
|
||||
import scala.concurrent.stm.japi.Stm;
|
||||
import scala.concurrent.stm.japi.STM;
|
||||
|
||||
These methods can also be statically imported::
|
||||
|
||||
import static scala.concurrent.stm.japi.Stm.*;
|
||||
import static scala.concurrent.stm.japi.STM.*;
|
||||
|
||||
Other imports that are needed are in the stm package, particularly ``Ref``::
|
||||
|
||||
|
|
@ -671,7 +671,7 @@ v1.3::
|
|||
|
||||
v2.0::
|
||||
|
||||
import static scala.concurrent.stm.japi.Stm.atomic;
|
||||
import static scala.concurrent.stm.japi.STM.atomic;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
atomic(new Runnable() {
|
||||
|
|
@ -756,7 +756,7 @@ Java
|
|||
|
||||
As ``Ref.View`` in ScalaSTM does not require implicit transactions, this is more
|
||||
easily used from Java. ``Ref`` could be used, but requires explicit threading of
|
||||
transactions. There are helper methods in ``japi.Stm`` for creating ``Ref.View``
|
||||
transactions. There are helper methods in ``japi.STM`` for creating ``Ref.View``
|
||||
references.
|
||||
|
||||
v1.3::
|
||||
|
|
@ -765,7 +765,7 @@ v1.3::
|
|||
|
||||
v2.0::
|
||||
|
||||
Ref.View<Integer> ref = Stm.newRef(0);
|
||||
Ref.View<Integer> ref = STM.newRef(0);
|
||||
|
||||
The ``set`` and ``get`` methods work the same way for both versions.
|
||||
|
||||
|
|
@ -780,7 +780,7 @@ v2.0::
|
|||
ref.set(1); // set new value
|
||||
|
||||
There are also ``transform``, ``getAndTransform``, and ``transformAndGet``
|
||||
methods in ``japi.Stm`` which accept ``scala.runtime.AbstractFunction1``.
|
||||
methods in ``japi.STM`` which accept ``japi.STM.Transformer`` objects.
|
||||
|
||||
There are ``increment`` helper methods for ``Ref.View<Integer>`` and
|
||||
``Ref.View<Long>`` references.
|
||||
|
|
@ -821,7 +821,7 @@ Java
|
|||
|
||||
Rather than using the ``deferred`` and ``compensating`` methods in
|
||||
``akka.stm.StmUtils``, use the ``afterCommit`` and ``afterRollback`` methods in
|
||||
``scala.concurrent.stm.japi.Stm``, which behave in the same way and accept
|
||||
``scala.concurrent.stm.japi.STM``, which behave in the same way and accept
|
||||
``Runnable``.
|
||||
|
||||
Transactional Datastructures
|
||||
|
|
@ -830,12 +830,12 @@ Transactional Datastructures
|
|||
In ScalaSTM see ``TMap``, ``TSet``, and ``TArray`` for transactional
|
||||
datastructures.
|
||||
|
||||
There are helper methods for creating these from Java in ``japi.Stm``:
|
||||
There are helper methods for creating these from Java in ``japi.STM``:
|
||||
``newTMap``, ``newTSet``, and ``newTArray``. These datastructures implement the
|
||||
``scala.collection`` interfaces and can also be used from Java with Scala's
|
||||
``JavaConversions``. There are helper methods that apply the conversions,
|
||||
returning ``java.util`` ``Map``, ``Set``, and ``List``: ``newMap``, ``newSet``,
|
||||
and ``newList``.
|
||||
and ``newArrayAsList``.
|
||||
|
||||
|
||||
More to be written
|
||||
|
|
|
|||
|
|
@ -1,147 +0,0 @@
|
|||
/* scala-stm - (c) 2009-2011, Stanford University, PPL */
|
||||
|
||||
package scala.concurrent.stm.japi
|
||||
|
||||
import java.util.concurrent.Callable
|
||||
import java.util.{ List ⇒ JList, Map ⇒ JMap, Set ⇒ JSet }
|
||||
import scala.collection.JavaConversions
|
||||
import scala.concurrent.stm
|
||||
import scala.concurrent.stm._
|
||||
import scala.runtime.AbstractFunction1
|
||||
|
||||
/**
|
||||
* Java-friendly API for ScalaSTM.
|
||||
* These methods can also be statically imported.
|
||||
*/
|
||||
object Stm {
|
||||
|
||||
/**
|
||||
* Create a Ref with an initial value. Return a `Ref.View`, which does not
|
||||
* require implicit transactions.
|
||||
* @param initialValue the initial value for the newly created `Ref.View`
|
||||
* @return a new `Ref.View`
|
||||
*/
|
||||
def newRef[A](initialValue: A): Ref.View[A] = Ref(initialValue).single
|
||||
|
||||
/**
|
||||
* Create an empty TMap. Return a `TMap.View`, which does not require
|
||||
* implicit transactions. See newMap for included java conversion.
|
||||
* @return a new, empty `TMap.View`
|
||||
*/
|
||||
def newTMap[A, B](): TMap.View[A, B] = TMap.empty[A, B].single
|
||||
|
||||
/**
|
||||
* Create an empty TMap. Return a `java.util.Map` view of this TMap.
|
||||
* @return a new, empty `TMap.View` wrapped as a `java.util.Map`.
|
||||
*/
|
||||
def newMap[A, B](): JMap[A, B] = JavaConversions.mutableMapAsJavaMap(newTMap[A, B])
|
||||
|
||||
/**
|
||||
* Create an empty TSet. Return a `TSet.View`, which does not require
|
||||
* implicit transactions. See newSet for included java conversion.
|
||||
* @return a new, empty `TSet.View`
|
||||
*/
|
||||
def newTSet[A](): TSet.View[A] = TSet.empty[A].single
|
||||
|
||||
/**
|
||||
* Create an empty TSet. Return a `java.util.Set` view of this TSet.
|
||||
* @return a new, empty `TSet.View` wrapped as a `java.util.Set`.
|
||||
*/
|
||||
def newSet[A](): JSet[A] = JavaConversions.mutableSetAsJavaSet(newTSet[A])
|
||||
|
||||
/**
|
||||
* Create a TArray containing `length` elements. Return a `TArray.View`,
|
||||
* which does not require implicit transactions. See newList for included
|
||||
* java conversion.
|
||||
* @param length the length of the `TArray.View` to be created
|
||||
* @return a new `TArray.View` containing `length` elements (initially null)
|
||||
*/
|
||||
def newTArray[A <: AnyRef](length: Int): TArray.View[A] = TArray.ofDim[A](length)(ClassManifest.classType(AnyRef.getClass)).single
|
||||
|
||||
/**
|
||||
* Create an empty TArray. Return a `java.util.List` view of this Array.
|
||||
* @param length the length of the `TArray.View` to be created
|
||||
* @return a new, empty `TArray.View` wrapped as a `java.util.List`.
|
||||
*/
|
||||
def newList[A <: AnyRef](length: Int): JList[A] = JavaConversions.mutableSeqAsJavaList(newTArray[A](length))
|
||||
|
||||
/**
|
||||
* Atomic block that takes a `Runnable`.
|
||||
* @param runnable the `Runnable` to run within a transaction
|
||||
*/
|
||||
def atomic(runnable: Runnable): Unit = stm.atomic { txn ⇒ runnable.run }
|
||||
|
||||
/**
|
||||
* Atomic block that takes a `Callable`.
|
||||
* @param callable the `Callable` to run within a transaction
|
||||
* @return the value returned by the `Callable`
|
||||
*/
|
||||
def atomic[A](callable: Callable[A]): A = stm.atomic { txn ⇒ callable.call }
|
||||
|
||||
/**
|
||||
* Transform the value stored by `ref` by applying the function `f`.
|
||||
* @param ref the `Ref.View` to be transformed
|
||||
* @param f the function to be applied
|
||||
*/
|
||||
def transform[A](ref: Ref.View[A], f: AbstractFunction1[A, A]): Unit = ref.transform(f)
|
||||
|
||||
/**
|
||||
* Transform the value stored by `ref` by applying the function `f` and
|
||||
* return the old value.
|
||||
* @param ref the `Ref.View` to be transformed
|
||||
* @param f the function to be applied
|
||||
* @return the old value of `ref`
|
||||
*/
|
||||
def getAndTransform[A](ref: Ref.View[A], f: AbstractFunction1[A, A]): A = ref.getAndTransform(f)
|
||||
|
||||
/**
|
||||
* Transform the value stored by `ref` by applying the function `f` and
|
||||
* return the new value.
|
||||
* @param ref the `Ref.View` to be transformed
|
||||
* @param f the function to be applied
|
||||
* @return the new value of `ref`
|
||||
*/
|
||||
def transformAndGet[A](ref: Ref.View[A], f: AbstractFunction1[A, A]): A = ref.transformAndGet(f)
|
||||
|
||||
/**
|
||||
* Increment the `java.lang.Integer` value of a `Ref.View`.
|
||||
* @param ref the `Ref.View<Integer>` to be incremented
|
||||
* @param delta the amount to increment
|
||||
*/
|
||||
def increment(ref: Ref.View[java.lang.Integer], delta: Int): Unit = ref.transform { v ⇒ v.intValue + delta }
|
||||
|
||||
/**
|
||||
* Increment the `java.lang.Long` value of a `Ref.View`.
|
||||
* @param ref the `Ref.View<Long>` to be incremented
|
||||
* @param delta the amount to increment
|
||||
*/
|
||||
def increment(ref: Ref.View[java.lang.Long], delta: Long): Unit = ref.transform { v ⇒ v.longValue + delta }
|
||||
|
||||
/**
|
||||
* Add a task to run after the current transaction has committed.
|
||||
* @param task the `Runnable` task to run after transaction commit
|
||||
*/
|
||||
def afterCommit(task: Runnable): Unit = {
|
||||
val txn = Txn.findCurrent
|
||||
if (txn.isDefined) Txn.afterCommit(status ⇒ task.run)(txn.get)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a task to run after the current transaction has rolled back.
|
||||
* @param task the `Runnable` task to run after transaction rollback
|
||||
*/
|
||||
def afterRollback(task: Runnable): Unit = {
|
||||
val txn = Txn.findCurrent
|
||||
if (txn.isDefined) Txn.afterRollback(status ⇒ task.run)(txn.get)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a task to run after the current transaction has either rolled back
|
||||
* or committed.
|
||||
* @param task the `Runnable` task to run after transaction completion
|
||||
*/
|
||||
def afterCompletion(task: Runnable): Unit = {
|
||||
val txn = Txn.findCurrent
|
||||
if (txn.isDefined) Txn.afterCompletion(status ⇒ task.run)(txn.get)
|
||||
}
|
||||
}
|
||||
|
|
@ -8,14 +8,14 @@ import akka.actor.ActorRef;
|
|||
import akka.actor.Actors;
|
||||
import akka.actor.UntypedActor;
|
||||
import scala.concurrent.stm.Ref;
|
||||
import scala.concurrent.stm.japi.Stm;
|
||||
import scala.concurrent.stm.japi.STM;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class UntypedCoordinatedCounter extends UntypedActor {
|
||||
private String name;
|
||||
private Ref.View<Integer> count = Stm.newRef(0);
|
||||
private Ref.View<Integer> count = STM.newRef(0);
|
||||
|
||||
public UntypedCoordinatedCounter(String name) {
|
||||
this.name = name;
|
||||
|
|
@ -40,8 +40,8 @@ public class UntypedCoordinatedCounter extends UntypedActor {
|
|||
}
|
||||
coordinated.atomic(new Runnable() {
|
||||
public void run() {
|
||||
Stm.increment(count, 1);
|
||||
Stm.afterCompletion(countDown);
|
||||
STM.increment(count, 1);
|
||||
STM.afterCompletion(countDown);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import akka.actor.ActorRef;
|
|||
import akka.transactor.UntypedTransactor;
|
||||
import akka.transactor.SendTo;
|
||||
import scala.concurrent.stm.Ref;
|
||||
import scala.concurrent.stm.japi.Stm;
|
||||
import scala.concurrent.stm.japi.STM;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
|
@ -16,7 +16,7 @@ import java.util.concurrent.TimeUnit;
|
|||
|
||||
public class UntypedCounter extends UntypedTransactor {
|
||||
private String name;
|
||||
private Ref.View<Integer> count = Stm.newRef(0);
|
||||
private Ref.View<Integer> count = STM.newRef(0);
|
||||
|
||||
public UntypedCounter(String name) {
|
||||
this.name = name;
|
||||
|
|
@ -39,14 +39,14 @@ public class UntypedCounter extends UntypedTransactor {
|
|||
|
||||
public void atomically(Object message) {
|
||||
if (message instanceof Increment) {
|
||||
Stm.increment(count, 1);
|
||||
STM.increment(count, 1);
|
||||
final Increment increment = (Increment) message;
|
||||
Runnable countDown = new Runnable() {
|
||||
public void run() {
|
||||
increment.getLatch().countDown();
|
||||
}
|
||||
};
|
||||
Stm.afterCompletion(countDown);
|
||||
STM.afterCompletion(countDown);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,156 +0,0 @@
|
|||
/* scala-stm - (c) 2009-2011, Stanford University, PPL */
|
||||
|
||||
package scala.concurrent.stm;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import scala.concurrent.stm.japi.Stm;
|
||||
import static scala.concurrent.stm.japi.Stm.*;
|
||||
|
||||
import scala.runtime.AbstractFunction1;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
|
||||
public class JavaAPITests {
|
||||
@Test
|
||||
public void createIntegerRef() {
|
||||
Ref.View<Integer> ref = newRef(0);
|
||||
int unboxed = ref.get();
|
||||
assertEquals(0, unboxed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void atomicWithRunnable() {
|
||||
final Ref.View<Integer> ref = newRef(0);
|
||||
atomic(new Runnable() {
|
||||
public void run() {
|
||||
ref.set(10);
|
||||
}
|
||||
});
|
||||
int value = ref.get();
|
||||
assertEquals(10, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void atomicWithCallable() {
|
||||
final Ref.View<Integer> ref = newRef(0);
|
||||
int oldValue = atomic(new Callable<Integer>() {
|
||||
public Integer call() {
|
||||
return ref.swap(10);
|
||||
}
|
||||
});
|
||||
assertEquals(0, oldValue);
|
||||
int newValue = ref.get();
|
||||
assertEquals(10, newValue);
|
||||
}
|
||||
|
||||
@Test(expected = TestException.class)
|
||||
public void failingTransaction() {
|
||||
final Ref.View<Integer> ref = newRef(0);
|
||||
try {
|
||||
atomic(new Runnable() {
|
||||
public void run() {
|
||||
ref.set(10);
|
||||
throw new TestException();
|
||||
}
|
||||
});
|
||||
} catch (TestException e) {
|
||||
int value = ref.get();
|
||||
assertEquals(0, value);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void transformInteger() {
|
||||
Ref.View<Integer> ref = newRef(0);
|
||||
transform(ref, new AbstractFunction1<Integer, Integer>() {
|
||||
public Integer apply(Integer i) {
|
||||
return i + 10;
|
||||
}
|
||||
});
|
||||
int value = ref.get();
|
||||
assertEquals(10, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void incrementInteger() {
|
||||
Ref.View<Integer> ref = newRef(0);
|
||||
increment(ref, 10);
|
||||
int value = ref.get();
|
||||
assertEquals(10, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void incrementLong() {
|
||||
Ref.View<Long> ref = newRef(0L);
|
||||
increment(ref, 10L);
|
||||
long value = ref.get();
|
||||
assertEquals(10L, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAndUseTMap() {
|
||||
Map<Integer, String> map = newMap();
|
||||
map.put(1, "one");
|
||||
map.put(2, "two");
|
||||
assertEquals("one", map.get(1));
|
||||
assertEquals("two", map.get(2));
|
||||
assertTrue(map.containsKey(2));
|
||||
map.remove(2);
|
||||
assertFalse(map.containsKey(2));
|
||||
}
|
||||
|
||||
@Test(expected = TestException.class)
|
||||
public void failingTMapTransaction() {
|
||||
final Map<Integer, String> map = newMap();
|
||||
try {
|
||||
atomic(new Runnable() {
|
||||
public void run() {
|
||||
map.put(1, "one");
|
||||
map.put(2, "two");
|
||||
assertTrue(map.containsKey(1));
|
||||
assertTrue(map.containsKey(2));
|
||||
throw new TestException();
|
||||
}
|
||||
});
|
||||
} catch (TestException e) {
|
||||
assertFalse(map.containsKey(1));
|
||||
assertFalse(map.containsKey(2));
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAndUseTSet() {
|
||||
Set<String> set = newSet();
|
||||
set.add("one");
|
||||
set.add("two");
|
||||
assertTrue(set.contains("one"));
|
||||
assertTrue(set.contains("two"));
|
||||
assertEquals(2, set.size());
|
||||
set.add("one");
|
||||
assertEquals(2, set.size());
|
||||
set.remove("two");
|
||||
assertFalse(set.contains("two"));
|
||||
assertEquals(1, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAndUseTArray() {
|
||||
List<String> list = newList(3);
|
||||
assertEquals(null, list.get(0));
|
||||
assertEquals(null, list.get(1));
|
||||
assertEquals(null, list.get(2));
|
||||
list.set(0, "zero");
|
||||
list.set(1, "one");
|
||||
list.set(2, "two");
|
||||
assertEquals("zero", list.get(0));
|
||||
assertEquals("one", list.get(1));
|
||||
assertEquals("two", list.get(2));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
/* scala-stm - (c) 2009-2011, Stanford University, PPL */
|
||||
|
||||
package scala.concurrent.stm;
|
||||
|
||||
public class TestException extends RuntimeException {
|
||||
public TestException() {
|
||||
super("Expected failure");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
/* scala-stm - (c) 2009-2011, Stanford University, PPL */
|
||||
|
||||
package scala.concurrent.stm
|
||||
|
||||
import org.scalatest.junit.JUnitWrapperSuite
|
||||
|
||||
class JavaAPISuite extends JUnitWrapperSuite("scala.concurrent.stm.JavaAPITests", Thread.currentThread.getContextClassLoader)
|
||||
|
|
@ -491,7 +491,7 @@ object Dependency {
|
|||
val Netty = "3.3.0.Final"
|
||||
val Protobuf = "2.4.1"
|
||||
val Rabbit = "2.3.1"
|
||||
val ScalaStm = "0.4"
|
||||
val ScalaStm = "0.5"
|
||||
val Scalatest = "1.6.1"
|
||||
val Slf4j = "1.6.4"
|
||||
val Spring = "3.0.5.RELEASE"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue