From 2fcbda771dd5a603c7264d527b2ff9fdfbd6bfbb Mon Sep 17 00:00:00 2001 From: "Taro L. Saito" Date: Fri, 22 Mar 2013 02:43:31 +0900 Subject: [PATCH] Rename Java API package --- README.md | 6 ++- project/Build.scala | 2 + src/main/java/xerial/larray/japi/LArray.java | 24 ++++++++++ src/main/java/xerial/larray/java/LArray.java | 39 ---------------- .../java/xerial/larray/java/LByteArray.java | 45 ------------------- .../xerial/larray/java/MemoryAllocator.java | 10 ----- src/main/scala/xerial/larray/LArray.scala | 6 +-- src/main/scala/xerial/larray/Logger.scala | 24 ++++++++++ .../java/xerial/larray/japi/JLArrayTest.java | 28 ++++++++++++ src/test/scala/xerial/larray/LArraySpec.scala | 3 +- 10 files changed, 86 insertions(+), 101 deletions(-) create mode 100644 src/main/java/xerial/larray/japi/LArray.java delete mode 100644 src/main/java/xerial/larray/java/LArray.java delete mode 100644 src/main/java/xerial/larray/java/LByteArray.java delete mode 100644 src/main/java/xerial/larray/java/MemoryAllocator.java create mode 100644 src/main/scala/xerial/larray/Logger.scala create mode 100644 src/test/java/xerial/larray/japi/JLArrayTest.java diff --git a/README.md b/README.md index c9ebfae..abcbff8 100644 --- a/README.md +++ b/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 ``` diff --git a/project/Build.scala b/project/Build.scala index 6a82440..57da688 100755 --- a/project/Build.scala +++ b/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 ), diff --git a/src/main/java/xerial/larray/japi/LArray.java b/src/main/java/xerial/larray/japi/LArray.java new file mode 100644 index 0000000..d0ebfc5 --- /dev/null +++ b/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); + } + + +} diff --git a/src/main/java/xerial/larray/java/LArray.java b/src/main/java/xerial/larray/java/LArray.java deleted file mode 100644 index 4882e36..0000000 --- a/src/main/java/xerial/larray/java/LArray.java +++ /dev/null @@ -1,39 +0,0 @@ -package xerial.larray.java; - -/** - * Java interface of LArray - * @author Taro L. Saito - */ -public interface LArray { - -// /** -// * Get an element at the given index -// * @param i index -// * @return element -// */ -// public A get(long i); -// -// /** -// * Set the element at the specified index -// * @param i index -// * @param v value to set -// * @return new value that is set -// */ -// public A set(long i, A v); - - /** - * Array size - */ - public long size(); - - /** - * Release the memory resource held by this LArray - */ - public void free(); - - /** - * Byte length of this LArray - * @return - */ - public long byteLength(); -} diff --git a/src/main/java/xerial/larray/java/LByteArray.java b/src/main/java/xerial/larray/java/LByteArray.java deleted file mode 100644 index 1198c30..0000000 --- a/src/main/java/xerial/larray/java/LByteArray.java +++ /dev/null @@ -1,45 +0,0 @@ -package xerial.larray.java; - -import xerial.larray.Memory; - -/** - * @author Taro L. Saito - */ -public class LByteArray implements LArray { - - private final long size; - private final Memory m; - - public LByteArray(long size) { - this(size, MemoryAllocator.defaultAllocator.allocate(size)); - } - - LByteArray(long size, Memory m) { - this.size = size; - this.m = m; - } - - public byte get(long i) { - return m.getByte(i); - } - - public byte set(long i, byte v) { - m.putByte(i, v); - return v; - } - - @Override - public long size() { - return size; - } - - @Override - public void free() { - m.free(); - } - - @Override - public long byteLength() { - return size; - } -} diff --git a/src/main/java/xerial/larray/java/MemoryAllocator.java b/src/main/java/xerial/larray/java/MemoryAllocator.java deleted file mode 100644 index f36bd74..0000000 --- a/src/main/java/xerial/larray/java/MemoryAllocator.java +++ /dev/null @@ -1,10 +0,0 @@ -package xerial.larray.java; - -import xerial.larray.DefaultAllocator; - -/** - * @author Taro L. Saito - */ -class MemoryAllocator { - static xerial.larray.MemoryAllocator defaultAllocator = new DefaultAllocator(); -} diff --git a/src/main/scala/xerial/larray/LArray.scala b/src/main/scala/xerial/larray/LArray.scala index 147cab0..effc591 100644 --- a/src/main/scala/xerial/larray/LArray.scala +++ b/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 = { diff --git a/src/main/scala/xerial/larray/Logger.scala b/src/main/scala/xerial/larray/Logger.scala new file mode 100644 index 0000000..1ffdc21 --- /dev/null +++ b/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) } + +} \ No newline at end of file diff --git a/src/test/java/xerial/larray/japi/JLArrayTest.java b/src/test/java/xerial/larray/japi/JLArrayTest.java new file mode 100644 index 0000000..19e6d25 --- /dev/null +++ b/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(); + } +} diff --git a/src/test/scala/xerial/larray/LArraySpec.scala b/src/test/scala/xerial/larray/LArraySpec.scala index 0923b0b..ba60016 100644 --- a/src/test/scala/xerial/larray/LArraySpec.scala +++ b/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)