Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/java api #15

Merged
merged 2 commits into from Mar 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -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
```
2 changes: 2 additions & 0 deletions project/Build.scala
Expand Up @@ -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
),
Expand Down
24 changes: 24 additions & 0 deletions 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);
}


}
6 changes: 2 additions & 4 deletions src/main/scala/xerial/larray/LArray.scala
Expand Up @@ -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]]
Expand Down Expand Up @@ -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 = {
Expand Down
24 changes: 24 additions & 0 deletions 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) }

}
28 changes: 28 additions & 0 deletions 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();
}
}
3 changes: 1 addition & 2 deletions src/test/scala/xerial/larray/LArraySpec.scala
Expand Up @@ -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
Expand All @@ -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)
Expand Down