Skip to main content
added 5 characters in body
Source Link
Sheinbergon
  • 3.1k
  • 1
  • 18
  • 27

ForJust for the sake of the argument ( though I agree with Louis above ) : You can pass around the original Reader/InputStream ( or any object, but the case you provided is actually flawed programming, because you can pass the FileReader instead of encapsulating it with BufferedReader) using common-lang3 Pair Class. Jool is also a valid library that provides Tuple* classes.

Example :

filenames.map(File::new) .filter(File::exists) .map(f->{ BufferedReader br = null; FileReader fr = null; try { fr = new FileReader(f) br = new BufferedReader(fr); return Optional.of(Pair.of(br,fr)) ; } catch(Exception e) {} return Optional.ofNullable(br); }) .filter(Optional::isPresent) .map(Optional::get) .flatMap( pair -> { try { // do something with br } finally { try { pair.getRight().close(); } catch (IOException x ){ throw new RuntimeException(x) ; } } }) 

For the sake of the argument ( though I agree with Louis above ) : You can pass around the original Reader/InputStream ( or any object, but the case you provided is actually flawed programming, because you can pass the FileReader instead of encapsulating it with BufferedReader) using common-lang3 Pair Class. Jool is also a valid library that provides Tuple* classes.

Example :

filenames.map(File::new) .filter(File::exists) .map(f->{ BufferedReader br = null; FileReader fr = null; try { fr = new FileReader(f) br = new BufferedReader(fr); return Optional.of(Pair.of(br,fr)) ; } catch(Exception e) {} return Optional.ofNullable(br); }) .filter(Optional::isPresent) .map(Optional::get) .flatMap( pair -> { try { // do something with br } finally { try { pair.getRight().close(); } catch (IOException x ){ throw new RuntimeException(x) ; } } }) 

Just for the sake of the argument ( though I agree with Louis above ) : You can pass around the original Reader/InputStream ( or any object, but the case you provided is actually flawed programming, because you can pass the FileReader instead of encapsulating it with BufferedReader) using common-lang3 Pair Class. Jool is also a valid library that provides Tuple* classes.

Example :

filenames.map(File::new) .filter(File::exists) .map(f->{ BufferedReader br = null; FileReader fr = null; try { fr = new FileReader(f) br = new BufferedReader(fr); return Optional.of(Pair.of(br,fr)) ; } catch(Exception e) {} return Optional.ofNullable(br); }) .filter(Optional::isPresent) .map(Optional::get) .flatMap( pair -> { try { // do something with br } finally { try { pair.getRight().close(); } catch (IOException x ){ throw new RuntimeException(x) ; } } }) 
added 224 characters in body
Source Link
Sheinbergon
  • 3.1k
  • 1
  • 18
  • 27

For the sake of the argument ( though I agree with Louis above ) : You can pass around the original Reader/InputStream Using( or any object, but the case you provided is actually flawed programming, because you can pass the FileReader instead of encapsulating it with BufferedReader) using common-lang3 Pair Class. Jool is also a valid library that provides Tuple* classes.

Example :

filenames.map(File::new) .filter(File::exists) .map(f->{ BufferedReader br = null; FileReader fr = null; try { fr = new FileReader(f) br = new BufferedReader(fr); return Optional.of(Pair.of(br,fr)) ; } catch(Exception e) {} return Optional.ofNullable(br); }) .filter(Optional::isPresent) .map(Optional::get) .flatMap( pair -> { try { // do something with br } finally { try { pair.getRight().close(); } catch (IOException x ){ throw new RuntimeException(x) ; } } }) 

For the sake of the argument : You can pass around the original Reader/InputStream Using common-lang3 Pair Class. Jool is also a valid library that provides Tuple* classes.

filenames.map(File::new) .filter(File::exists) .map(f->{ BufferedReader br = null; FileReader fr = null; try { fr = new FileReader(f) br = new BufferedReader(fr); return Optional.of(Pair.of(br,fr)) ; } catch(Exception e) {} return Optional.ofNullable(br); }) .filter(Optional::isPresent) .map(Optional::get) .flatMap( pair -> { try { // do something with br } finally { try { pair.getRight().close(); } catch (IOException x ){ throw new RuntimeException(x) ; } } }) 

For the sake of the argument ( though I agree with Louis above ) : You can pass around the original Reader/InputStream ( or any object, but the case you provided is actually flawed programming, because you can pass the FileReader instead of encapsulating it with BufferedReader) using common-lang3 Pair Class. Jool is also a valid library that provides Tuple* classes.

Example :

filenames.map(File::new) .filter(File::exists) .map(f->{ BufferedReader br = null; FileReader fr = null; try { fr = new FileReader(f) br = new BufferedReader(fr); return Optional.of(Pair.of(br,fr)) ; } catch(Exception e) {} return Optional.ofNullable(br); }) .filter(Optional::isPresent) .map(Optional::get) .flatMap( pair -> { try { // do something with br } finally { try { pair.getRight().close(); } catch (IOException x ){ throw new RuntimeException(x) ; } } }) 
Source Link
Sheinbergon
  • 3.1k
  • 1
  • 18
  • 27

For the sake of the argument : You can pass around the original Reader/InputStream Using common-lang3 Pair Class. Jool is also a valid library that provides Tuple* classes.

filenames.map(File::new) .filter(File::exists) .map(f->{ BufferedReader br = null; FileReader fr = null; try { fr = new FileReader(f) br = new BufferedReader(fr); return Optional.of(Pair.of(br,fr)) ; } catch(Exception e) {} return Optional.ofNullable(br); }) .filter(Optional::isPresent) .map(Optional::get) .flatMap( pair -> { try { // do something with br } finally { try { pair.getRight().close(); } catch (IOException x ){ throw new RuntimeException(x) ; } } })