refactored the module for java optional as a return
This commit is contained in:
5
core-java-modules/core-java-optional/README.md
Normal file
5
core-java-modules/core-java-optional/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
=========
|
||||
|
||||
## Core Java Optional
|
||||
|
||||
### Relevant Articles:
|
||||
53
core-java-modules/core-java-optional/pom.xml
Normal file
53
core-java-modules/core-java-optional/pom.xml
Normal file
@@ -0,0 +1,53 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>core-java-optional</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<hibernate.core.version>5.4.0.Final</hibernate.core.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<jackson.databind.version>2.9.8</jackson.databind.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.databind.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
</project>
|
||||
@@ -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<String, User> 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<User> 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<User> findUserByName(String name) {
|
||||
// look up the user in the database, the user object below could be null
|
||||
User user = usersByName.get(name);
|
||||
Optional<User> opt = Optional.ofNullable(user);
|
||||
|
||||
return opt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
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();
|
||||
|
||||
// to run this app, uncomment the follow line in META-INF/persistence.xml
|
||||
// <class>com.baeldung.optionalReturnType.UserOptionalField</class>
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.optionalReturnType;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
public class PersistUserExample {
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> getFirstName() {
|
||||
return Optional.ofNullable(firstName);
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
Optional.ofNullable(firstName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> firstName;
|
||||
|
||||
public long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Optional<String> getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(Optional<String> firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user