forked from hazukac/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContext.java
More file actions
36 lines (35 loc) · 1.09 KB
/
Copy pathContext.java
File metadata and controls
36 lines (35 loc) · 1.09 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
import java.util.*;
public class Context {
private StringTokenizer tokenizer;
private String currentToken;
public Context(String text) {
this.tokenizer = new StringTokenizer(text);
this.nextToken();
}
public String nextToken() {
if (this.tokenizer.hasMoreTokens()) {
this.currentToken = this.tokenizer.nextToken();
} else {
this.currentToken = null;
}
return this.currentToken;
}
public String currentToken() {
return this.currentToken;
}
public void skipToken(String token) throws ParseException {
if (! token.equals(this.currentToken)) {
throw new ParseException("Warning: try to skip `" + token + "` but found `" + this.currentToken);
}
this.nextToken();
}
public int currentNumber() throws ParseException {
int number = 0;
try {
number = Integer.parseInt(this.currentToken);
} catch (NumberFormatException e) {
throw new ParseException("Warning: " + e.toString());
}
return number;
}
}