-
Notifications
You must be signed in to change notification settings - Fork 21
Add implementation for UserServiceImpl #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,68 +2,99 @@ | |
|
|
||
| import com.endava.internship.domain.Privilege; | ||
| import com.endava.internship.domain.User; | ||
| import com.endava.internship.service.UserService; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
| import java.util.function.Predicate; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class UserServiceImpl implements UserService { | ||
|
|
||
| @Override | ||
| public List<String> getFirstNamesReverseSorted(List<User> users) { | ||
| throw new UnsupportedOperationException("Not implemented"); | ||
| return users.stream(). | ||
| map(User::getFirstName). | ||
| sorted(Comparator.reverseOrder()). | ||
| collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Override | ||
| public List<User> sortByAgeDescAndNameAsc(final List<User> users) { | ||
| throw new UnsupportedOperationException("Not implemented"); | ||
| return users.stream(). | ||
| sorted(Comparator. | ||
| comparing(User::getAge, Comparator.reverseOrder()). | ||
| thenComparing(User::getFirstName) | ||
| ). | ||
| collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Privilege> getAllDistinctPrivileges(final List<User> users) { | ||
| throw new UnsupportedOperationException("Not implemented"); | ||
| return users.stream(). | ||
| map(User::getPrivileges). | ||
| flatMap(Collection::stream). | ||
| distinct(). | ||
| collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<User> getUpdateUserWithAgeHigherThan(final List<User> users, final int age) { | ||
| throw new UnsupportedOperationException("Not implemented"); | ||
| return users.stream(). | ||
| filter(user -> user.getAge() > age). | ||
| findFirst(); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<Integer, List<User>> groupByCountOfPrivileges(final List<User> users) { | ||
| throw new UnsupportedOperationException("Not implemented"); | ||
| return users.stream().collect(Collectors.groupingBy(user -> user.getPrivileges().size())); | ||
| } | ||
|
|
||
| @Override | ||
| public double getAverageAgeForUsers(final List<User> users) { | ||
| throw new UnsupportedOperationException("Not implemented"); | ||
| return users.stream().mapToDouble(User::getAge).average().orElse(-1); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<String> getMostFrequentLastName(final List<User> users) { | ||
| throw new UnsupportedOperationException("Not implemented"); | ||
| Map<String, Long> map = users.stream().map(User::getLastName). | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we have a list containing one user? should it return Optional.empty?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a logical error in condition |
||
| collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); | ||
| Long max = map.values().stream().max(Long::compareTo).orElse(0L); | ||
| return map.values().stream().filter(val -> val.equals(max)).count() != 1 ? Optional.empty() : | ||
| map.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey); | ||
| } | ||
|
|
||
| @Override | ||
| public List<User> filterBy(final List<User> users, final Predicate<User>... predicates) { | ||
| throw new UnsupportedOperationException("Not implemented"); | ||
| return users.stream(). | ||
| filter( | ||
| Arrays.stream(predicates).reduce(Predicate::and).orElse(x -> true) | ||
| ). | ||
| collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Override | ||
| public String convertTo(final List<User> users, final String delimiter, final Function<User, String> mapFun) { | ||
| throw new UnsupportedOperationException("Not implemented"); | ||
| return users.stream().map(mapFun).collect(Collectors.joining(delimiter)); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<Privilege, List<User>> groupByPrivileges(List<User> users) { | ||
| throw new UnsupportedOperationException("Not implemented"); | ||
| return users.stream().map(User::getPrivileges).flatMap(Collection::stream). | ||
| distinct(). | ||
| collect(Collectors.toMap( | ||
| privilege -> privilege, | ||
| privilege -> users.stream().filter(user -> user.getPrivileges().contains(privilege)).collect(Collectors.toList()) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Long> getNumberOfLastNames(final List<User> users) { | ||
| throw new UnsupportedOperationException("Not implemented"); | ||
| return users.stream().collect(Collectors.groupingBy(User::getLastName, Collectors.counting())); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a reversed() method in Comparator that could be used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, it's a great idea