summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/TwoLevelSplitter.java
blob: 92b9de05520aad1e9bbef799b6acdf02097a40c4 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
 *
 */
@Deprecated
public class TwoLevelSplitter implements TokenSplitter {
	private final SimpleTokenSplitter	high;
	private final SimpleTokenSplitter	low;

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

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

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

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

			for (final 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(final String... delims) {
		for (final 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(final String... delims) {
		for (final 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(final String... delims) {
		for (final 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(final String... delims) {
		for (final String delim : delims) {
			low.addMultiDelimiter(delim);
		}
	}

	/**
	 * Exclude strings matching a regex from both splits.
	 *
	 * @param exclusions
	 *                The regexes to exclude matches for.
	 */
	public void exclude(final String... exclusions) {
		for (final String exclusion : exclusions) {
			high.addNonMatcher(exclusion);

			low.addNonMatcher(exclusion);
		}
	}

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

		low.compile();
	}
}