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