The situation is as follows: I implement CharSequence over large text files (in order to be able to pass them to a Pattern).
I have a list of CharWindows which is a pretty simple class:
public final class CharWindow
{
private final long fileOffset;
private final long mappingLength;
private final int charOffset;
private final int charLength;
// Constructor, methods, etc etc
}
In a separate class, I generate instances of CharWindows starting from the beginning of the file to the end; during that process, I increment an AtomicInteger (let us call it totalChars) which is the total number of characters in the file.
Now, let us imagine for a moment that a caller calls .charAt(25030) on the CharSequence; but at that time, the reader/decoder class has only completed (successfully) decoding 10430 characters; and it continues on: 15640, 21032, 25602 -- each time updating totalChars. And other callers may call .charAt() with different arguments as well.
Let us say that the reader/decoder class has a (thread safe, concurrent-friendly) method has a .needChars() with an int as an argument and the code of .charAt() in the CharSequence implementation reads:
@Override
public char charAt(final int index)
{
readerDecoder.needChars(index);
// do whatever is needed to read the chars
}
Is there a way implement .needChars() so that it block waiting for totalChars to reach an appropriate value?