SJava (version 0.98.0)

setJavaFunctionConverter: Registers a function to convert between R and Java objects

Description

This allows one to register two functions that are used to convert an object from Omegahat/Java to an R object. One function (match) determines whether the other function (handler) that actually performs the computation is suitable for converting the target object. The result of the handler function is an R object that represents the Java object being converted.

Usage

setJavaFunctionConverter(handler, match, description=NULL, fromJava=TRUE, autoArray = TRUE, position = -1)

Arguments

handler
a function that takes two arguments: a reference to the Java object to be converted and the name of its Java class. This function should return the converted value or the reference to the Java object if it cannot convert it meaningfully.
match
a function that deterines whether the associated handler function is appropriate for converting the target Java object. This function should expect two arguments: a reference to the Java object and its class name. It must return a logical value indicating whether the handler is capable of converting the Java object.
description
a descripion that is stored internally with the converter and accessible to users via the function getJavaConverterDescriptions.
fromJava
a logical value indicating whether the functions are intended for converting from Java to R or vice-versa. Currently, the R to Java mechanism is not implemented.
autoArray
a logical value indicating whether R is to deal with Java arrays in relation to this converter by calling the match/predicate function with an element of the array (TRUE) or the array itself (FALSE).
position
the index (starting at 1) at which to insert the converter. If this is non-positive, the converter entry is appended at the end of the list.

Value

An object of class "JavaFunctionConverter" with fields
index
the position of this converter in the internal list of converters. This is a useful identifier for removing the converter.
description
the value of the description argument. This is also a useful and preferred identifier for removing the converter.
handler
the value of the handler argument.
match
the value of the match argument.

References

http://www.omegahat.org/RSJava/index.html

See Also

setJavaConverter setJavaConvertible .Java .JavaConstructor

Examples

Run this code
  # this must be run wit the ROmegahatExamples.jar
  # file in the classpath (e.g. 
  #     .JavaInit(list(classPath=system.file("org/omegahat/Jars/ROmegahatExamples.jar")))
  # so as to be able to find RealVariable!

 if(!is.null(.Java("__Evaluator", "expandClassName", "RealVariable"))) {
   cvt <- setJavaFunctionConverter(function(jobj,className) {
       .Java(jobj, "getValues")
   }, function(jobj, className) {
       return(className == "org.omegahat.DataStructures.Data.RealVariable")
   }, "Omegahat RealVariable to numeric vector")

   setJavaConvertible("RealVariable")
   .JavaConstructor("RealVariable", rnorm(10))

     # now unregister the converter
   setJavaConvertible("RealVariable", FALSE)
   removeJavaConverter(cvt$index)
 }

Run the code above in your browser using DataLab