-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBruteForce.java
89 lines (78 loc) · 3.8 KB
/
BruteForce.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.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.InvalidParameterException;
import java.util.function.Predicate;
public class BruteForce {
/**
* Create a file list.txt with all possible sequences of length 1 to the specified length
* that can be made with the given array.
* @param maxLength Maximal length of sequences
* @param alphabet Contains each individual element that will be used for building a sequence.
*/
public static <T> void inFile(int maxLength, T[] alphabet) {
BruteForce.inFile(1, maxLength, alphabet);
}
/**
* Create a file list.txt with all possible sequences that can be made with the given array.
* @param minLength Minimal length of sequences
* @param maxLength Maximal length of sequences
* @param alphabet Contains each individual element that will be used for building a sequence.
*/
public static <T> void inFile(int minLength, int maxLength, T[] alphabet) {
if (maxLength <= 0 || alphabet == null || alphabet.length <= 0) {
System.out.println("Parameters are incorrect !");
throw new InvalidParameterException("First parameter should be a positive integer and the second non null with at least one element inside.");
}
CycleList<T> cList = new CycleList<>(alphabet);
Cogwheel<T> cw;
PrintWriter writer = null;
try {
writer = new PrintWriter("list.txt", "UTF-8");
int length = minLength;
do {
cw = new Cogwheel<>(length++, cList);
cw.action(writer);
} while (length <= maxLength);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
System.out.println(e);
} finally {
if (writer != null) writer.close();
}
}
/**
* Find the first sequence of String that makes the predicate test returns true.
* @param maxLength Maximal length of the sequences
* @param alphabet Contains each individual element that will be used for building a sequence.
* @param pred Is the condition to return a sequence.
* @return This method returns the sequence as a String or null if nothing has been found.
*/
public static String find(int maxLength, String alphabet, Predicate<String> pred) {
return BruteForce.find(maxLength, alphabet.split(""), pred);
}
/**
* Find the first sequence that makes the predicate test returns true.
* @param maxLength Maximal length of the sequences
* @param alphabet Contains each individual element that will be used for building a sequence.
* @param pred Is the condition to return a sequence.
* @return This method returns the sequence as a String or null if nothing has been found.
*/
public static <T> String find(int maxLength, T[] alphabet, Predicate<String> pred) {
if (maxLength <= 0 || alphabet == null || alphabet.length <= 0) {
System.out.println("Parameters are incorrect !");
throw new InvalidParameterException("First parameter should be a positive integer and the second non null with at least one element inside.");
}
CycleList<T> cList = new CycleList<>(alphabet);
Cogwheel<T> cw;
StringBuilder result;
int length = 1;
do {
cw = new Cogwheel<>(length++, cList);
if ((result = cw.action(pred)) != null) return result.toString(); // Check if cw.action(p) has found something
} while (length <= maxLength);
System.out.println("Nothing has been found.");
return null;
}
}