-
Notifications
You must be signed in to change notification settings - Fork 31
/
LoremComponent.java
174 lines (134 loc) · 4.65 KB
/
LoremComponent.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package io.kimo.lib.faker.component.text;
import android.content.Context;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import io.kimo.lib.faker.R;
import io.kimo.lib.faker.api.LoremAPI;
import io.kimo.lib.faker.component.FakerTextComponent;
public class LoremComponent extends FakerTextComponent implements LoremAPI {
public static final int [] DEFAULT_RANDOM_NUMBERS_POOL = {3,4,5,6,7};
public static final int DEFAULT_NUMBER_OF_CHARACTERS = 255;
public static final String ALPHA_NUMERIC_CHARACTERS = "0123456789abcdefghijklmnopqrstuwvxyz";
public static final String INVALID_ARGUMENT_ERROR_MSG = "Argument should be bigger than 0";
private List<String> loremWords;
public LoremComponent(Context context) {
super(context);
loremWords = Arrays.asList(context.getResources().getStringArray(R.array.lorem_words));
}
@Override
public String randomText() {
int method = (int)(Math.random() * 10);
switch (method % 4) {
case 0:
return characters();
case 1:
return words();
case 2:
return sentences();
case 3:
return paragraphs();
default:
return "";
}
}
/**
* Provides a random words from the words list
* @return a String with the word
*/
@Override
public String word() {
return loremWords.get(new Random().nextInt(loremWords.size()));
}
/**
* Provies random words from the words list.
* The amount of words varies from 3 to 7.
* @return a String with the words
*/
@Override
public String words() {
int numberOfWords = DEFAULT_RANDOM_NUMBERS_POOL[new Random().nextInt(DEFAULT_RANDOM_NUMBERS_POOL.length)];
return words(numberOfWords);
}
/**
* Provides the
* @param numberOfWords
* @return a String with the words
*/
@Override
public String words(int numberOfWords) {
if (numberOfWords < 1) {
throw new IllegalArgumentException(INVALID_ARGUMENT_ERROR_MSG);
}
List<String> loremWords = new ArrayList<>();
for(int i = 0; i < numberOfWords; i++) {
loremWords.add(word());
}
return TextUtils.join(" ", loremWords);
}
@Override
public String sentence() {
String randomWords = words();
return randomWords.substring(0,1).toUpperCase() + randomWords.substring(1) + ".";
}
@Override
public String sentences() {
int randomNumberOfSentences = DEFAULT_RANDOM_NUMBERS_POOL[new Random().nextInt(DEFAULT_RANDOM_NUMBERS_POOL.length)];
return sentences(randomNumberOfSentences);
}
@Override
public String sentences(int numberOfSentences) {
if (numberOfSentences < 1) {
throw new IllegalArgumentException(INVALID_ARGUMENT_ERROR_MSG);
}
List<String> sentences = new ArrayList<>();
for(int i = 0; i < numberOfSentences; i++) {
sentences.add(sentence());
}
return TextUtils.join(" ", sentences);
}
@Override
public String character() {
return characters(1);
}
@Override
public String characters() {
return characters(DEFAULT_NUMBER_OF_CHARACTERS);
}
@Override
public String characters(int numberOfCharacters) {
if(numberOfCharacters < 1) {
throw new IllegalArgumentException(INVALID_ARGUMENT_ERROR_MSG);
}
StringBuilder randomCharacters = new StringBuilder();
for(int i = 0; i < numberOfCharacters; i++) {
randomCharacters.append(ALPHA_NUMERIC_CHARACTERS.charAt(new Random().nextInt(ALPHA_NUMERIC_CHARACTERS.length())));
}
return randomCharacters.toString();
}
@Override
public String paragraph() {
return paragraphs(1);
}
@Override
public String paragraphs() {
int randomNumberOfParagraphs = DEFAULT_RANDOM_NUMBERS_POOL[new Random().nextInt(DEFAULT_RANDOM_NUMBERS_POOL.length)];
return paragraphs(randomNumberOfParagraphs);
}
@Override
public String paragraphs(int numberOfParagraphs) {
if(numberOfParagraphs < 1) {
throw new IllegalArgumentException(INVALID_ARGUMENT_ERROR_MSG);
}
StringBuilder paragraphs = new StringBuilder();
for(int i = 0; i < numberOfParagraphs; i++) {
paragraphs.append(sentences());
if(i != numberOfParagraphs-1) {
paragraphs.append("\n");
}
}
return paragraphs.toString();
}
}