@Override public void outAOperationSet(AOperationSet node) { try { CharSet cs1 = (CharSet) getOut(node.getLeft()); CharSet cs2 = (CharSet) getOut(node.getRight()); char binop = ((Character) getOut(node.getBinOp())).charValue(); switch (binop) { case '+': { setOut(node, cs1.union(cs2)); } break; case '-': { setOut(node, cs1.diff(cs2)); } break; } } catch (Exception e) { throw new RuntimeException(node + " is invalid."); } // free memory if (getOut(node.getLeft()) != null) setOut(node.getLeft(), null); if (getOut(node.getBinOp()) != null) setOut(node.getBinOp(), null); if (getOut(node.getRight()) != null) setOut(node.getRight(), null); }
/** * Implementation of delete and keep * * @param str String to modify characters within * @param set String[] set of characters to modify * @param expect whether to evaluate on match, or non-match * @return the modified String, not null */ private static String modify(final String str, final String[] set, final boolean expect) { final CharSet chars = CharSet.getInstance(set); final StringBuilder buffer = new StringBuilder(str.length()); final char[] chrs = str.toCharArray(); final int sz = chrs.length; for (int i = 0; i < sz; i++) { if (chars.contains(chrs[i]) == expect) { buffer.append(chrs[i]); } } return buffer.toString(); }
/** * Takes an argument in set-syntax, see evaluateSet, and identifies whether any of the characters * are present in the specified string. * * <p> * * <pre> * CharSetUtils.containsAny(null, *) = false * CharSetUtils.containsAny("", *) = false * CharSetUtils.containsAny(*, null) = false * CharSetUtils.containsAny(*, "") = false * CharSetUtils.containsAny("hello", "k-p") = true * CharSetUtils.containsAny("hello", "a-d") = false * </pre> * * @param str String to look for characters in, may be null * @param set String[] set of characters to identify, may be null * @return whether or not the characters in the set are in the primary string * @see CharSet#getInstance(java.lang.String...) for set-syntax. * @since 3.2 */ public static boolean containsAny(final String str, final String... set) { if (StringUtils.isEmpty(str) || deepEmpty(set)) { return false; } final CharSet chars = CharSet.getInstance(set); for (final char c : str.toCharArray()) { if (chars.contains(c)) { return true; } } return false; }
/** * Takes an argument in set-syntax, see evaluateSet, and returns the number of characters present * in the specified string. * * <p> * * <pre> * CharSetUtils.count(null, *) = 0 * CharSetUtils.count("", *) = 0 * CharSetUtils.count(*, null) = 0 * CharSetUtils.count(*, "") = 0 * CharSetUtils.count("hello", "k-p") = 3 * CharSetUtils.count("hello", "a-e") = 1 * </pre> * * @param str String to count characters in, may be null * @param set String[] set of characters to count, may be null * @return the character count, zero if null string input * @see CharSet#getInstance(java.lang.String...) for set-syntax. */ public static int count(final String str, final String... set) { if (StringUtils.isEmpty(str) || deepEmpty(set)) { return 0; } final CharSet chars = CharSet.getInstance(set); int count = 0; for (final char c : str.toCharArray()) { if (chars.contains(c)) { count++; } } return count; }
/** * Creates values that are different from expected, replacing chars with invalid ones using some * heuristics and random probability. Relates to generated charactgers, length is controlled with * the "invalid" variable. * * @return the next generated value. */ public String randomInvalid() { if (rand == null) throw new IllegalStateException("You need to set seed before using data objects"); int length = length(); char[] c = new char[length]; for (int i = 0; i < length; i++) { float f = rand.nextFloat(0, 1); if (f > invalidProbability) { c[i] = chars.random(); } else { c[i] = chars.invalidRandom(); } } String next = new String(c); history.add(next); record(next); return next; }
/** * Squeezes any repetitions of a character that is mentioned in the supplied set. * * <p> * * <pre> * CharSetUtils.squeeze(null, *) = null * CharSetUtils.squeeze("", *) = "" * CharSetUtils.squeeze(*, null) = * * CharSetUtils.squeeze(*, "") = * * CharSetUtils.squeeze("hello", "k-p") = "helo" * CharSetUtils.squeeze("hello", "a-e") = "hello" * </pre> * * @param str the string to squeeze, may be null * @param set the character set to use for manipulation, may be null * @return the modified String, {@code null} if null string input * @see CharSet#getInstance(java.lang.String...) for set-syntax. */ public static String squeeze(final String str, final String... set) { if (StringUtils.isEmpty(str) || deepEmpty(set)) { return str; } final CharSet chars = CharSet.getInstance(set); final StringBuilder buffer = new StringBuilder(str.length()); final char[] chrs = str.toCharArray(); final int sz = chrs.length; char lastChar = ' '; char ch = ' '; for (int i = 0; i < sz; i++) { ch = chrs[i]; // Compare with contains() last for performance. if (ch == lastChar && i != 0 && chars.contains(ch)) { continue; } buffer.append(ch); lastChar = ch; } return buffer.toString(); }
/** * Generate random valid characters. * * @return Generated data. */ public String random() { if (rand == null) throw new IllegalStateException("You need to set seed before using data objects"); int length = length(); char[] c = new char[length]; for (int i = 0; i < length; i++) { c[i] = chars.random(); } String next = new String(c); history.add(next); record(next); return next; }
@Override public void setSeed(long seed) { super.setSeed(seed); chars.setSeed(seed); }
public Text numbersOnly() { chars.numbersOnly(); return this; }
/** * Reduce the set of generated chars by given set of chars. * * @param charsToRemove The string of chars to be removed. * @return Self reference for chaining. */ public Text reduceBy(String charsToRemove) { chars.reduceBy(charsToRemove); return this; }
/** * Only a-z, A-Z, 0-9. * * @return Self reference for chaining. */ public Text asciiLettersAndNumbersOnly() { chars.asciiLettersAndNumbersOnly(); return this; }
/** * Set to only generate XML compliant characters. * * @return Self reference for chaining. */ public Text enableXml() { chars.enableXml(); return this; }