diff --git a/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java b/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java index 0bc32e6..0c427de 100644 --- a/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java +++ b/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java @@ -3,21 +3,26 @@ import org.ntlab.tampoposerver.models.Notification; import org.springframework.stereotype.Repository; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; +import java.util.*; @Repository public class NotificationRepository { private HashMap> notificationMap = new HashMap<>(); //通知の追加 - public Notification addNotification(String userId, String notificationId, String from, String text, String time) { + public Notification addNotification(String userId, String from, String text, String time) { + String notificationId; + Map userNotifications = notificationMap.get(userId); + if (userNotifications == null) { + notificationId = Integer.toString(0); + } + else{ + notificationId = Integer.toString(userNotifications.size()); + } + Notification n = new Notification(userId, notificationId, from, text, time); - if(notificationMap.get(userId) == null) { + if(userNotifications == null) { notificationMap.put(userId, new HashMap<>()); } notificationMap.get(userId).put(notificationId, n); @@ -40,8 +45,36 @@ return new ArrayList<>(); } - return userNotifications.values().stream() - .sorted((a, b) -> b.getTime().compareTo(a.getTime())) - .collect(Collectors.toList()); + List sortedNotifications = new ArrayList<>(userNotifications.values()); + + Collections.sort(sortedNotifications, new Comparator() { + @Override + public int compare(Notification o1, Notification o2) { + if(o1.getTime().compareTo(o2.getTime()) < 0) { + return 1; + } + else if(o1.getTime().compareTo(o2.getTime()) > 0) { + return -1; + } + return 0; + } + }); + +// for(int i = 0; i < sortedNotifications.size() - 1; i++) { +// for(int j = i + 1; j < sortedNotifications.size(); j++) { +// if(sortedNotifications.get(i).getTime().compareTo(sortedNotifications.get(j).getTime()) < 0) { +// Notification tmp = sortedNotifications.get(i); +// sortedNotifications.set(i, sortedNotifications.get(j)); +// sortedNotifications.set(j, tmp); +// } +// } +// } + + return sortedNotifications; + +// ラムダ式 +// return userNotifications.values().stream() +// .sorted((a, b) -> b.getTime().compareTo(a.getTime())) +// .collect(Collectors.toList()); } }