2017-01-05 21 views
5

Tôi đang cố gắng để làm một cái gì đó như thế này:Java 8 tùy chọn: ifPresent đối tượng trở lại orElseThrow ngoại lệ

private String getStringIfObjectIsPresent(Optional<Object> object){ 
     object.ifPresent(() ->{ 
      String result = "result"; 
      //some logic with result and return it 
      return result; 
     }).orElseThrow(MyCustomException::new); 
    } 

này sẽ không làm việc, bởi vì ifPresent mất giao diện chức năng tiêu dùng như tham số, trong đó có chấp nhận void (T t). Nó không thể trả về bất kỳ giá trị nào. CÒn cách nào khác để thực hiện việc này không ?

+0

có thể trùng lặp của [sử dụng đúng cách Optional.ifPresent()] (http://stackoverflow.com/questions/24228279/proper-usage-of-optional-ifpresent) –

Trả lời

7

Tôi muốn lập bản đồ sau khi đảm bảo giá trị là có sẵn

private String getStringIfObjectIsPresent(Optional<Object> object) { 
    Object ob = object.orElseThrow(MyCustomException::new); 
    // do your mapping with ob 
    String result = your-map-function(ob); 
    return result; 
} 

hoặc lót

private String getStringIfObjectIsPresent(Optional<Object> object) { 
    return your-map-function(object.orElseThrow(MyCustomException::new)); 
} 
+0

Thực tế 'Optional.map' kiểm tra xem đối tượng có sẵn trước khi gọi hàm ánh xạ được truyền không. Nhưng ít nhất bạn bỏ 1 điều kiện ('giá trị! = Null') theo cách này. – Roland

4

Sử dụng hàm map-chức năng thay thế. Nó biến đổi giá trị bên trong tùy chọn.

Như thế này:

private String getStringIfObjectIsPresent(Optional<Object> object) { 
    return object.map(() -> { 
     String result = "result"; 
     //some logic with result and return it 
     return result; 
    }).orElseThrow(MyCustomException::new); 
} 
11

thực tế những gì bạn đang tìm kiếm là: Optional.map. Mã của bạn sẽ trông giống như sau:

object.map(o -> "result" /* or your function */) 
     .orElseThrow(MyCustomException::new); 

Tôi thà bỏ qua số Optional nếu có thể. Cuối cùng, bạn không có gì bằng cách sử dụng một số Optional tại đây. Một biến thể hơi khác:

public String getString(Object yourObject) { 
    if (Objects.isNull(yourObject)) { // or use requireNonNull instead if NullPointerException suffices 
    throw new MyCustomException(); 
    } 
    String result = ... 
    // your string mapping function 
    return result; 
} 

Nếu bạn đã có Optional -object do một cuộc gọi khác, tôi vẫn sẽ khuyên bạn nên sử dụng map -method, thay vì isPresent, vv cho lý do duy nhất, mà tôi tìm thấy nó dễ đọc hơn (rõ ràng là một quyết định chủ quan ;-)).

2

Hai tùy chọn ở đây:

Thay ifPresent với map và sử dụng Function thay vì Consumer

private String getStringIfObjectIsPresent(Optional<Object> object) { 
    return object 
      .map(obj -> { 
       String result = "result"; 
       //some logic with result and return it 
       return result; 
      }) 
      .orElseThrow(MyCustomException::new); 
} 

Sử dụng isPresent:

private String getStringIfObjectIsPresent(Optional<Object> object) { 
    if (object.isPresent()) { 
     String result = "result"; 
     //some logic with result and return it 
     return result; 
    } else { 
     throw new MyCustomException(); 
    } 
} 
Các vấn đề liên quan