View Javadoc

1   package net.sf.adatagenerator.modifiers;
2   
3   import java.util.ArrayList;
4   import java.util.Random;
5   
6   public class ModifierUtilities {
7   
8   	private static ArrayList<Character> alphaSet() {
9   		ArrayList<Character> alpha = new ArrayList<Character>();
10  		for (char c = 'A'; c <= 'Z'; c++) {
11  			alpha.add(c);
12  		}
13  		for (char c = 'a'; c <= 'z'; c++) {
14  			alpha.add(c);
15  		}
16  		return alpha;
17  	}
18  
19  	private static ArrayList<Character> numericSet() {
20  		ArrayList<Character> numeric = new ArrayList<Character>();
21  		for (char c = 0; c <= 9; c++) {
22  			numeric.add(c);
23  		}
24  		for (char c = 'a'; c <= 'z'; c++) {
25  			numeric.add(c);
26  		}
27  		return numeric;
28  	}
29  
30  	private static ArrayList<Character> alphaNumericSet() {
31  		ArrayList<Character> alphaNumeric = new ArrayList<Character>();
32  		alphaNumeric.addAll(alphaSet());
33  		alphaNumeric.addAll(numericSet());
34  		return alphaNumeric;
35  	}
36  
37  	public static int errorPosition(String value, int offset) {
38  		int len = value.length();
39  		if (len == 0) {
40  			return 0;
41  		}
42  		int maxReturnPos = len - 1 + offset;
43  		float randomPos = (float) new Random().nextGaussian();
44  		int rPos = Math.max(0, Math.round(randomPos));
45  		return Math.min(rPos, maxReturnPos);
46  	}
47  
48  	public static char errorChar(Object value) {
49  		String stringifiedValue = (String) value;
50  		Random rand = new Random();
51  
52  		if (stringifiedValue.matches("[a-zA-Z]*")) {
53  			return alphaSet().get(rand.nextInt(alphaSet().size()));
54  
55  		} else if (stringifiedValue.matches("[0-9]*")) {
56  			return numericSet().get(rand.nextInt(numericSet().size()));
57  
58  		} else if (stringifiedValue.matches("[0-9a-zA-Z]*")) {
59  			return alphaNumericSet()
60  					.get(rand.nextInt(alphaNumericSet().size()));
61  		} else {
62  			return 0;
63  		}
64  
65  	}
66  
67  }