formatting the source code
This commit is contained in:
parent
2c9c96a811
commit
a5a99c28b1
@ -20,20 +20,20 @@ public class HandleOptionalTypeExample {
|
|||||||
|
|
||||||
public static void changeUserName(String oldFirstName, String newFirstName) {
|
public static void changeUserName(String oldFirstName, String newFirstName) {
|
||||||
Optional<User> userOpt = findUserByName(oldFirstName);
|
Optional<User> userOpt = findUserByName(oldFirstName);
|
||||||
if(userOpt.isPresent()){
|
if (userOpt.isPresent()) {
|
||||||
User user = userOpt.get();
|
User user = userOpt.get();
|
||||||
user.setFirstName(newFirstName);
|
user.setFirstName(newFirstName);
|
||||||
|
|
||||||
System.out.println("user with name "+oldFirstName+" is changed to "+ user.getFirstName());
|
System.out.println("user with name " + oldFirstName + " is changed to " + user.getFirstName());
|
||||||
} else {
|
} else {
|
||||||
//user is missing
|
// user is missing
|
||||||
System.out.println("user with name "+oldFirstName+" is not found.");
|
System.out.println("user with name " + oldFirstName + " is not found.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Optional<User> findUserByName(String name){
|
public static Optional<User> findUserByName(String name) {
|
||||||
//look up the user in the database, the user object below could be null
|
// look up the user in the database, the user object below could be null
|
||||||
User user=usersByName.get(name);
|
User user = usersByName.get(name);
|
||||||
Optional<User> opt = Optional.ofNullable(user);
|
Optional<User> opt = Optional.ofNullable(user);
|
||||||
|
|
||||||
return opt;
|
return opt;
|
||||||
|
@ -11,9 +11,8 @@ public class OptionalToJsonExample {
|
|||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,8 @@ public class PersistOptionalTypeExample {
|
|||||||
|
|
||||||
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);
|
||||||
@ -21,6 +21,6 @@ public class PersistOptionalTypeExample {
|
|||||||
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,6 @@ public class PersistOptionalTypeExample2 {
|
|||||||
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,6 @@ public class PersistUserExample {
|
|||||||
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,6 @@ public class SerializeOptionalTypeExample {
|
|||||||
|
|
||||||
serializeObject(user1, "user1.ser");
|
serializeObject(user1, "user1.ser");
|
||||||
|
|
||||||
|
|
||||||
UserOptionalField user2 = new UserOptionalField();
|
UserOptionalField user2 = new UserOptionalField();
|
||||||
user2.setUserId(1l);
|
user2.setUserId(1l);
|
||||||
user2.setFirstName(Optional.of("baeldung"));
|
user2.setFirstName(Optional.of("baeldung"));
|
||||||
@ -33,9 +32,9 @@ public class SerializeOptionalTypeExample {
|
|||||||
out.close();
|
out.close();
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
System.out.println("Object "+object.toString()+ " has been serialized to file "+fileName);
|
System.out.println("Object " + object.toString() + " has been serialized to file " + fileName);
|
||||||
|
|
||||||
} catch(IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user