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

import java.util.Iterator;
import java.util.function.BiFunction;

import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;

final class TokenDeaffixer
		implements BiFunction<String, String, IList<String>> {
	private String token;

	public TokenDeaffixer(String tok) {
		token = tok;
	}

	@Override
	public IList<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 (StringUtils.containsOnly(token, operatorRegex)) {
			return new FunctionalList<>(token);
		} else if (token.startsWith(operatorName)) {
			if(token.endsWith(operatorName)) {
				return new FunctionalList<>(operatorName, token.split(operatorRegex)[1], operatorName);
			}

			return new FunctionalList<>(operatorName, token.split(operatorRegex)[1]);
		} else if (token.endsWith(operatorName)) {
			return new FunctionalList<>(token.split(operatorRegex)[0], operatorName);
		} else if (token.contains(operatorName)) {
			String[] tokenParts = token.split(operatorRegex);

			IList<String> returned = new FunctionalList<>();
			
			for(int i = 0; i < tokenParts.length; i++) {
				returned.add(tokenParts[i]);

				if(i < tokenParts.length - 1) {
					returned.add(operatorName);
				}
			}

			return returned;
		} else {
			return new FunctionalList<>(token);
		}
	}
}