package com.plannaplan.responses.mappers; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Objects; import java.util.function.Function; import java.util.stream.Collectors; import com.plannaplan.entities.Groups; import com.plannaplan.responses.models.GroupDefaultResponse; import com.plannaplan.responses.models.CourseWithGroupsResponse; import com.plannaplan.responses.models.GroupWithCapacityResponse; import com.plannaplan.types.GroupType; /** * Mappers for Groups to api responses */ public class GroupsMappers { /** * @param groups list of groups to be mapped * @param taken ammoints to be take into account * @return list of api responses */ public static List mapToDefaultResponse(List groups, HashMap taken) { return groups.stream().filter(Objects::nonNull).map(new Function() { @Override public GroupDefaultResponse apply(Groups p) { if (taken != null) { return new GroupDefaultResponse(p, taken.get(p.getId())); } else { return new GroupDefaultResponse(p); } } }).collect(Collectors.toList()); } /** * * @param groups list of groups to be mapped * @return ammoints to be take into account */ public static List mapToDefaultResponse(List groups) { return GroupsMappers.mapToDefaultResponse(groups, null); } /** * @param groups list of groups to be mapped * @param taken ammoints to be take into account * @return list of api responses */ public static List mapToCapacityResponse(List groups, HashMap taken) { return groups.stream().filter(Objects::nonNull).map(new Function() { @Override public GroupWithCapacityResponse apply(Groups p) { if (taken != null) { return new GroupWithCapacityResponse(p, taken.get(p.getId())); } else { return new GroupWithCapacityResponse(p); } } }).collect(Collectors.toList()); } /** * @param groups list of groups to be mapped * @return ammoints to be take into account */ public static List mapToCapacityResponse(List groups) { return GroupsMappers.mapToCapacityResponse(groups, null); } /** * @param groups list of groups to be mapped * @param taken ammoints to be take into account * @return list of api responses */ public static CourseWithGroupsResponse mapToGetCourseGroupsDefaultResponse( List groups, HashMap taken) { List lectures = new ArrayList<>(); List classes = new ArrayList<>(); groups.stream().forEach(group -> { if (group.getType() == GroupType.CLASS) { if (taken != null) { classes.add(new GroupDefaultResponse(group, taken.get(group.getId()))); } else { classes.add(new GroupDefaultResponse(group)); } } else { if (taken != null) { lectures.add(new GroupDefaultResponse(group, taken.get(group.getId()))); } else { lectures.add(new GroupDefaultResponse(group)); } } }); return new CourseWithGroupsResponse<>(classes, lectures); } /** * @param groups list of groups to be mapped * @return ammoints to be take into account */ public static CourseWithGroupsResponse mapToGetCourseGroupsDefaultResponse( List groups) { return GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups, null); } /** * @param groups list of groups to be mapped * @param taken ammoints to be take into account * @return list of api responses */ public static CourseWithGroupsResponse mapToGetCourseGroupsWithCapacityResponse( List groups, HashMap taken) { List lectures = new ArrayList<>(); List classes = new ArrayList<>(); groups.stream().forEach(group -> { if (group.getType() == GroupType.CLASS) { if (taken != null) { classes.add(new GroupWithCapacityResponse(group, taken.get(group.getId()))); } else { classes.add(new GroupWithCapacityResponse(group)); } } else { if (taken != null) { lectures.add(new GroupWithCapacityResponse(group, taken.get(group.getId()))); } else { lectures.add(new GroupWithCapacityResponse(group)); } } }); return new CourseWithGroupsResponse<>(classes, lectures); } /** * @param groups list of groups to be mapped * @return ammoints to be take into account */ public static CourseWithGroupsResponse mapToGetCourseGroupsWithCapacityResponse( List groups) { return GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups, null); } }