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) {
|
||||
Optional<User> userOpt = findUserByName(oldFirstName);
|
||||
if(userOpt.isPresent()){
|
||||
if (userOpt.isPresent()) {
|
||||
User user = userOpt.get();
|
||||
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 {
|
||||
//user is missing
|
||||
System.out.println("user with name "+oldFirstName+" is not found.");
|
||||
// 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);
|
||||
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;
|
||||
|
@ -11,9 +11,8 @@ public class OptionalToJsonExample {
|
||||
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
try {
|
||||
System.out.print("user in json is:"+om.writeValueAsString(user));
|
||||
System.out.print("user in json is:" + om.writeValueAsString(user));
|
||||
} catch (JsonProcessingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ public class PersistOptionalTypeExample {
|
||||
|
||||
static EntityManager entityManager = emf.createEntityManager();
|
||||
|
||||
//to run this app, uncomment the follow line in META-INF/persistence.xml
|
||||
//<class>com.baeldung.optionalReturnType.UserOptionalField</class>
|
||||
// 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);
|
||||
@ -21,6 +21,6 @@ public class PersistOptionalTypeExample {
|
||||
entityManager.persist(user1);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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");
|
||||
|
||||
|
||||
UserOptionalField user2 = new UserOptionalField();
|
||||
user2.setUserId(1l);
|
||||
user2.setFirstName(Optional.of("baeldung"));
|
||||
@ -33,9 +32,9 @@ public class SerializeOptionalTypeExample {
|
||||
out.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();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user