Skip to content

Instantly share code, notes, and snippets.

@bnickel
Forked from yogurtearl/gist:894050af6fbbdf3c6b0e
Last active August 29, 2015 14:02
Show Gist options
  • Save bnickel/5c94a8ea6c6da7dc3281 to your computer and use it in GitHub Desktop.
Save bnickel/5c94a8ea6c6da7dc3281 to your computer and use it in GitHub Desktop.
// Good idea or Bad idea?
// Lets say you have to call some API with lots of parameters that you don't control
// Like this http://developer.android.com/reference/android/text/StaticLayout.html#StaticLayout(java.lang.CharSequence, int, int, android.text.TextPaint, int, android.text.Layout.Alignment, float, float, boolean, android.text.TextUtils.TruncateAt, int)
// Do you prefer Option #1 or Option #2? Is Option #2 a terrible idea?
// I suppose Option #3 could be to wrap it in a Builder
class Foo {
void draw() {
new StaticLayout(
"text",
2,
4,
new TextPaint(),
450,
Layout.Alignment.ALIGN_CENTER,
4.5f,
2,
false,
TextUtils.TruncateAt.END,
34
);
// option #1
final String source = "text";
final int bufstart = 2;
final int bufend = 4;
final int outerwidth = 450;
final float spacingmult = 4.5f;
final int spacingadd = 2;
final boolean includepad = false;
final int ellipsizedWidth = 34;
new StaticLayout(
source,
bufstart,
bufend,
new TextPaint(),
outerwidth,
Layout.Alignment.ALIGN_CENTER,
spacingmult,
spacingadd,
includepad,
TextUtils.TruncateAt.END,
ellipsizedWidth
);
// option #2
new StaticLayout(
source("text"),
bufferStart(2),
bufferStop(4),
new TextPaint(),
outerWidth(450),
Layout.Alignment.ALIGN_CENTER,
spacingMultplier(4.5f),
spacingAdding(2),
dontIncludePadding(),
TextUtils.TruncateAt.END,
ellipsizedWidth(34)
);
}
private int ellipsizedWidth(final int i) {
return i;
}
private boolean dontIncludePadding() {
return false;
}
private float spacingAdding(final int i) {
return 0;
}
private float spacingMultplier(final float v) {
return v;
}
private int outerWidth(final int i) {
return i;
}
private int bufferStop(final int i) {
return i;
}
private int bufferStart(final int i) {
return i;
}
private String source(final String text) {
return text;
}
}
new StaticLayoutBuilder()
.source("text")
.bufferRange(2,4)
.layout();
public class StaticLayoutBuilder {
// You could set sane defaults.
String source;
int bufferStart;
int bufferEnd;
int outerWidth;
float spacingMultiplier;
int spacingAdd;
boolean includePadding;
int ellipsizedWidth;
StaticLayoutBuilder source(String source) {
this.source = source;
return this;
}
StaticLayoutBuilder bufferRange(int bufferStart, int bufferEnd) {
this.bufferStart = bufferStart;
this.bufferEnd = bufferEnd;
return this;
}
...
StaticLayout layout() {
return new StaticLayout(source, bufferStart, bufferEnd, ...);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment