-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringMerger.java
89 lines (71 loc) · 2.24 KB
/
StringMerger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.ArrayList;
import java.util.List;
public static class StringMerger {
protected int mMaxLength;
protected char mBgChar;
protected List<Layer> mLayers;
public StringMerger() {
mMaxLength = 30;
mBgChar = ' '; // a space
mLayers = new ArrayList<>();
}
public StringMerger(int maxLength, char bgChar, List<Layer> layers) {
mMaxLength = maxLength;
mBgChar = bgChar;
mLayers = layers;
}
public String getMergedString() {
char[] chars = new char[mMaxLength];
for (int i = 0; i < chars.length; i++) {
chars[i] = mBgChar;
}
// int a = 0;
for (Layer layer : mLayers) {
if (layer.writeFrom.equals("right")) {
// Reverse chars
chars = new StringBuilder(new String(chars)).reverse().toString().toCharArray();
layer.text = new StringBuilder(layer.text).reverse().toString();
}
for (int i = 0; i < chars.length; i++) {
try {
chars[i + layer.start] = layer.text.charAt(i);
} catch (StringIndexOutOfBoundsException e) {
// Do nothing.
}
}
if (layer.writeFrom.equals("right")) {
// Reverse chars (to normal)
chars = new StringBuilder(new String(chars)).reverse().toString().toCharArray();
}
}
return new StringBuilder(new String(chars)).toString();
}
public static class Layer {
public String writeFrom;
public int start;
public String text;
public Layer(String writeFrom, int start, String text) {
this.writeFrom = writeFrom;
this.start = start;
this.text = text;
}
}
public int getMaxLength() {
return mMaxLength;
}
public void setMaxLength(int maxLength) {
mMaxLength = maxLength;
}
public char getBgChar() {
return mBgChar;
}
public void setBgChar(char bgChar) {
mBgChar = bgChar;
}
public List<Layer> getLayers() {
return mLayers;
}
public void setLayers(List<Layer> layers) {
mLayers = layers;
}
}