Make CovariateRow's serializable; add R utility functions.

This commit is contained in:
Joel Therrien 2018-09-14 14:53:01 -07:00
parent 7008959999
commit de39f60314
2 changed files with 33 additions and 1 deletions

View file

@ -4,12 +4,13 @@ import ca.joeltherrien.randomforest.covariates.Covariate;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequiredArgsConstructor
public class CovariateRow {
public class CovariateRow implements Serializable {
private final Map<String, Covariate.Value> valueMap;

View file

@ -1,6 +1,9 @@
package ca.joeltherrien.randomforest.utils;
import java.io.*;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* These static methods are designed to make the R interface more performant; and to avoid using R loops.
@ -30,5 +33,33 @@ public final class RUtils {
return times;
}
/**
* Convenience method to help R package serialize Java objects.
*
* @param object The object to be serialized.
* @param filename The path to the object to be saved.
* @throws IOException
*/
public static void serializeObject(Serializable object, String filename) throws IOException {
final ObjectOutputStream outputStream = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(filename)));
outputStream.writeObject(object);
outputStream.close();
}
/**
* Convenience method to help R package load serialized Java objects.
*
* @param filename The path to the object saved.
* @throws IOException
*/
public static Object loadObject(String filename) throws IOException, ClassNotFoundException {
final ObjectInputStream inputStream = new ObjectInputStream(new GZIPInputStream(new FileInputStream(filename)));
final Object object = inputStream.readObject();
inputStream.close();
return object;
}
}