@Test public void fromMap() throws Exception { verifyPredicate(PredicateUtils.fromMap(createMap())); }
public static Predicate fromMap(Map<?, ?> map) { Predicate predicate = null; if ((map != null) && !map.isEmpty()) { if (map.size() == 1) { Map.Entry<?, ?> entry = map.entrySet().iterator().next(); String name = Objects.toString(entry.getKey()); Class<? extends Predicate> predicateClass = PredicateClassFactory.getPredicateClass(name); if (predicateClass == null) { throw new IllegalArgumentException(String.format("Unexpected predicate name - %s", name)); } else { try { Method method = predicateClass.getMethod("fromMap", Map.class); if (method == null) { throw new UnsupportedOperationException(String.format("Cannot translate data to a %s - %s", predicateClass.getName(), "Failed to find toMap method")); } else { predicate = (Predicate) method.invoke(null, Collections.singletonMap(name, entry.getValue())); } } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new UnsupportedOperationException(String.format("Cannot translate data to a %s - %s", predicateClass.getName(), e.getLocalizedMessage()), e); } } } else { throw new IllegalArgumentException(String.format("Too many map entries have been encountered - %d", map.size())); } } return predicate; }
PredicateUtils { public static Predicate fromMap(Map<?, ?> map) { Predicate predicate = null; if ((map != null) && !map.isEmpty()) { if (map.size() == 1) { Map.Entry<?, ?> entry = map.entrySet().iterator().next(); String name = Objects.toString(entry.getKey()); Class<? extends Predicate> predicateClass = PredicateClassFactory.getPredicateClass(name); if (predicateClass == null) { throw new IllegalArgumentException(String.format("Unexpected predicate name - %s", name)); } else { try { Method method = predicateClass.getMethod("fromMap", Map.class); if (method == null) { throw new UnsupportedOperationException(String.format("Cannot translate data to a %s - %s", predicateClass.getName(), "Failed to find toMap method")); } else { predicate = (Predicate) method.invoke(null, Collections.singletonMap(name, entry.getValue())); } } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new UnsupportedOperationException(String.format("Cannot translate data to a %s - %s", predicateClass.getName(), e.getLocalizedMessage()), e); } } } else { throw new IllegalArgumentException(String.format("Too many map entries have been encountered - %d", map.size())); } } return predicate; } }
PredicateUtils { public static Predicate fromMap(Map<?, ?> map) { Predicate predicate = null; if ((map != null) && !map.isEmpty()) { if (map.size() == 1) { Map.Entry<?, ?> entry = map.entrySet().iterator().next(); String name = Objects.toString(entry.getKey()); Class<? extends Predicate> predicateClass = PredicateClassFactory.getPredicateClass(name); if (predicateClass == null) { throw new IllegalArgumentException(String.format("Unexpected predicate name - %s", name)); } else { try { Method method = predicateClass.getMethod("fromMap", Map.class); if (method == null) { throw new UnsupportedOperationException(String.format("Cannot translate data to a %s - %s", predicateClass.getName(), "Failed to find toMap method")); } else { predicate = (Predicate) method.invoke(null, Collections.singletonMap(name, entry.getValue())); } } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new UnsupportedOperationException(String.format("Cannot translate data to a %s - %s", predicateClass.getName(), e.getLocalizedMessage()), e); } } } else { throw new IllegalArgumentException(String.format("Too many map entries have been encountered - %d", map.size())); } } return predicate; }  }
PredicateUtils { public static Predicate fromMap(Map<?, ?> map) { Predicate predicate = null; if ((map != null) && !map.isEmpty()) { if (map.size() == 1) { Map.Entry<?, ?> entry = map.entrySet().iterator().next(); String name = Objects.toString(entry.getKey()); Class<? extends Predicate> predicateClass = PredicateClassFactory.getPredicateClass(name); if (predicateClass == null) { throw new IllegalArgumentException(String.format("Unexpected predicate name - %s", name)); } else { try { Method method = predicateClass.getMethod("fromMap", Map.class); if (method == null) { throw new UnsupportedOperationException(String.format("Cannot translate data to a %s - %s", predicateClass.getName(), "Failed to find toMap method")); } else { predicate = (Predicate) method.invoke(null, Collections.singletonMap(name, entry.getValue())); } } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new UnsupportedOperationException(String.format("Cannot translate data to a %s - %s", predicateClass.getName(), e.getLocalizedMessage()), e); } } } else { throw new IllegalArgumentException(String.format("Too many map entries have been encountered - %d", map.size())); } } return predicate; }  static Map<String, Object> toMap(Predicate predicate); static Predicate fromMap(Map<?, ?> map); static String toJSON(Predicate predicate); static Predicate fromJSON(String json); }
PredicateUtils { public static Predicate fromMap(Map<?, ?> map) { Predicate predicate = null; if ((map != null) && !map.isEmpty()) { if (map.size() == 1) { Map.Entry<?, ?> entry = map.entrySet().iterator().next(); String name = Objects.toString(entry.getKey()); Class<? extends Predicate> predicateClass = PredicateClassFactory.getPredicateClass(name); if (predicateClass == null) { throw new IllegalArgumentException(String.format("Unexpected predicate name - %s", name)); } else { try { Method method = predicateClass.getMethod("fromMap", Map.class); if (method == null) { throw new UnsupportedOperationException(String.format("Cannot translate data to a %s - %s", predicateClass.getName(), "Failed to find toMap method")); } else { predicate = (Predicate) method.invoke(null, Collections.singletonMap(name, entry.getValue())); } } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new UnsupportedOperationException(String.format("Cannot translate data to a %s - %s", predicateClass.getName(), e.getLocalizedMessage()), e); } } } else { throw new IllegalArgumentException(String.format("Too many map entries have been encountered - %d", map.size())); } } return predicate; }  static Map<String, Object> toMap(Predicate predicate); static Predicate fromMap(Map<?, ?> map); static String toJSON(Predicate predicate); static Predicate fromJSON(String json);  }