Hi, hab ich es richtig verstanden ,dass Optional sozusagen nur ein Behälter ist mit dem man überprüfen kann ob ein Wert eines Attributes gesetzt wurde?
In meinem Beispiel:
Explizit um diese Methode:
Hier muss ja erst überprüft werden , dass für birthday ein wert gesetzt wurde, da birthday nicht im Konstruktor gesetzt wurde, sonst würden Fehler (Exeptions geworfen) entstehen. Aber ich verstehe nicht ganz was ofNullable(birthday) dann macht? Die Rückgabe ist ja:
Return value: This method returns an instance of this Optional class with the specified value of the specified type. If the specified value is null, then this method returns an empty instance of the Optional class.
Aber das würde ja dann kein Objekt von LocalDate zurückgeben, oder?
In meinem Beispiel:
Java:
public class Contact {
private String firstName ;
private String lastName;
private ArrayList<String> telephone = new ArrayList<>();
private Ringtone ringtone = Ringtone.SIGNAL;
private LocalDate birthday;
public Contact(String firstName, String lastName){
setFirstName(firstName);
setLastName(lastName);
}
private void setLastName(String lastName) {
if(checkLastName(lastName)){
this.lastName = lastName;
}else{
throw new IllegalArgumentException();
}
}
private boolean checkLastName(String lastName) {
return lastName.length() > 0 && Character.isUpperCase(lastName.charAt(0)) && lastName != null;
}
private void setFirstName(String firstName) {
if(checkFirstName(firstName)){
this.firstName = firstName;
}else{
throw new IllegalArgumentException("Fehler!");
}
}
private boolean checkFirstName(String firstName) {
return firstName.length() > 0 && Character.isUpperCase(firstName.charAt(0)) && firstName != null;
}
public String getLastName(){
return lastName;
}
public String getFirstName(){
return firstName;
}
public ArrayList<String> getTelephone(){
return telephone;
}
public void addTelephone(String telephone){
if(checkTelephone(telephone)){
this.telephone.add(telephone);
}else{
throw new IllegalArgumentException("Fehler");
}
}
public void deleteTelephone(String telephone){
if(this.telephone.contains(telephone)){
this.telephone.remove(telephone);
}else{
throw new IllegalArgumentException("Fehler");
}
}
private boolean checkTelephone(String telephone) {
return telephone.matches("//d");
}
public Ringtone getRingtone(){
return ringtone;
}
public void setRingtone(Ringtone ringtone){
this.ringtone = ringtone;
}
public Optional<LocalDate> getBirthday(){
return Optional.ofNullable(birthday);
}
public void setBirthday(LocalDate birthday){
this.birthday = birthday;
}
@Override
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(obj.getClass().equals(this.getClass())){
Contact c = (Contact) obj;
return c.getFirstName() == this.firstName && c.getLastName() == this.getLastName();
}
return false;
}
Explizit um diese Methode:
Code:
public Optional<LocalDate> getBirthday(){
return Optional.ofNullable(birthday);
}
Return value: This method returns an instance of this Optional class with the specified value of the specified type. If the specified value is null, then this method returns an empty instance of the Optional class.
Aber das würde ja dann kein Objekt von LocalDate zurückgeben, oder?