class DefaultContextualPreferenceReducer implements ContextualPreferenceReducer {

    @Override
    public Optional<ContextualPreference> reduce(final List<ContextualPreference> preferences) {
        if (preferences.isEmpty()) {
            return Optional.empty();
        }
        final ContextualPreference validPreference = preferences.get(0);
        if (preferences.size() == 1) {
            return Optional.of(validPreference);
        }
        final List<ContextualPreference> invalidPreferences = preferences.stream()
                .filter(preference -> !preference.getName().equals(validPreference.getName())
                        || preference.getResource().getLevel() != validPreference.getResource().getLevel()
                        || preference.getType() != validPreference.getType())
                .collect(Collectors.toList());
        return invalidPreferences.isEmpty()
                ? reduceValidPreferences(preferences)
                : reduceInvalidPreferences(validPreference, invalidPreferences);
    }

}