DelegatingMarshalContext implements MarshalContext { public static Marshaler<?> findMarshaler(Object object, Collection<? extends Marshaler<?>> marshalers) throws MarshalingException { Objects.requireNonNull(object); Objects.requireNonNull(marshalers); return marshalers.stream() .filter(marshaler -> marshaler.canHandle(object)) .findFirst() .orElseThrow( () -> new MarshalingException(String.format( "None of %s is capable of marshaling instance: %s.", marshalers, object )) ); } private  DelegatingMarshalContext(Key contextKey, MarshalTarget marshalTarget, Marshaler<?> marshaler,
            ImmutableList<Marshaler<?>> marshalers, @Nullable DelegatingMarshalContext ancestor); static DelegatingMarshalContext create(Marshaler<?> currentMarshaler,
            Collection<? extends Marshaler<?>> marshalers, MarshalTarget marshalTarget); static void marshal(Object object, Collection<? extends Marshaler<?>> marshalers,
            MarshalTarget marshalTarget); static void processMarshalingTree(ObjectNode tree, Collection<? extends Marshaler<?>> marshalers,
            MarshalTarget marshalTarget); static Marshaler<?> findMarshaler(Object object, Collection<? extends Marshaler<?>> marshalers); @Override void close(); @Override String toString(); @Override OutputStream newOutputStream(Key key); @Override void putByteSequence(ByteSequence byteSequence, Key key); @Override void writeObject(Object object, Key key); void acceptObject(Object object); void acceptTree(ObjectNode tree);  }
@Test public void findMarshalerFound() throws MarshalingException { Assert.assertSame( DelegatingMarshalContext.findMarshaler( 4, Arrays.asList(STRING_MARSHALER, INTEGER_MARSHALER) ), INTEGER_MARSHALER ); Assert.assertSame( DelegatingMarshalContext.findMarshaler( 4, Arrays.asList(INTEGER_MARSHALER, STRING_MARSHALER) ), INTEGER_MARSHALER ); }
@Test public void findMarshalerNotFound() { try { DelegatingMarshalContext.findMarshaler( 4.0, Arrays.asList(INTEGER_MARSHALER, STRING_MARSHALER) ); Assert.fail(); } catch (MarshalingException exception) { Assert.assertTrue(exception.getMessage().startsWith("None of")); Assert.assertTrue(exception.getMessage().contains("is capable of marshaling instance:")); } }
@Test public void findMarshalerBadArguments() throws MarshalingException { try { DelegatingMarshalContext.findMarshaler( null, Arrays.asList(INTEGER_MARSHALER, STRING_MARSHALER) ); Assert.fail(); } catch (NullPointerException ignored) { } try { DelegatingMarshalContext.findMarshaler(4.0, null); Assert.fail(); } catch (NullPointerException ignored) { } }