diff options
author | Grzegorz Kossakowski <grek@google.com> | 2009-09-23 15:47:28 -0700 |
---|---|---|
committer | Grzegorz Kossakowski <grek@google.com> | 2009-09-23 15:47:28 -0700 |
commit | c2c1870e0f32958586ad5c370ef3f697de3db4e6 (patch) | |
tree | 862bb9c4dd4343f9fc35e22ae5a3db3439695dcd | |
parent | 679080789acdea738e6c802a1caf90f507da5c97 (diff) | |
download | gimd-c2c1870e0f32958586ad5c370ef3f697de3db4e6.tar.gz |
Add initial version of Java glue code useful when calling Gimd from Java.
This is initial version of Java glue code that might be useful when calling
Gimd API which heavily relies on passing functions that is poorly
supported in Java.
Right now it's a simple interface for java function and a few conversion
methods but this might evolve into something more sophisticated and then
better documented.
Change-Id: Ieaea6194c982cc96fed860ae2afea454637434c7
Signed-off-by: Grzegorz Kossakowski <grek@google.com>
-rw-r--r-- | src/main/java/com/google/gimd/javaglue/JFunction1.java | 21 | ||||
-rw-r--r-- | src/main/java/com/google/gimd/javaglue/ScalaUtil.java | 66 |
2 files changed, 87 insertions, 0 deletions
diff --git a/src/main/java/com/google/gimd/javaglue/JFunction1.java b/src/main/java/com/google/gimd/javaglue/JFunction1.java new file mode 100644 index 0000000..7e2e7ca --- /dev/null +++ b/src/main/java/com/google/gimd/javaglue/JFunction1.java @@ -0,0 +1,21 @@ +// Copyright (C) 2009 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gimd.javaglue; + +public interface JFunction1<T,R> { + + public R apply(T x) throws Throwable; + +} diff --git a/src/main/java/com/google/gimd/javaglue/ScalaUtil.java b/src/main/java/com/google/gimd/javaglue/ScalaUtil.java new file mode 100644 index 0000000..14b2560 --- /dev/null +++ b/src/main/java/com/google/gimd/javaglue/ScalaUtil.java @@ -0,0 +1,66 @@ +// Copyright (C) 2009 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gimd.javaglue; + +import java.util.Collection; + +import scala.Function1; +import scala.Function1$class; + +public class ScalaUtil { + + public static <T,R> Function1<T,R> fun1(final JFunction1<T,R> f) { + return new Function1<T,R>() { + + @SuppressWarnings("unchecked") + @Override + public <A> Function1<T, A> andThen(Function1<R, A> g) { + return Function1$class.andThen(this, g); + } + + @Override + public R apply(T x) { + try { + return f.apply(x); + } catch (Throwable e) { + throw new RuntimeException(e); + } + } + + @SuppressWarnings("unchecked") + @Override + public <A> Function1<A, R> compose(Function1<A, T> g) { + return Function1$class.compose(this, g); + } + + @Override + public int $tag() { + return 0; + } + + }; + } + + @SuppressWarnings("unchecked") + public static <T> scala.List<T> toScalaList(Collection<T> xs) { + //Java sucks as there is no way to create a generic Array so casting is needed + return scala.List$.MODULE$.fromArray(xs.toArray( (T[])new Object[xs.size()] )); + } + + public static <T,U> scala.Tuple2<T,U> tuple2(T x, U y) { + return new scala.Tuple2<T,U>(x, y); + } + +} |