Skip to content

Commit

Permalink
CLDR-17327 speedup VoteResolver's transcript
Browse files Browse the repository at this point in the history
- add and correct some comments
  • Loading branch information
srl295 committed Jan 24, 2024
1 parent 0701754 commit 7074dd8
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,29 @@
import java.util.stream.Collectors;

/**
* lazily-calculates the transcript. Thread safe to call get() multiple times once commit() is
* called and before clear() is called.
* A deferred list of formatted strings, one per line. The formatting is deferred until when (and
* if) get() is called. It's thread safe to call get() multiple times, but add() should be called
* from a single thread.
*
* <p>Once get() is called, the contents are 'resolved' into a string, and subsequent get() calls
* will return the same string (even if add() was called). To re-use the object, call clear() and
* then new content can be added with add().
*
* <p>In testing, there was a significant speed-up for the use case where get() might not be called.
*/
public class DeferredTranscript implements Supplier<String> {

private final class FormatEntry {
public final CharSequence fmt;
public final Object[] args;

/** Add a new FormatEntry as a deferred format */
public FormatEntry(String fmt, Object[] args) {
this.fmt = fmt;
this.args = args;
}

/** Output this entry as a CharSequence, performing any formatting needed. */
public final CharSequence format() {
if (args == null || args.length == 0) {
return fmt;
Expand All @@ -30,14 +39,18 @@ public final CharSequence format() {
}
}

/** clear the DeferredTranscript on startup */
public DeferredTranscript() {
clear();
}

/** The first time this is read, it will calculate the entire string. */
private Supplier<String> delegate = null;

/** cached formats */
private List<FormatEntry> formats;

/** reset this transcript so it can be used again. */
/** reset this transcript so it can be added to again. */
public void clear() {
// the delegate only calculates the value once, the first time it is accessed.
delegate =
Expand All @@ -60,7 +73,8 @@ public final String get() {
}

/**
* Add a formatted entry. Will be ignored if get() has been called since clear()
* Add a formatted entry. Will have no effect if get() has been called since clear() or object
* construction.
*
* @param fmt the string or pattern string
* @param args if non-null, arguments to String.format()
Expand Down

0 comments on commit 7074dd8

Please sign in to comment.