From 552b2b2fa941f313d38ae44588dec44d3b0d82df Mon Sep 17 00:00:00 2001 From: Liesheng Long Date: Mon, 20 May 2019 02:24:51 -0400 Subject: [PATCH] sample code for article "Java Optional As Return Type" --- core-java-modules/core-java-8-2/pom.xml | 16 ++++++- .../HandleOptionalTypeExample.java | 41 ++++++++++++++++++ .../OptionalToJsonExample.java | 20 +++++++++ .../PersistOptionalTypeExample.java | 24 +++++++++++ .../PersistOptionalTypeExample2.java | 22 ++++++++++ .../PersistUserNoOptionalExample.java | 22 ++++++++++ .../SerializeOptionalTypeExample.java | 42 +++++++++++++++++++ .../com/baeldung/optionalReturnType/User.java | 31 ++++++++++++++ .../optionalReturnType/UserOptional.java | 35 ++++++++++++++++ .../optionalReturnType/UserOptionalField.java | 31 ++++++++++++++ .../main/resources/META-INF/persistence.xml | 32 ++++++++++++++ 11 files changed, 315 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/HandleOptionalTypeExample.java create mode 100644 core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/OptionalToJsonExample.java create mode 100644 core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample.java create mode 100644 core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample2.java create mode 100644 core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistUserNoOptionalExample.java create mode 100644 core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/SerializeOptionalTypeExample.java create mode 100644 core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/User.java create mode 100644 core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptional.java create mode 100644 core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptionalField.java create mode 100644 core-java-modules/core-java-8-2/src/main/resources/META-INF/persistence.xml diff --git a/core-java-modules/core-java-8-2/pom.xml b/core-java-modules/core-java-8-2/pom.xml index fbaf795b95..862b739530 100644 --- a/core-java-modules/core-java-8-2/pom.xml +++ b/core-java-modules/core-java-8-2/pom.xml @@ -30,7 +30,21 @@ icu4j ${icu.version} - + + org.hibernate + hibernate-core + 5.4.0.Final + + + com.h2database + h2 + 1.4.197 + + + com.fasterxml.jackson.core + jackson-databind + 2.9.8 + diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/HandleOptionalTypeExample.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/HandleOptionalTypeExample.java new file mode 100644 index 0000000000..a49909901b --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/HandleOptionalTypeExample.java @@ -0,0 +1,41 @@ +package com.baeldung.optionalReturnType; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class HandleOptionalTypeExample { + static Map usersByName = new HashMap(); + static { + User user1 = new User(); + user1.setUserId(1l); + user1.setFirstName("baeldung"); + usersByName.put("baeldung", user1); + } + + public static void main(String[] args) { + changeUserName("baeldung", "baeldung-new"); + changeUserName("user", "user-new"); + } + + public static void changeUserName(String oldFirstName, String newFirstName) { + Optional userOpt = findUserByName(oldFirstName); + if(userOpt.isPresent()){ + User user = userOpt.get(); + user.setFirstName(newFirstName); + + System.out.println("user with name "+oldFirstName+" is changed to "+ user.getFirstName()); + } else { + //user is missing + System.out.println("user with name "+oldFirstName+" is not found."); + } + } + + public static Optional findUserByName(String name){ + //look up the user in the database, the user object below could be null + User user=usersByName.get(name); + Optional opt = Optional.ofNullable(user); + + return opt; + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/OptionalToJsonExample.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/OptionalToJsonExample.java new file mode 100644 index 0000000000..cec5ba5ce1 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/OptionalToJsonExample.java @@ -0,0 +1,20 @@ +package com.baeldung.optionalReturnType; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class OptionalToJsonExample { + public static void main(String[] args) { + UserOptional user = new UserOptional(); + user.setUserId(1l); + user.setFirstName("Bael Dung"); + + ObjectMapper om = new ObjectMapper(); + try { + System.out.print("user in json is:"+om.writeValueAsString(user)); + } catch (JsonProcessingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample.java new file mode 100644 index 0000000000..afe46ca7eb --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample.java @@ -0,0 +1,24 @@ +package com.baeldung.optionalReturnType; + +import java.util.Optional; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +public class PersistOptionalTypeExample { + static String persistenceUnit = "com.baeldung.optionalReturnType"; + static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit); + + static EntityManager entityManager = emf.createEntityManager(); + + public static void main(String[] args) { + UserOptionalField user1 = new UserOptionalField(); + user1.setUserId(1l); + user1.setFirstName(Optional.of("Bael Dung")); + entityManager.persist(user1); + + UserOptional user2 = entityManager.find(UserOptional.class, 1l); + System.out.print("User2.firstName:"+user2.getFirstName()); + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample2.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample2.java new file mode 100644 index 0000000000..0662b692a8 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample2.java @@ -0,0 +1,22 @@ +package com.baeldung.optionalReturnType; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +public class PersistOptionalTypeExample2 { + static String persistenceUnit = "com.baeldung.optionalReturnType"; + static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit); + + static EntityManager em = emf.createEntityManager(); + + public static void main(String[] args) { + UserOptional user1 = new UserOptional(); + user1.setUserId(1l); + user1.setFirstName("Bael Dung"); + em.persist(user1); + + UserOptional user2 = em.find(UserOptional.class, 1l); + System.out.print("User2.firstName:"+user2.getFirstName()); + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistUserNoOptionalExample.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistUserNoOptionalExample.java new file mode 100644 index 0000000000..dba67feecd --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistUserNoOptionalExample.java @@ -0,0 +1,22 @@ +package com.baeldung.optionalReturnType; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +public class PersistUserNoOptionalExample { + static String persistenceUnit = "com.baeldung.optionalReturnType"; + static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit); + + static EntityManager em = emf.createEntityManager(); + + public static void main(String[] args) { + User user1 = new User(); + user1.setUserId(1l); + user1.setFirstName("Bael Dung"); + em.persist(user1); + + User user2 = em.find(User.class, 1l); + System.out.print("User2.firstName:"+user2.getFirstName()); + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/SerializeOptionalTypeExample.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/SerializeOptionalTypeExample.java new file mode 100644 index 0000000000..7ad4a5325e --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/SerializeOptionalTypeExample.java @@ -0,0 +1,42 @@ +package com.baeldung.optionalReturnType; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.util.Optional; + +public class SerializeOptionalTypeExample { + public static void main(String[] args) { + User user1 = new User(); + user1.setUserId(1l); + user1.setFirstName("baeldung"); + + serializeObject(user1, "user1.ser"); + + + UserOptionalField user2 = new UserOptionalField(); + user2.setUserId(1l); + user2.setFirstName(Optional.of("baeldung")); + + serializeObject(user2, "user2.ser"); + + } + + public static void serializeObject(Object object, String fileName) { + // Serialization + try { + FileOutputStream file = new FileOutputStream(fileName); + ObjectOutputStream out = new ObjectOutputStream(file); + + out.writeObject(object); + + out.close(); + file.close(); + + System.out.println("Object "+object.toString()+ " has been serialized to file "+fileName); + + } catch(IOException e) { + e.printStackTrace(); + } + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/User.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/User.java new file mode 100644 index 0000000000..42bedb4817 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/User.java @@ -0,0 +1,31 @@ +package com.baeldung.optionalReturnType; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class User implements Serializable { + @Id + private long userId; + + private String firstName; + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptional.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptional.java new file mode 100644 index 0000000000..c48781ea80 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptional.java @@ -0,0 +1,35 @@ +package com.baeldung.optionalReturnType; + +import java.io.Serializable; +import java.util.Optional; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class UserOptional implements Serializable { + @Id + private long userId; + + @Column(nullable = true) + private String firstName; + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + public Optional getFirstName() { + return Optional.ofNullable(firstName); + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + Optional.ofNullable(firstName); + } + +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptionalField.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptionalField.java new file mode 100644 index 0000000000..0778a8bb99 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptionalField.java @@ -0,0 +1,31 @@ +package com.baeldung.optionalReturnType; + +import java.io.Serializable; +import java.util.Optional; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class UserOptionalField implements Serializable { + @Id + private long userId; + + private Optional firstName; + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + public Optional getFirstName() { + return firstName; + } + + public void setFirstName(Optional firstName) { + this.firstName = firstName; + } +} diff --git a/core-java-modules/core-java-8-2/src/main/resources/META-INF/persistence.xml b/core-java-modules/core-java-8-2/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..3a05c733ee --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,32 @@ + + + + + Persist Optional Return Type Demo + org.hibernate.jpa.HibernatePersistenceProvider + com.baeldung.optionalReturnType.UserNoOptional + com.baeldung.optionalReturnType.UserOptional + com.baeldung.optionalReturnType.UserOptionalField + true + + + + + + + + + + + + + \ No newline at end of file