Skip to content

Instantly share code, notes, and snippets.

@SirEdvin
Last active August 29, 2015 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SirEdvin/575bfe6f17ba371af735 to your computer and use it in GitHub Desktop.
Save SirEdvin/575bfe6f17ba371af735 to your computer and use it in GitHub Desktop.
Lambda serialization sample
import java.io.Serializable;
/**
* Created by siredvin on 02.08.15.
*
* @author siredvin
*/
public interface DoubleToString extends Serializable {
public String get(double t);
}
import java.io.Serializable;
/**
* Created by siredvin on 02.08.15.
*
* @author siredvin
*/
public class Factory implements Serializable{
private int c;
public Factory(int c) {
this.c = c;
}
public DoubleToString createLambda(int i,SerializableData data){
return t-> "int value: " +Integer.toString(i) +
"\nclass int value: " + String.valueOf(c) +
"\nfirst double value: "+String.valueOf(data.getT1())+
"\nsecond double value: "+String.valueOf(data.getT2())+
"\nparameter: "+String.valueOf(t);
}
}
import java.io.*;
/**
* Created by siredvin on 02.08.15.
*
* @author siredvin
*/
public class Main {
public static void main(String[] args) throws Exception {
//saveLambda();
loadLambda();
}
public static void saveLambda() throws IOException {
Factory factory = new Factory(3);
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("lambdas")));
output.writeObject(factory.createLambda(4,new SerializableData(3.3,4)));
output.writeObject(factory.createLambda(5,new SerializableData(5.3,-4)));
output.writeObject(factory.createLambda(3,new SerializableData(10.3,+80e-5)));
output.close();
}
public static void loadLambda() throws IOException, ClassNotFoundException {
ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("lambdas")));
DoubleToString operator1 = (DoubleToString) input.readObject();
DoubleToString operator2 = (DoubleToString) input.readObject();
DoubleToString operator3 = (DoubleToString) input.readObject();
System.out.println(operator1.get(5));
System.out.println(operator2.get(5));
System.out.println(operator3.get(5));
}
}
import java.io.Serializable;
/**
* Created by siredvin on 02.08.15.
*
* @author siredvin
*/
public class SerializableData implements Serializable {
private double t1;
private double t2;
public SerializableData(double t1, double t2) {
this.t1 = t1;
this.t2 = t2;
}
public double getT1() {
return t1;
}
public void setT1(double t1) {
this.t1 = t1;
}
public double getT2() {
return t2;
}
public void setT2(double t2) {
this.t2 = t2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment