Skip to content

Instantly share code, notes, and snippets.

@dsmiley
Created December 29, 2019 06:00
Show Gist options
  • Save dsmiley/b425b152d51d4c63f498fc84d125ea0a to your computer and use it in GitHub Desktop.
Save dsmiley/b425b152d51d4c63f498fc84d125ea0a to your computer and use it in GitHub Desktop.
class SettableBreakIterator extends BreakIterator {
private final BreakIterator delegate;
private int currentCache;
public SettableBreakIterator(BreakIterator delegate) {
this.delegate = delegate;
currentCache = delegate.current();
}
public void setPosition(int position) {
this.currentCache = position;
}
@Override
public int current() {
return currentCache;
}
@Override
public int next() {
if (currentCache == current()) {
return currentCache = delegate.next();
} else {
return currentCache = delegate.next(currentCache);
}
}
@Override
public int previous() {
if (currentCache == current()) {
return currentCache = delegate.previous();
} else {
return currentCache = delegate.preceding(currentCache);
}
}
@Override
public int first() {
return currentCache = delegate.first();
}
@Override
public int last() {
return currentCache = delegate.last();
}
@Override
public int next(int n) {
this.next(); // sets currentCache
if (n > 1) {
currentCache = delegate.next(n - 1);
}
return currentCache;
}
@Override
public int following(int offset) {
return currentCache = delegate.following(offset);
}
@Override
public int preceding(int offset) {
return currentCache = delegate.preceding(offset);
}
@Override
public boolean isBoundary(int offset) {
return delegate.isBoundary(offset);
}
@Override
public CharacterIterator getText() {
return delegate.getText();
}
@Override
public void setText(String newText) {
delegate.setText(newText);
currentCache = delegate.current();
}
@Override
public void setText(CharacterIterator newText) {
delegate.setText(newText);
currentCache = delegate.current();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment