Skip to content

Instantly share code, notes, and snippets.

@saket
Created May 9, 2016 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saket/12c886207fa0c0e813f3eb1b424f28f8 to your computer and use it in GitHub Desktop.
Save saket/12c886207fa0c0e813f3eb1b424f28f8 to your computer and use it in GitHub Desktop.
Copied from some project by Jake Wharton. He describees it as "A SpannableStringBuilder wrapper whose API doesn't make me want to stab my eyes out"
package com.jakewharton.u2020.ui.misc;
import android.text.SpannableStringBuilder;
import java.util.ArrayDeque;
import java.util.Deque;
import static android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE;
/** A {@link SpannableStringBuilder} wrapper whose API doesn't make me want to stab my eyes out. */
public class Truss {
private final SpannableStringBuilder builder;
private final Deque<Span> stack;
public Truss() {
builder = new SpannableStringBuilder();
stack = new ArrayDeque<>();
}
public Truss append(String string) {
builder.append(string);
return this;
}
public Truss append(CharSequence charSequence) {
builder.append(charSequence);
return this;
}
public Truss append(char c) {
builder.append(c);
return this;
}
public Truss append(int number) {
builder.append(String.valueOf(number));
return this;
}
/** Starts {@code span} at the current position in the builder. */
public Truss pushSpan(Object span) {
stack.addLast(new Span(builder.length(), span));
return this;
}
/** End the most recently pushed span at the current position in the builder. */
public Truss popSpan() {
Span span = stack.removeLast();
builder.setSpan(span.span, span.start, builder.length(), SPAN_INCLUSIVE_EXCLUSIVE);
return this;
}
/** Create the final {@link CharSequence}, popping any remaining spans. */
public CharSequence build() {
while (!stack.isEmpty()) {
popSpan();
}
return builder; // TODO make immutable copy?
}
private static final class Span {
final int start;
final Object span;
public Span(int start, Object span) {
this.start = start;
this.span = span;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment