This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConditionGenerator.java
96 lines (84 loc) · 2.22 KB
/
ConditionGenerator.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
package nl.utwente.cs.pxml;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
* Class that generates {@link Condition} instances from a string. Meant to be
* used as
*
* <pre>
* for (Condition condition : new ConditionGenerator(conditions)) {
* // do something with condition
* }
* </pre>
*
* @author Mattijs Ugen
*/
public class ConditionGenerator implements Iterable<Condition>, Iterator<Condition> {
// pattern separating both conditions and names and values within a
// condition
public static final Pattern PATTERN = Pattern.compile("[\\s=]+");
protected Scanner input;
/**
* Creates a new ConditionGenerator generating Conditions from the provided
* 'stream'.
*
* @param descriptor
* A string containing conditions in "name=value"-form.
*/
public ConditionGenerator(String descriptor) {
this.input = new Scanner(descriptor).useDelimiter(ConditionGenerator.PATTERN);
}
@Override
public Iterator<Condition> iterator() {
// class is its own iterator, just return this
return this;
}
@Override
public boolean hasNext() {
return this.input.hasNext();
}
/**
* Provides the next Condition in the 'stream'.
*
* @throws NoSuchElementException
* - when the stream is malformed when the end of the stream is
* reached.
*/
@Override
public Condition next() {
String name = this.input.next();
int value = this.input.nextInt();
return new Condition(name, value);
}
/**
* Unsupported operation.
*
* @throws UnsupportedOperationException
* - always
*/
@Override
public void remove() {
throw new UnsupportedOperationException("cannot remove condition, generated from stream");
}
/**
* Tests DescriptionParser by feeding it input from the arguments provided
* by the user.
*
* @param args
*/
public static void main(String... args) {
for (String arg : args) {
try {
System.out.println("for input '" + arg + "'");
for (Condition cond : new ConditionGenerator(arg)) {
System.out.print(cond + " ");
}
System.out.println();
} catch (NoSuchElementException e) {
System.err.println("input was malformed");
}
}
}
}