Menu

[r16]: / trunk / src / MyRand.java  Maximize  Restore  History

Download this file

287 lines (172 with data), 5.7 kB

  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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class MyRand {
private ArrayList<Integer> ids;
private Integer r_count;
private static DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
private static long INITIAL_DATE_MILLIS;
private static long FINAL_DATE_MILLIS;
static {
try {
//INITIAL_DATE_MILLIS = formatter.parse("1900-01-01").getTime(); //112 years before today.
INITIAL_DATE_MILLIS = formatter.parse("1995-01-01").getTime(); //112 years before today.
FINAL_DATE_MILLIS = (new Date()).getTime();
}
catch (ParseException e) {
e.printStackTrace();
}
}
private static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
private static final long DIAS_COUNT = (FINAL_DATE_MILLIS - INITIAL_DATE_MILLIS) / MILLIS_PER_DAY;
public MyRand(Integer count){
this.r_count = count;
this.ids = new ArrayList<Integer>();
this.reSetRandomInt();
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println( MyRand.RandomString(20) );
System.out.println( MyRand.RandomString(20) );
System.out.println( MyRand.RandomString(10) );
System.out.println();
System.out.println();
System.out.println( MyRand.RandomDate() );
System.out.println( MyRand.RandomDate() );
System.out.println( MyRand.RandomDate() );
System.out.println( MyRand.RandomDate() );
System.out.println( MyRand.RandomDate() );
System.out.println();
System.out.println();
for(int i = 0; i < 100; i++){
System.out.println( MyRand.RandomInt(1, 55));
}
for(int i = 0; i < 5; i++){
System.out.println( MyRand.RandomDate("2012-10-01", "2012-10-02") );
}
for(int i = 0; i < 5; i++){
System.out.println( MyRand.MasKedNumber(null) );
}
for(int i = 0; i < 10000; i++){
System.out.print( (MyRand.RandomInt(1, 100) == 10)+ ";\n ");
//System.out.println(MyRand.RandomUF());
/*
if(MyRand.RandomInt(0, 10) == 9){
System.out.println("xzz");
}
*/
}
}
public static String RandomDate(){
Date antes = new Date(INITIAL_DATE_MILLIS + MILLIS_PER_DAY * MyRand.RandomInt(1, (int)DIAS_COUNT) );
return formatter.format( antes );
}
public static String RandomUF(){
String[] estados = {"AC","AL","AP","AM","BA","CE","DF","ES","GO","MA","MT","MS","MG","PA","PB","PR","PE","PI","RJ","RN","RS","RO","RR","SC","SP","SE","TO"};
Integer i = RandomInt(0,estados.length);
return estados[i];
}
public void reSetRandomInt(){
for(int i = 0; i < this.r_count; i++){
ids.add(i+1);
}
}
public Integer RandomId(){
Integer id = ids.get(RandomInt(0,ids.size()-1));
ids.remove(id);
if(ids.size() == 0){
this.reSetRandomInt();
}
return id;
}
public static String RandomDate(String startDate, String endDate){
try {
long start_date = formatter.parse(startDate).getTime();
long end_date = formatter.parse(endDate).getTime();
long days = (end_date - start_date) / MILLIS_PER_DAY;
Date data = new Date( start_date + MILLIS_PER_DAY * MyRand.RandomInt(0, (int)days + 1 ) );
return formatter.format( data );
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/*
* @param i integer from 0 to 60
* */
private static char getCustomChar(int i){
//Space
char c = ' ';
if(i < 26){
//Upper Case
c = (char) (i + 65);
}
else if(i < 52){
//Lower Case; 25 <= x <= 50
c = (char) (i + 71);
}
else if (i < 61)
{
// Numbers
c = (char) (i - 3);
}
return c;
}
/**
* Cria um string randomizado do tamanho que for solicitado.
* @param size vai ser o tamanho do campo na tabela
*/
public static String RandomString(int size){
char[] c = new char[size];
for(int i = 0; i < size; i++){
c[i] = MyRand.getCustomChar( MyRand.RandomInt(0, 62) );
}
return new String(c);
}
/**
* @param start limite inferior do numero
* @param size amplitude do numero
*/
public static Integer RandomInt(int start, int end){
return (int)(start + Math.random() * end);
}
public static String RandomNumber(int size){
String number = "";
for(int i = 0; i < size; i++){
number += MyRand.RandomInt(0,9).toString();
}
return number;
}
public static String MasKedNumber(String mask){
if(mask == null){
mask = new String("+(55) ?? ???? - ????"); //16 characters
}
char[] t = mask.toCharArray();
mask = "";
for(int i = 0; i < t.length; i++){
if(t[i] == '?'){ t[i] = MyRand.RandomInt(0, 10).toString().charAt(0); }
mask += t[i];
}
return mask;
}
public static String Randomize(MyColumn columnType){
if( columnType.getType().toLowerCase().equals("int") ){
return MyRand.RandomInt(0, 100000).toString();
}
else if( columnType.getType().toLowerCase().equals("date") ){
return MyRand.RandomDate();
}
else if( columnType.getType().toLowerCase().equals("numeric") ){
return MyRand.RandomNumber(columnType.getSize());
}
else if( columnType.getType().toLowerCase().equals("mask") ){
return MyRand.MasKedNumber(columnType.getMask());
}
else{
return MyRand.RandomString(columnType.getSize());
}
}
}