Permalink
Browse files

Merge pull request #15 from xerial/feature/java-api

Feature/java api
  • Loading branch information...
2 parents 0d0e5ad + 2fcbda7 commit 951c48cd901dd60388ca5579cf55f21a15e66001 @xerial committed Mar 21, 2013
View
6 README.md
@@ -67,9 +67,13 @@ l2(0) // The result of accessing released LArray is undefined
In Java we cannot provide concise syntaxes as in Scala. Instead, use `apply` and `update` methods to read/write values in arrays.
```java
+import xerial.larray.japi.LArray;
import xerial.larray.LIntArray;
-LIntArray l = new LIntArray(10000L);
+LIntArray l = LArray.newLIntArray(10000L);
l.update(0L, 20) // Set l[0L] = 20
l.apply(0L) // Get l[0L]
+
+// release
+l.free
```
View
2 project/Build.scala
@@ -47,6 +47,8 @@ object Build extends sbt.Build {
libraryDependencies ++= Seq(
// Add dependent jars here
"org.xerial" % "xerial-core" % "3.1",
+ "junit" % "junit" % "4.10" % "test",
+ "com.novocode" % "junit-interface" % "0.10-M2" % "test",
"org.scalatest" %% "scalatest" % "2.0.M5b" % "test",
"org.scala-lang" % "scala-reflect" % SCALA_VERSION
),
View
24 src/main/java/xerial/larray/japi/LArray.java
@@ -0,0 +1,24 @@
+package xerial.larray.japi;
+
+import xerial.larray.DefaultAllocator;
+import xerial.larray.LByteArray;
+import xerial.larray.LIntArray;
+
+/**
+ * Java interface of LArray
+ * @author Taro L. Saito
+ */
+public class LArray {
+
+ static xerial.larray.MemoryAllocator defaultAllocator = new DefaultAllocator();
+
+ public static LIntArray newLIntArray(long size) {
+ return new LIntArray(size, defaultAllocator);
+ }
+
+ public static LByteArray newLByteArray(long size) {
+ return new LByteArray(size, defaultAllocator);
+ }
+
+
+}
View
6 src/main/scala/xerial/larray/LArray.scala
@@ -95,8 +95,7 @@ object LArray {
def apply() = EmptyArray
-
- import java.{lang=>jl}
+ import _root_.java.{lang=>jl}
private[larray] def wrap[A:ClassTag](size:Long, m:Memory) : LArray[A] = {
val tag = implicitly[ClassTag[A]]
@@ -351,8 +350,7 @@ class LIntArray(val size: Long, private[larray] val m:Memory)(implicit alloc: Me
extends LArray[Int]
with UnsafeArray[Int]
{
- def this(size: Long)(implicit alloc: MemoryAllocator) = this(size, alloc.allocate(size << 2))
-
+ def this(size: Long)(implicit alloc: MemoryAllocator = MemoryAllocator.default) = this(size, alloc.allocate(size << 2))
import UnsafeUtil.unsafe
def apply(i: Long): Int = {
View
24 src/main/scala/xerial/larray/Logger.scala
@@ -0,0 +1,24 @@
+//--------------------------------------
+//
+// Logger.scala
+// Since: 2013/03/22 2:32
+//
+//--------------------------------------
+
+package xerial.larray
+
+/**
+ * Logger wrapper for using [[xerial.core.log.Logger]] in Java
+ * @author Taro L. Saito
+ */
+class Logger(cl:Class[_]) {
+ private val _logger = xerial.core.log.LoggerFactory(cl)
+
+ def trace(m:String) { _logger.trace(m) }
+ def debug(m:String) { _logger.debug(m) }
+ def info(m:String) { _logger.info(m) }
+ def warn(m:String) { _logger.warn(m) }
+ def error(m:String) { _logger.error(m) }
+ def fatal(m:String) { _logger.fatal(m) }
+
+}
View
28 src/test/java/xerial/larray/japi/JLArrayTest.java
@@ -0,0 +1,28 @@
+package xerial.larray.japi;
+
+import junit.framework.Assert;
+import org.junit.Test;
+import xerial.larray.LIntArray;
+import xerial.larray.Logger;
+
+/**
+ * @author Taro L. Saito
+ */
+public class JLArrayTest {
+
+ Logger _logger = new Logger(this.getClass());
+
+ @Test
+ public void constructor() {
+
+ LIntArray l = LArray.newLIntArray(5L);
+ for (long i = 0; i < l.size(); ++i) l.update(i, (int) i * 2);
+ _logger.debug(l.mkString(", "));
+ for (long i = 0; i < l.size(); ++i) l.update(i, (int) (i * i));
+ _logger.debug(l.mkString(", "));
+
+ Assert.assertEquals(5L, l.size());
+
+ l.free();
+ }
+}
View
3 src/test/scala/xerial/larray/LArraySpec.scala
@@ -9,7 +9,6 @@ package xerial.larray
import org.scalatest.matchers.{MustMatchers, ShouldMatchers}
import xerial.core.util.Timer
-import xerial.core.log.Logger
import org.scalatest._
import java.io.ByteArrayOutputStream
import scala.language.implicitConversions
@@ -19,7 +18,7 @@ import xerial.core.io.Resource
/**
* @author leo
*/
-trait LArraySpec extends WordSpec with ShouldMatchers with MustMatchers with GivenWhenThen with OptionValues with Resource with Timer with Logger
+trait LArraySpec extends WordSpec with ShouldMatchers with MustMatchers with GivenWhenThen with OptionValues with Resource with Timer with xerial.core.log.Logger
with BeforeAndAfterAll with BeforeAndAfter with BeforeAndAfterEach {
implicit def toTag(t:String) = Tag(t)

0 comments on commit 951c48c

Please sign in to comment.