ConstraintSerialization { private <T> Predicate<?> and(Iterable<Predicate<? super T>> preds) { Iterator<Predicate<? super T>> pi = preds.iterator(); if (!pi.hasNext()) return Predicates.alwaysTrue(); Predicate<?> first = pi.next(); if (!pi.hasNext()) return first; return Predicates.and(preds); } private  ConstraintSerialization(); List<Object> toJsonList(ConfigKey<?> config); List<Object> toJsonList(Predicate<?> constraint); @SuppressWarnings({ "unchecked", "rawtypes" }) List<Object> toExactJsonList(Predicate<?> constraint); Predicate<?> toPredicateFromJson(Object o); List<Predicate<?>> toPredicateListFromJsonList(Collection<?> o); final static ConstraintSerialization INSTANCE; }
@Test public void testSimpleEquivalents() { Gson gson = new Gson(); assertPredicateFromJson(ConfigConstraints.required(), "required", "required()", "isNonBlank"); assertPredicateFromJson(StringPredicates.matchesRegex("a"), "regex(\"a\")", "matchesRegex(\"a\")"); assertPredicateFromJson(StringPredicates.matchesGlob("a"), "glob(\"a\")", "matchesGlob(\"a\")"); assertPredicateFromJson(Predicates.and(StringPredicates.matchesRegex("a"), StringPredicates.matchesRegex("b")), gson.fromJson("[{and: [{regex: a},{regex: b}]}]", List.class), gson.fromJson("[{all: [{regex: a},{regex: b}]}]", List.class)); assertPredicateFromJson(Predicates.or(StringPredicates.matchesRegex("a"), StringPredicates.matchesRegex("b")), gson.fromJson("[{or: [{regex: a},{regex: b}]}]", List.class), gson.fromJson("[{any: [{regex: a},{regex: b}]}]", List.class)); }
@Test public void testInteresting() { assertPredJsonBidi(Predicates.and(ConfigConstraints.required(), StringPredicates.matchesRegex(".*")), MutableList.of("required", MutableMap.of("regex", ".*"))); }
@Test public void testAnd() { Predicate<String> p = Predicates.<String>and( StringPredicates.matchesRegex("my.*first"), StringPredicates.matchesRegex("my.*second")); List<?> json = MutableList.of( MutableMap.of("regex", "my.*first"), MutableMap.of("regex", "my.*second")); assertPredJsonBidi(p, json); }