@Test public void shouldParseJsonInputToLineItems() throws Exception { List<OrderLineItem> orderLineItems = InputParser.parse("[" + " 'ITEM000001'," + " 'ITEM000001'," + " 'ITEM000001'," + " 'ITEM000001'," + " 'ITEM000001'," + " 'ITEM000002-2'," + " 'ITEM000003'," + " 'ITEM000003'," + " 'ITEM000003'" + "]"); assertThat(orderLineItems.size(), is(3)); assertThat(orderLineItems.get(0).amount(), is(5)); assertThat(orderLineItems.get(1).amount(), is(2)); assertThat(orderLineItems.get(2).amount(), is(3)); }
public static List<OrderLineItem> parse(String json) { String cleanItems = json.replace("[", "").replace("]", "").replaceAll(" ", "").replaceAll("'", ""); String[] jsonLines = cleanItems.split(","); Map<String, List<String>> jsonLineGroups = Arrays.asList(jsonLines).stream() .collect(groupingBy(item -> item)); return jsonLineGroups.keySet().stream() .map(jsonLine -> jsonLineToOrderLineItem(jsonLine, jsonLineGroups)) .sorted((x, y) -> x.product().code().compareTo(y.product().code())) .collect(toList()); }
InputParser { public static List<OrderLineItem> parse(String json) { String cleanItems = json.replace("[", "").replace("]", "").replaceAll(" ", "").replaceAll("'", ""); String[] jsonLines = cleanItems.split(","); Map<String, List<String>> jsonLineGroups = Arrays.asList(jsonLines).stream() .collect(groupingBy(item -> item)); return jsonLineGroups.keySet().stream() .map(jsonLine -> jsonLineToOrderLineItem(jsonLine, jsonLineGroups)) .sorted((x, y) -> x.product().code().compareTo(y.product().code())) .collect(toList()); } }
InputParser { public static List<OrderLineItem> parse(String json) { String cleanItems = json.replace("[", "").replace("]", "").replaceAll(" ", "").replaceAll("'", ""); String[] jsonLines = cleanItems.split(","); Map<String, List<String>> jsonLineGroups = Arrays.asList(jsonLines).stream() .collect(groupingBy(item -> item)); return jsonLineGroups.keySet().stream() .map(jsonLine -> jsonLineToOrderLineItem(jsonLine, jsonLineGroups)) .sorted((x, y) -> x.product().code().compareTo(y.product().code())) .collect(toList()); }  }
InputParser { public static List<OrderLineItem> parse(String json) { String cleanItems = json.replace("[", "").replace("]", "").replaceAll(" ", "").replaceAll("'", ""); String[] jsonLines = cleanItems.split(","); Map<String, List<String>> jsonLineGroups = Arrays.asList(jsonLines).stream() .collect(groupingBy(item -> item)); return jsonLineGroups.keySet().stream() .map(jsonLine -> jsonLineToOrderLineItem(jsonLine, jsonLineGroups)) .sorted((x, y) -> x.product().code().compareTo(y.product().code())) .collect(toList()); }  static List<OrderLineItem> parse(String json); }
InputParser { public static List<OrderLineItem> parse(String json) { String cleanItems = json.replace("[", "").replace("]", "").replaceAll(" ", "").replaceAll("'", ""); String[] jsonLines = cleanItems.split(","); Map<String, List<String>> jsonLineGroups = Arrays.asList(jsonLines).stream() .collect(groupingBy(item -> item)); return jsonLineGroups.keySet().stream() .map(jsonLine -> jsonLineToOrderLineItem(jsonLine, jsonLineGroups)) .sorted((x, y) -> x.product().code().compareTo(y.product().code())) .collect(toList()); }  static List<OrderLineItem> parse(String json);  }