@Test public void firstMapperGetToMap() { InteractTypeMapper mapper1 = Mockito.mock(InteractTypeMapper.class); when(mapper1.getInteractType("eats")).thenReturn(InteractType.ATE); InteractTypeMapper mapper2 = Mockito.mock(InteractTypeMapper.class); when(mapper2.getInteractType("eats")).thenThrow(new RuntimeException("kaboom!")); InteractTypeMapper interactTypeMapperWithFallback = new InteractTypeMapperWithFallbackImpl(mapper1, mapper2); assertThat(interactTypeMapperWithFallback.getInteractType("eats"), is(InteractType.ATE)); }
@Override public InteractType getInteractType(String interactionTypeNameOrId) { final Optional<InteractTypeMapper> lastMapper = mappers .stream() .filter(x -> x.shouldIgnoreInteractionType(interactionTypeNameOrId)) .findFirst(); int lastIndex = mappers.size() - 1; if (lastMapper.isPresent()) { lastIndex = mappers.indexOf(lastMapper.get()); } Optional<InteractType> first = mappers.subList(0, lastIndex + 1) .stream() .map(x -> x.getInteractType(interactionTypeNameOrId)) .filter(Objects::nonNull) .findFirst(); return first.orElse(null); }
InteractTypeMapperWithFallbackImpl implements InteractTypeMapper { @Override public InteractType getInteractType(String interactionTypeNameOrId) { final Optional<InteractTypeMapper> lastMapper = mappers .stream() .filter(x -> x.shouldIgnoreInteractionType(interactionTypeNameOrId)) .findFirst(); int lastIndex = mappers.size() - 1; if (lastMapper.isPresent()) { lastIndex = mappers.indexOf(lastMapper.get()); } Optional<InteractType> first = mappers.subList(0, lastIndex + 1) .stream() .map(x -> x.getInteractType(interactionTypeNameOrId)) .filter(Objects::nonNull) .findFirst(); return first.orElse(null); } }
InteractTypeMapperWithFallbackImpl implements InteractTypeMapper { @Override public InteractType getInteractType(String interactionTypeNameOrId) { final Optional<InteractTypeMapper> lastMapper = mappers .stream() .filter(x -> x.shouldIgnoreInteractionType(interactionTypeNameOrId)) .findFirst(); int lastIndex = mappers.size() - 1; if (lastMapper.isPresent()) { lastIndex = mappers.indexOf(lastMapper.get()); } Optional<InteractType> first = mappers.subList(0, lastIndex + 1) .stream() .map(x -> x.getInteractType(interactionTypeNameOrId)) .filter(Objects::nonNull) .findFirst(); return first.orElse(null); } InteractTypeMapperWithFallbackImpl(InteractTypeMapper... mappers); }
InteractTypeMapperWithFallbackImpl implements InteractTypeMapper { @Override public InteractType getInteractType(String interactionTypeNameOrId) { final Optional<InteractTypeMapper> lastMapper = mappers .stream() .filter(x -> x.shouldIgnoreInteractionType(interactionTypeNameOrId)) .findFirst(); int lastIndex = mappers.size() - 1; if (lastMapper.isPresent()) { lastIndex = mappers.indexOf(lastMapper.get()); } Optional<InteractType> first = mappers.subList(0, lastIndex + 1) .stream() .map(x -> x.getInteractType(interactionTypeNameOrId)) .filter(Objects::nonNull) .findFirst(); return first.orElse(null); } InteractTypeMapperWithFallbackImpl(InteractTypeMapper... mappers); @Override boolean shouldIgnoreInteractionType(String interactionTypeNameOrId); @Override InteractType getInteractType(String interactionTypeNameOrId); }
InteractTypeMapperWithFallbackImpl implements InteractTypeMapper { @Override public InteractType getInteractType(String interactionTypeNameOrId) { final Optional<InteractTypeMapper> lastMapper = mappers .stream() .filter(x -> x.shouldIgnoreInteractionType(interactionTypeNameOrId)) .findFirst(); int lastIndex = mappers.size() - 1; if (lastMapper.isPresent()) { lastIndex = mappers.indexOf(lastMapper.get()); } Optional<InteractType> first = mappers.subList(0, lastIndex + 1) .stream() .map(x -> x.getInteractType(interactionTypeNameOrId)) .filter(Objects::nonNull) .findFirst(); return first.orElse(null); } InteractTypeMapperWithFallbackImpl(InteractTypeMapper... mappers); @Override boolean shouldIgnoreInteractionType(String interactionTypeNameOrId); @Override InteractType getInteractType(String interactionTypeNameOrId);  }