/* * Copyright 2014 Jake Wharton. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ticketmaster.servos.util; 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. * * Extremely simple wrapper around SpannableStringBuilder to make the API more logical and less awful. Apache 2 licensed. * * https://gist.github.com/JakeWharton/11274467 * * @author Jake Wharton */ public class Truss { private final SpannableStringBuilder builder; private final Deque<Span> stack; public Truss() { builder = new SpannableStringBuilder(); stack = new ArrayDeque<Span>(); } 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; } private static final class Span { final int start; final Object span; public Span(int start, Object span) { this.start = start; this.span = span; } } }