@Test public void testCanMergeTwoLists() throws Exception { List<String> inFields = new ArrayList<String>(); inFields.add("list1"); inFields.add("list2"); stage.inFields = inFields; stage.outField = "out"; stage.separator = ";"; List<String> list1 = new ArrayList<String>(); list1.add("a"); list1.add("b"); doc.putContentField("list1", list1); List<String> list2 = new ArrayList<String>(); list2.add("1"); list2.add("2"); doc.putContentField("list2", list2); stage.process(doc); String correctList = "[a;1, b;2]"; String resultList = doc.getContentField(stage.outField).toString(); assertTrue("Got: " + resultList + " expected " + correctList, resultList.equals(correctList)); }
@Override public void process(LocalDocument doc) { List<String> outList = new ArrayList<String>(); List<List<String>> inLists = new ArrayList<List<String>>(); int length = -1; for (String field : inFields) { Object fieldContent = doc.getContentField(field); if (fieldContent == null) { logger.info("Field " + field + " does not exist, skipping document"); return; } if (fieldContent instanceof List<?>) { @SuppressWarnings("unchecked") List<String> list = (List<String>)fieldContent; if (length == -1) length = list.size(); if (list.size() == length) { inLists.add(list); } else { logger.info("Lists in inFields are of different lengths, skipping document"); return; } } else { logger.info("Field " + field + " is not a list, skipping document"); return; } } for (int item = 0; item < length; item++) { StringBuilder strb = new StringBuilder(); for (int list = 0; list < inLists.size(); list++) { if (list > 0) strb.append(separator); strb.append(inLists.get(list).get(item)); } outList.add(strb.toString()); } doc.putContentField(outField, outList); }
MergeListsStage extends AbstractProcessStage { @Override public void process(LocalDocument doc) { List<String> outList = new ArrayList<String>(); List<List<String>> inLists = new ArrayList<List<String>>(); int length = -1; for (String field : inFields) { Object fieldContent = doc.getContentField(field); if (fieldContent == null) { logger.info("Field " + field + " does not exist, skipping document"); return; } if (fieldContent instanceof List<?>) { @SuppressWarnings("unchecked") List<String> list = (List<String>)fieldContent; if (length == -1) length = list.size(); if (list.size() == length) { inLists.add(list); } else { logger.info("Lists in inFields are of different lengths, skipping document"); return; } } else { logger.info("Field " + field + " is not a list, skipping document"); return; } } for (int item = 0; item < length; item++) { StringBuilder strb = new StringBuilder(); for (int list = 0; list < inLists.size(); list++) { if (list > 0) strb.append(separator); strb.append(inLists.get(list).get(item)); } outList.add(strb.toString()); } doc.putContentField(outField, outList); } }
MergeListsStage extends AbstractProcessStage { @Override public void process(LocalDocument doc) { List<String> outList = new ArrayList<String>(); List<List<String>> inLists = new ArrayList<List<String>>(); int length = -1; for (String field : inFields) { Object fieldContent = doc.getContentField(field); if (fieldContent == null) { logger.info("Field " + field + " does not exist, skipping document"); return; } if (fieldContent instanceof List<?>) { @SuppressWarnings("unchecked") List<String> list = (List<String>)fieldContent; if (length == -1) length = list.size(); if (list.size() == length) { inLists.add(list); } else { logger.info("Lists in inFields are of different lengths, skipping document"); return; } } else { logger.info("Field " + field + " is not a list, skipping document"); return; } } for (int item = 0; item < length; item++) { StringBuilder strb = new StringBuilder(); for (int list = 0; list < inLists.size(); list++) { if (list > 0) strb.append(separator); strb.append(inLists.get(list).get(item)); } outList.add(strb.toString()); } doc.putContentField(outField, outList); }  }
MergeListsStage extends AbstractProcessStage { @Override public void process(LocalDocument doc) { List<String> outList = new ArrayList<String>(); List<List<String>> inLists = new ArrayList<List<String>>(); int length = -1; for (String field : inFields) { Object fieldContent = doc.getContentField(field); if (fieldContent == null) { logger.info("Field " + field + " does not exist, skipping document"); return; } if (fieldContent instanceof List<?>) { @SuppressWarnings("unchecked") List<String> list = (List<String>)fieldContent; if (length == -1) length = list.size(); if (list.size() == length) { inLists.add(list); } else { logger.info("Lists in inFields are of different lengths, skipping document"); return; } } else { logger.info("Field " + field + " is not a list, skipping document"); return; } } for (int item = 0; item < length; item++) { StringBuilder strb = new StringBuilder(); for (int list = 0; list < inLists.size(); list++) { if (list > 0) strb.append(separator); strb.append(inLists.get(list).get(item)); } outList.add(strb.toString()); } doc.putContentField(outField, outList); }  @Override void process(LocalDocument doc); @Override void init(); }
MergeListsStage extends AbstractProcessStage { @Override public void process(LocalDocument doc) { List<String> outList = new ArrayList<String>(); List<List<String>> inLists = new ArrayList<List<String>>(); int length = -1; for (String field : inFields) { Object fieldContent = doc.getContentField(field); if (fieldContent == null) { logger.info("Field " + field + " does not exist, skipping document"); return; } if (fieldContent instanceof List<?>) { @SuppressWarnings("unchecked") List<String> list = (List<String>)fieldContent; if (length == -1) length = list.size(); if (list.size() == length) { inLists.add(list); } else { logger.info("Lists in inFields are of different lengths, skipping document"); return; } } else { logger.info("Field " + field + " is not a list, skipping document"); return; } } for (int item = 0; item < length; item++) { StringBuilder strb = new StringBuilder(); for (int list = 0; list < inLists.size(); list++) { if (list > 0) strb.append(separator); strb.append(inLists.get(list).get(item)); } outList.add(strb.toString()); } doc.putContentField(outField, outList); }  @Override void process(LocalDocument doc); @Override void init();  }