formatting the source code
This commit is contained in:
parent
2c9c96a811
commit
a5a99c28b1
@ -5,37 +5,37 @@ import java.util.Map;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public class HandleOptionalTypeExample {
|
public class HandleOptionalTypeExample {
|
||||||
static Map<String, User> usersByName = new HashMap();
|
static Map<String, User> usersByName = new HashMap();
|
||||||
static {
|
static {
|
||||||
User user1 = new User();
|
User user1 = new User();
|
||||||
user1.setUserId(1l);
|
user1.setUserId(1l);
|
||||||
user1.setFirstName("baeldung");
|
user1.setFirstName("baeldung");
|
||||||
usersByName.put("baeldung", user1);
|
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;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,17 +4,16 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
public class OptionalToJsonExample {
|
public class OptionalToJsonExample {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
UserOptional user = new UserOptional();
|
UserOptional user = new UserOptional();
|
||||||
user.setUserId(1l);
|
user.setUserId(1l);
|
||||||
user.setFirstName("Bael Dung");
|
user.setFirstName("Bael Dung");
|
||||||
|
|
||||||
ObjectMapper om = new ObjectMapper();
|
ObjectMapper om = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
System.out.print("user in json is:"+om.writeValueAsString(user));
|
System.out.print("user in json is:" + om.writeValueAsString(user));
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
// TODO Auto-generated catch block
|
e.printStackTrace();
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -7,20 +7,20 @@ import javax.persistence.EntityManagerFactory;
|
|||||||
import javax.persistence.Persistence;
|
import javax.persistence.Persistence;
|
||||||
|
|
||||||
public class PersistOptionalTypeExample {
|
public class PersistOptionalTypeExample {
|
||||||
static String persistenceUnit = "com.baeldung.optionalReturnType";
|
static String persistenceUnit = "com.baeldung.optionalReturnType";
|
||||||
static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
|
static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
|
||||||
|
|
||||||
static EntityManager entityManager = emf.createEntityManager();
|
static EntityManager entityManager = emf.createEntityManager();
|
||||||
|
|
||||||
//to run this app, uncomment the follow line in META-INF/persistence.xml
|
// to run this app, uncomment the follow line in META-INF/persistence.xml
|
||||||
//<class>com.baeldung.optionalReturnType.UserOptionalField</class>
|
// <class>com.baeldung.optionalReturnType.UserOptionalField</class>
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
UserOptionalField user1 = new UserOptionalField();
|
UserOptionalField user1 = new UserOptionalField();
|
||||||
user1.setUserId(1l);
|
user1.setUserId(1l);
|
||||||
user1.setFirstName(Optional.of("Bael Dung"));
|
user1.setFirstName(Optional.of("Bael Dung"));
|
||||||
entityManager.persist(user1);
|
entityManager.persist(user1);
|
||||||
|
|
||||||
UserOptional user2 = entityManager.find(UserOptional.class, 1l);
|
UserOptional user2 = entityManager.find(UserOptional.class, 1l);
|
||||||
System.out.print("User2.firstName:"+user2.getFirstName());
|
System.out.print("User2.firstName:" + user2.getFirstName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,18 +5,18 @@ import javax.persistence.EntityManagerFactory;
|
|||||||
import javax.persistence.Persistence;
|
import javax.persistence.Persistence;
|
||||||
|
|
||||||
public class PersistOptionalTypeExample2 {
|
public class PersistOptionalTypeExample2 {
|
||||||
static String persistenceUnit = "com.baeldung.optionalReturnType";
|
static String persistenceUnit = "com.baeldung.optionalReturnType";
|
||||||
static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
|
static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
|
||||||
|
|
||||||
static EntityManager em = emf.createEntityManager();
|
static EntityManager em = emf.createEntityManager();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
UserOptional user1 = new UserOptional();
|
UserOptional user1 = new UserOptional();
|
||||||
user1.setUserId(1l);
|
user1.setUserId(1l);
|
||||||
user1.setFirstName("Bael Dung");
|
user1.setFirstName("Bael Dung");
|
||||||
em.persist(user1);
|
em.persist(user1);
|
||||||
|
|
||||||
UserOptional user2 = em.find(UserOptional.class, 1l);
|
UserOptional user2 = em.find(UserOptional.class, 1l);
|
||||||
System.out.print("User2.firstName:"+user2.getFirstName());
|
System.out.print("User2.firstName:" + user2.getFirstName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,18 +5,18 @@ import javax.persistence.EntityManagerFactory;
|
|||||||
import javax.persistence.Persistence;
|
import javax.persistence.Persistence;
|
||||||
|
|
||||||
public class PersistUserExample {
|
public class PersistUserExample {
|
||||||
static String persistenceUnit = "com.baeldung.optionalReturnType";
|
static String persistenceUnit = "com.baeldung.optionalReturnType";
|
||||||
static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
|
static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
|
||||||
|
|
||||||
static EntityManager em = emf.createEntityManager();
|
static EntityManager em = emf.createEntityManager();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
User user1 = new User();
|
User user1 = new User();
|
||||||
user1.setUserId(1l);
|
user1.setUserId(1l);
|
||||||
user1.setFirstName("Bael Dung");
|
user1.setFirstName("Bael Dung");
|
||||||
em.persist(user1);
|
em.persist(user1);
|
||||||
|
|
||||||
User user2 = em.find(User.class, 1l);
|
User user2 = em.find(User.class, 1l);
|
||||||
System.out.print("User2.firstName:"+user2.getFirstName());
|
System.out.print("User2.firstName:" + user2.getFirstName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,37 +6,36 @@ import java.io.ObjectOutputStream;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public class SerializeOptionalTypeExample {
|
public class SerializeOptionalTypeExample {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
User user1 = new User();
|
User user1 = new User();
|
||||||
user1.setUserId(1l);
|
user1.setUserId(1l);
|
||||||
user1.setFirstName("baeldung");
|
user1.setFirstName("baeldung");
|
||||||
|
|
||||||
serializeObject(user1, "user1.ser");
|
|
||||||
|
|
||||||
|
|
||||||
UserOptionalField user2 = new UserOptionalField();
|
serializeObject(user1, "user1.ser");
|
||||||
user2.setUserId(1l);
|
|
||||||
user2.setFirstName(Optional.of("baeldung"));
|
|
||||||
|
|
||||||
serializeObject(user2, "user2.ser");
|
|
||||||
|
|
||||||
}
|
UserOptionalField user2 = new UserOptionalField();
|
||||||
|
user2.setUserId(1l);
|
||||||
public static void serializeObject(Object object, String fileName) {
|
user2.setFirstName(Optional.of("baeldung"));
|
||||||
// Serialization
|
|
||||||
try {
|
serializeObject(user2, "user2.ser");
|
||||||
FileOutputStream file = new FileOutputStream(fileName);
|
|
||||||
ObjectOutputStream out = new ObjectOutputStream(file);
|
}
|
||||||
|
|
||||||
out.writeObject(object);
|
public static void serializeObject(Object object, String fileName) {
|
||||||
|
// Serialization
|
||||||
out.close();
|
try {
|
||||||
file.close();
|
FileOutputStream file = new FileOutputStream(fileName);
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(file);
|
||||||
System.out.println("Object "+object.toString()+ " has been serialized to file "+fileName);
|
|
||||||
|
out.writeObject(object);
|
||||||
} catch(IOException e) {
|
|
||||||
e.printStackTrace();
|
out.close();
|
||||||
}
|
file.close();
|
||||||
}
|
|
||||||
|
System.out.println("Object " + object.toString() + " has been serialized to file " + fileName);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,25 +7,25 @@ import javax.persistence.Id;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class User implements Serializable {
|
public class User implements Serializable {
|
||||||
@Id
|
@Id
|
||||||
private long userId;
|
private long userId;
|
||||||
|
|
||||||
private String firstName;
|
|
||||||
|
|
||||||
public long getUserId() {
|
|
||||||
return userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserId(long userId) {
|
private String firstName;
|
||||||
this.userId = userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFirstName() {
|
public long getUserId() {
|
||||||
return firstName;
|
return userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setUserId(long userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
|
||||||
this.firstName = firstName;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,27 +9,27 @@ import javax.persistence.Id;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class UserOptional implements Serializable {
|
public class UserOptional implements Serializable {
|
||||||
@Id
|
@Id
|
||||||
private long userId;
|
private long userId;
|
||||||
|
|
||||||
@Column(nullable = true)
|
|
||||||
private String firstName;
|
|
||||||
|
|
||||||
public long getUserId() {
|
|
||||||
return userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserId(long userId) {
|
@Column(nullable = true)
|
||||||
this.userId = userId;
|
private String firstName;
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<String> getFirstName() {
|
public long getUserId() {
|
||||||
return Optional.ofNullable(firstName);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
|
||||||
this.firstName = firstName;
|
|
||||||
Optional.ofNullable(firstName);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,24 +8,24 @@ import javax.persistence.Id;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class UserOptionalField implements Serializable {
|
public class UserOptionalField implements Serializable {
|
||||||
@Id
|
@Id
|
||||||
private long userId;
|
private long userId;
|
||||||
|
|
||||||
private Optional<String> firstName;
|
|
||||||
|
|
||||||
public long getUserId() {
|
|
||||||
return userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserId(long userId) {
|
private Optional<String> firstName;
|
||||||
this.userId = userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<String> getFirstName() {
|
public long getUserId() {
|
||||||
return firstName;
|
return userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFirstName(Optional<String> firstName) {
|
public void setUserId(long userId) {
|
||||||
this.firstName = firstName;
|
this.userId = userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<String> getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(Optional<String> firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user