RFR: 8341566: Adding factory for non-synchronized CharSequence Reader [v4]

Jaikiran Pai jpai at openjdk.org
Tue Oct 8 12:49:59 UTC 2024


On Tue, 8 Oct 2024 12:32:00 GMT, Jaikiran Pai <jpai at openjdk.org> wrote:

>> I would treat this specific scenario as one of the "no concurrent usage" examples. Note that by this principle, mutable objects like `StringBuilder` should not override object comparison methods as these states can change, but they do :(
>
>> The question is: Would that make any sense in the end?
> 
> Consider the example of `StringBuffer`, which is a `CharSequence`. Wouldn't something like the following be a logical use of `Reader.of(CharSequence)`, where you create a `Reader` for the underlying sequence (which is not yet populated) and then keep reading through the `Reader` until the underlying sequence's data is finished?
> 
> 
> final StringBuffer sb = new StringBuffer();
> try (final Reader reader = Reader.of(sb)) {
>     final ContentGenerator cg = new ContentGenerator(sb);
>     Thread.ofPlatform().start(cg);
>     int numRead = 0;
>     while (numRead != -1) {
>         final char[] content = new char[1024];
>         numRead = reader.read(content); // wait for content
>     }
> }
> 
> private class ContentGenerator {
>     final StringBuffer sb;
>     ContentGenerator(StringBuffer sb) {
>         this.sb = sb;
>     }
> 
>     @Override
>     run() {
>         while (true) {
>             String foo = getSomeContent(); // maybe blocking
>             sb.append(foo);
>         }
>     }
> }

>  I cannot image any scenario where such a program would result in useful outcome.

An additional point of reference is the current default implementation of CharSequence's `public default IntStream chars()`, which returns an IntStream of char values from the sequence and the implementation of which doesn't consider the `length()` to be fixed.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/21371#discussion_r1791811620


More information about the core-libs-dev mailing list