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

import java.util.Deque;

/**
 * Utility methods for operations on strings
 * 
 * @author ben
 *
 */
public class StringUtils {
	/**
	 * Checks if the given expression contains the specified operator in a
	 * situation that indicates its use as an infix operator.
	 * 
	 * @param expression
	 *            The expression to check
	 * @param operator
	 *            The operator to see if it is contained
	 * @return Whether or not the given expression contains the specified
	 *         operator as a infix operator
	 */
	public static boolean containsInfixOperator(String expression, String operator) {
		// Bit annoying to have to use a full class name, but what are you
		// going to do?
		return org.apache.commons.lang3.StringUtils.countMatches(expression, operator) == 1
				&& !expression.equalsIgnoreCase(operator)
				&& !expression.startsWith(operator);
	}

	/**
	 * Check if a string consists only of one or more matches of a regular
	 * expression
	 * 
	 * @param input
	 *            The string to check
	 * @param regex
	 *            The regex to see if the string only contains matches of
	 * @return Whether or not the string consists only of multiple matches
	 *         of the provided regex
	 */
	public static boolean containsOnly(String input, String regex) {
		/*
		 * This regular expression is fairly simple.
		 * 
		 * First, we match the beginning of the string. Then, we start a
		 * non-capturing group whose contents are the passed in regex. That
		 * group is then matched one or more times and the pattern matches
		 * to the end of the string
		 */
		if (input == null) {
			throw new NullPointerException("Input must not be null");
		} else if (regex == null) {
			throw new NullPointerException("Regex must not be null");
		}

		return input.matches("\\A(?:" + regex + ")+\\Z");
	}

	/**
	 * Indent the string being built in a StringBuilder n levels
	 * 
	 * @param builder
	 *            The builder to indent in
	 * @param levels
	 *            The number of levels to indent
	 */
	public static void indentNLevels(StringBuilder builder, int levels) {
		for (int i = 0; i < levels; i++) {
			builder.append("\t");
		}
	}

	/**
	 * Print out a deque with a special case for easily showing a deque is
	 * empty
	 * 
	 * @param <ContainedType>
	 *            The type in the deque
	 * @param queue
	 *            The deque to print
	 * @return A string version of the deque, with allowance for an empty
	 *         deque
	 */
	public static <ContainedType> String printDeque(Deque<ContainedType> queue) {
		return queue.isEmpty() ? "(none)" : queue.toString();
	}
}