public List<FormBean> sortByDependency() { final ArrayList<FormBean> formBeansClone = new ArrayList<FormBean>(formBeans); List<FormBean> sortedFormBeans = new ArrayList<FormBean>(); for (FormBean formBean : formBeans) { if (CollectionUtils.isEmpty(formBean.getDepends())) { sortedFormBeans.add(formBean); formBeansClone.remove(formBean); } } final List<String> namesOfForms = collect(formBeans, on(FormBean.class).getFormname()); resolveDependency(sortedFormBeans, formBeansClone, namesOfForms); return sortedFormBeans; }
/** * For each item in the given iterable collects the value defined by the given argument and put them in a List. * For example the following code: * <pre> * List<Person> myFriends = asList(new Person("Biagio", 39), new Person("Luca", 29), new Person("Celestino", 29)); * List<Integer> ages = collect(meAndMyFriends, on(Person.class).getAge()); * </pre> * extracts the ages of all the Persons in the list and put them in a List of Integer. * <p/> * Actually it handles also Maps, Arrays and Iterator by collecting their values. * Note that this method accepts an Object in order to be used in conjunction with the {@link Lambda#forEach(Iterable)}. * @param iterable The iterable of which the items should be collected * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return A List containing all the items collected from the give iterable * @throws RuntimeException if the iterable is not an Iterable or a Map */ public static <T> List<T> collect(Object iterable, T argument) { return (List<T>)collect(convert(iterable, new ArgumentConverter<Object, T>(argument))); }
private void resolveDependency(List<FormBean> sortedFormBeans, List<FormBean> unprocessedFormBeans, List<String> namesOfForms) { if (!unprocessedFormBeans.isEmpty()) { int processedNodes = 0; for (FormBean unprocessedFormBean : new ArrayList<FormBean>(unprocessedFormBeans)) { boolean independent = true; for (String dependent : unprocessedFormBean.getDepends()) { if (namesOfForms.contains(dependent)) { List<String> processedFormNames = collect(sortedFormBeans, on(FormBean.class).getFormname()); if (!processedFormNames.contains(dependent)) { independent = false; break; } } } if (independent) { sortedFormBeans.add(unprocessedFormBean); unprocessedFormBeans.remove(unprocessedFormBean); processedNodes++; } } if (processedNodes > 0) { resolveDependency(sortedFormBeans, unprocessedFormBeans, namesOfForms); } else { throw new MotechException("Detected cyclic mobile form dependencies"); } } }
private boolean canBeDelivered(MessageGroup group) { final Collection<Message<?>> messages = group.getMessages(); if (messages.size() <= 0) { return false; } List<T> eventsInPayload = convert(collect(messages, on(Message.class).getPayload()), new Converter<Object, T>() { @Override public T convert(Object from) { return (T) from; } }); boolean canBeDispatched = aggregationHandler.canBeDispatched(eventsInPayload); logger.debug("Events: " + eventsInPayload + ". Can be dispatched? " + canBeDispatched); return canBeDispatched; } }
List<FormBean> allForms = flatten(collect(studies, on(Study.class).forms())); for (Study study : studies) { for (FormBeanGroup group : study.groupedForms()) {
private void handleDownloadUsersAndForms(ByteArrayOutputStream byteStream, DataInputStream dataInput) throws IOException, SerializerException { try { serializer.serializeUsers(byteStream, usersService.getUsers()); } catch (Exception e) { throw new SerializerException(e); } int studyIndex = dataInput.readInt(); FormGroup groupNameAndForms = mobileFormsService.getForms(studyIndex); List<String> formsXmlContent = collect(groupNameAndForms.getForms(), on(Form.class).content()); try { serializer.serializeForms(byteStream, formsXmlContent, studyIndex, groupNameAndForms.getName()); } catch (Exception e) { throw new SerializerException(e); } }