Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save michaldabrowski/5bf040e5cdf9573f5c74dc5165770349 to your computer and use it in GitHub Desktop.
Save michaldabrowski/5bf040e5cdf9573f5c74dc5165770349 to your computer and use it in GitHub Desktop.

2017.07.20 - Small interfaces and 'smarts'

We have an interface:

ObjectMapper.java

public interface ObjectMapper {
    <Input, Output> Output map(Input input, Class<Output> outputClass);
}

Sometimes we can't map input so we want to have a possibility to return the type of Optional<Output>.

"Bad":

Add new method to interface ObjectMapper:

    <Input, Output> Optional<Output> mapOptional(Input input, Class<Output> outputClass);

Now we have to implement it in every class that implements ObjectMapper, e.g.:

class DozerObjectMapper implements ObjectMapper {
    /** 'map' method implementation and some other stuff **/
    @Override
    public <Input, Output> Optional<Output> mapOptional(Input input, Class<Output> outputClass) {
        if (input == null) {
            return Optional.empty();
        }
        return Optional.of(map(input, outputClass));
    }
}

"Bad", it depends :-)

Good:

ObjectMapper.java with OptionalObjectMapper class called smart

public interface ObjectMapper {
    <Input, Output> Output map(Input input, Class<Output> outputClass);

    final class OptionalObjectMapper implements ObjectMapper {

        private final ObjectMapper mapper;

        public OptionalObjectMapper(ObjectMapper mapper) {
            this.mapper = mapper;
        }

        @Override
        public <Input, Output> Output map(Input input, Class<Output> outputClass) {
            return mapper.map(input, outputClass);
        }

        public <Input, Output> Optional<Output> mapOptional(Input input, Class<Output> outputClass) {
            if (input == null) {
                return Optional.empty();
            }
            return Optional.of(map(input, outputClass));
        }
    }
}

DozerObjectMapper class has not changed.

Usage:

ObjectMapper objectMapper = dozerObjectMapperFactory.mapper();
Optional<Output> output = new ObjectMapper.OptionalObjectMapper(objectMapper).mapOptional(input);

Topics

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment