summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenSplitter.java
blob: 68dde25ed7a939446b71ae32d3f98e7a2c27c864 (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
package bjc.utils.funcutils;

import java.util.function.BiFunction;

import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IFunctionalList;

final class TokenSplitter
		implements BiFunction<String, String, IFunctionalList<String>> {
	private String tokenToSplit;

	public TokenSplitter(String tok) {
		this.tokenToSplit = tok;
	}

	@Override
	public IFunctionalList<String> apply(String operatorName,
			String operatorRegex) {
		if (operatorName == null) {
			throw new NullPointerException(
					"Operator name must not be null");
		} else if (operatorRegex == null) {
			throw new NullPointerException(
					"Operator regex must not be null");
		}

		if (tokenToSplit.contains(operatorName)) {
			if (StringUtils.containsOnly(tokenToSplit, operatorRegex)) {
				return new FunctionalList<>(tokenToSplit);
			}

			IFunctionalList<String> splitTokens = new FunctionalList<>(
					tokenToSplit.split(operatorRegex));

			IFunctionalList<String> result = new FunctionalList<>();

			int tokenExpansionSize = splitTokens.getSize();

			splitTokens.forEachIndexed((tokenIndex, token) -> {

				if (tokenIndex != tokenExpansionSize && tokenIndex != 0) {
					result.add(operatorName);
					result.add(token);
				} else {
					result.add(token);
				}
			});

			return result;
		}

		return new FunctionalList<>(tokenToSplit);
	}
}