Constraints implements Iterable<Constraint> { public static Constraints parse(String input) { if (emptyToNull(input) != null) { Operator operator; if (input.contains(AND.operator)) { operator = AND; } else if (input.contains(OR.operator)) { operator = OR; } else { operator = AND; } Iterable<String> values = Splitter.on(operator.operator) .omitEmptyStrings() .trimResults() .split(input); LinkedHashSet<Constraint> constraints = new LinkedHashSet<>(); for (String value : values) { try { constraints.add(Constraint.parse(value)); } catch (IllegalArgumentException ignored) { } } return new Constraints(constraints, operator); } else { return empty(); } } private  Constraints(LinkedHashSet<Constraint> constraints, Operator operator); static Constraints single(Constraint constraint); static Constraints and(Constraint first, Constraint... rest); static Constraints and(Iterable<Constraint> constraints); static Constraints or(Constraint first, Constraint... rest); static Constraints or(Iterable<Constraint> constraints); static Constraints empty(); static Constraints parse(String input); @Override String toString(); String data(); @Override Iterator<Constraint> iterator(); int size(); boolean isEmpty(); boolean contains(Object o); Set<Constraint> getConstraints(); Operator getOperator();  }
@Test public void parseSingle() { Constraints single = Constraints.parse(ENGAGE_DATA); assertSingle(single); }
@Test public void parseAnd() { Constraints and = Constraints.parse(AND_DATA); assertAnd(and); }
@Test public void parseOr() { Constraints or = Constraints.parse(OR_DATA); assertOr(or); }
@Test public void parseNull() { Constraints constraints = Constraints.parse(null); assertEmpty(constraints); }
@Test public void parseEmpty() { Constraints constraints = Constraints.parse(""); assertEmpty(constraints); }
@Test public void parseBlank() { Constraints constraints = Constraints.parse(" "); assertEmpty(constraints); }
@Test public void parseIllegal() { Constraints constraints = Constraints.parse("no constraints"); assertEmpty(constraints); }