Converter { Node convertToNode(Object source, String label, boolean lenient) { for (int i = 0; i < factories.size(); i++) { NodeFactory factory = factories.get(i); if (isLastFactory(i) || factory.isPreferredFor(source)) { return factory.convertToNode(source, label, lenient); } } throw new IllegalStateException("Should not happen"); } Converter(List<NodeFactory> factories);   }
@Test void shouldUseTheLastFactoryForNonPreferred() { Converter converter = new Converter(Arrays.asList(new GsonNodeFactory(), new Jackson2NodeFactory())); Node node = converter.convertToNode(JSON, "", false); assertEquals(Jackson2NodeFactory.Jackson2Node.class, node.getClass()); }
@Test void shouldUsePreferredFactory() { Converter converter = new Converter(Arrays.asList(new Jackson2NodeFactory(), new GsonNodeFactory())); Node node = converter.convertToNode(BooleanNode.TRUE, "", false); assertEquals(Jackson2NodeFactory.Jackson2Node.class, node.getClass()); }