Skip to content

Instantly share code, notes, and snippets.

@ashkrit
Last active June 28, 2020 16:02
Show Gist options
  • Select an option

  • Save ashkrit/5f3068ec4f3613e09c419b20884a2774 to your computer and use it in GitHub Desktop.

Select an option

Save ashkrit/5f3068ec4f3613e09c419b20884a2774 to your computer and use it in GitHub Desktop.
@Test()
public void save_function_works() throws Exception {
// Addtional casting allow to mark as serilized
Function<String, String> upper = (Function<String, String> & Serializable) x -> x.toUpperCase();
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bos)) {
os.writeObject(upper);
try (ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bis)) {
Function<String, String> restoredUpper = (Function<String, String>) in.readObject();
Assertions.assertEquals("FAAS", restoredUpper.apply("FaaS"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment