Skip to main content
more comments
Source Link
Reinderien
  • 72.3k
  • 5
  • 76
  • 260

try-with-resources and exception handling are features, not bugs. The vastly less complicated approach is to use Java as Java was intended - write an AutoCloseable utility class. For a very typical example of a situation where the JDK itself operates this way, see DirectoryStream. This is how Java is designed to work. Attempting otherwise is going to be some mix of non-constructive, difficult, non-idiomatic, or "the bad kind of surprise".

Very.

TWR keeps getting forgotten.

If someone keeps forgetting to hold the steering wheel, the solution is not to remove the steering wheel and build a more complicated, less accessible steering wheel behind the dashboard. Your entry-level developers should be learning how to write idiomatic Java, rather than learning (or - as the case may be - not learning) a custom framework that attempts to be too clever. There are standard tools, some mentioned in the comments, that flag when resources are mismanaged. Learn to use these tools.

To quote a separate commenter,

I would like to consume a stream shared between threads. The stream should autodetect when it is not used any more

It's only safe to close the stream once we can guarantee that all threads are done (i.e. after a join). This can still use AutoCloseable. And what does it even mean to auto-detect when a stream isn't used any more? When it hits EOF? No: because then we break the case where a user wants to perform special logic on EOF, or seek away from EOF. How about when it "goes out of scope"? Other than TWR, this is not possible, since (unlike, say, C++) Java scope is not directly coupled to memory management. The clearest and least-surprising mechanism to declare that we're done with a stream is not automatic, but explicit.

try-with-resources and exception handling are features, not bugs. The vastly less complicated approach is to use Java as Java was intended - write an AutoCloseable utility class.

Very.

try-with-resources and exception handling are features, not bugs. The vastly less complicated approach is to use Java as Java was intended - write an AutoCloseable utility class. For a very typical example of a situation where the JDK itself operates this way, see DirectoryStream. This is how Java is designed to work. Attempting otherwise is going to be some mix of non-constructive, difficult, non-idiomatic, or "the bad kind of surprise".

Very.

TWR keeps getting forgotten.

If someone keeps forgetting to hold the steering wheel, the solution is not to remove the steering wheel and build a more complicated, less accessible steering wheel behind the dashboard. Your entry-level developers should be learning how to write idiomatic Java, rather than learning (or - as the case may be - not learning) a custom framework that attempts to be too clever. There are standard tools, some mentioned in the comments, that flag when resources are mismanaged. Learn to use these tools.

To quote a separate commenter,

I would like to consume a stream shared between threads. The stream should autodetect when it is not used any more

It's only safe to close the stream once we can guarantee that all threads are done (i.e. after a join). This can still use AutoCloseable. And what does it even mean to auto-detect when a stream isn't used any more? When it hits EOF? No: because then we break the case where a user wants to perform special logic on EOF, or seek away from EOF. How about when it "goes out of scope"? Other than TWR, this is not possible, since (unlike, say, C++) Java scope is not directly coupled to memory management. The clearest and least-surprising mechanism to declare that we're done with a stream is not automatic, but explicit.

Source Link
Reinderien
  • 72.3k
  • 5
  • 76
  • 260

try-with-resources and exception handling are features, not bugs. The vastly less complicated approach is to use Java as Java was intended - write an AutoCloseable utility class.

I am opening a connection to the internet just to check if my stream is parallel. I don't know how terrible that is

Very.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.stream.Stream;

public class Main {
    public static class HTTPLineStream implements AutoCloseable {
        public final URI uri;
        private final BufferedReader reader;

        public HTTPLineStream(URI uri) throws IOException {
            this.uri = uri;
            reader = new BufferedReader(
                new InputStreamReader(
                    uri.toURL().openStream()
                )
            );
        }
        public HTTPLineStream(String uri) throws URISyntaxException, IOException {
            this(new URI(uri));
        }

        public Stream<String> lines() {
            return reader.lines();
        }

        @Override
        public void close() throws IOException {
            // also closes the inner stream
            reader.close();
        }
    }


    public static void main(String[] args) {
        try (HTTPLineStream stream = new HTTPLineStream(
            "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv"
        )) {
            stream.lines()
                .limit(10)
                .forEach(System.out::println);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}
date,cases,deaths
2020-01-21,1,0
2020-01-22,1,0
2020-01-23,1,0
2020-01-24,2,0
2020-01-25,3,0
2020-01-26,5,0
2020-01-27,5,0
2020-01-28,5,0
2020-01-29,5,0