From c2c1870e0f32958586ad5c370ef3f697de3db4e6 Mon Sep 17 00:00:00 2001 From: Grzegorz Kossakowski Date: Wed, 23 Sep 2009 15:47:28 -0700 Subject: 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 --- .../java/com/google/gimd/javaglue/JFunction1.java | 21 +++++++ .../java/com/google/gimd/javaglue/ScalaUtil.java | 66 ++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/main/java/com/google/gimd/javaglue/JFunction1.java create mode 100644 src/main/java/com/google/gimd/javaglue/ScalaUtil.java 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 { + + 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 Function1 fun1(final JFunction1 f) { + return new Function1() { + + @SuppressWarnings("unchecked") + @Override + public Function1 andThen(Function1 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 Function1 compose(Function1 g) { + return Function1$class.compose(this, g); + } + + @Override + public int $tag() { + return 0; + } + + }; + } + + @SuppressWarnings("unchecked") + public static scala.List toScalaList(Collection 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 scala.Tuple2 tuple2(T x, U y) { + return new scala.Tuple2(x, y); + } + +} -- cgit v1.2.3