summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/TwoLevelSplitter.java
blob: 38f303d22ed017bd4935e436c9b7cedb97f60ae7 (plain)
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
package bjc.utils.parserutils.splitter;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Implementation of a splitter that runs in two passes.
 * 
 * This is useful because {@link SimpleTokenSplitter} doesn't like handling both
 * <= and = without mangling them.
 * 
 * The first pass splits on compound operators, which are built up from simple
 * operators.
 * 
 * The second pass removes simple operators.
 * 
 * @author EVE
 *
 */
public class TwoLevelSplitter implements TokenSplitter {
	private SimpleTokenSplitter	high;
	private SimpleTokenSplitter	low;

	/**
	 * Create a new two level splitter.
	 */
	public TwoLevelSplitter() {
		high = new SimpleTokenSplitter();
		low = new SimpleTokenSplitter();
	}

	@Override
	public String[] split(String inp) {
		List<String> ret = new ArrayList<>();

		String[] partials = high.split(inp);

		for(String partial : partials) {
			String[] finals = low.split(partial);

			for(String fin : finals) {
				ret.add(fin);
			}
		}

		return ret.toArray(new String[ret.size()]);
	}

	/**
	 * Adds compound operators to split on.
	 * 
	 * @param delims
	 *                The compound operators to split on.
	 */
	public void addCompoundDelim(String... delims) {
		for(String delim : delims) {
			high.addDelimiter(delim);

			low.addNonMatcher(Pattern.quote(delim));
		}
	}

	/**
	 * Adds simple operators to split on.
	 * 
	 * @param delims
	 *                The simple operators to split on.
	 */
	public void addSimpleDelim(String... delims) {
		for(String delim : delims) {
			low.addDelimiter(delim);
		}
	}

	/**
	 * Adds repeated compound operators to split on.
	 * 
	 * @param delims
	 *                The repeated compound operators to split on.
	 */
	public void addCompoundMulti(String... delims) {
		for(String delim : delims) {
			high.addMultiDelimiter(delim);

			low.addNonMatcher("(?:" + delim + ")+");
		}
	}

	/**
	 * Adds simple compound operators to split on.
	 * 
	 * @param delims
	 *                The repeated simple operators to split on.
	 */
	public void addSimpleMulti(String... delims) {
		for(String delim : delims) {
			low.addMultiDelimiter(delim);
		}
	}

	/**
	 * Ready the splitter for use.
	 */
	public void compile() {
		high.compile();

		low.compile();
	}
}