private List<ChatMessage> sanitizeMap(final SortedMap<Date, ChatMessage> aMap) { if (aMap.isEmpty() || aMap.size() == 1) return Lists.newArrayList(aMap.values()); final LinkedList<ChatMessage> ret = Lists.newLinkedList(aMap.values()); final ListIterator<ChatMessage> i = ret.listIterator(); ChatMessage prevMsg = i.next(); do { ChatMessage msg = i.next(); if (!msg.getPreviousMessageDate().equals(prevMsg.getDate())) { if (msg.getPreviousMessageDate().before(prevMsg.getDate())) { msg.setPreviousMessageDate(prevMsg.getDate()); } else { final ChatMessage tmp = createLostMessageBetween( msg.getRoom(), prevMsg.getDate(), msg.getPreviousMessageDate()); aMap.put(tmp.getDate(), tmp); i.previous(); i.add(tmp); i.next(); msg = tmp; } } prevMsg = msg; } while (i.hasNext()); return ret; }
public List<ChatMessage> getAllRoomMessagesAfter(final ChatMessage aLastMsg) { final SortedMap<Date, ChatMessage> map = getMapForRoom(aLastMsg.getRoom()); synchronized (map) { final List<ChatMessage> ret = sanitizeMap(map.tailMap(aLastMsg.getDate())); if (ret.get(0).getDate().equals(aLastMsg.getDate())) { ret.remove(0); } return ret; } }
public void recordAndUpdateNewMessage(final ChatMessage aMsg) { final SortedMap<Date, ChatMessage> map = getMapForRoom(aMsg.getRoom()); synchronized (map) { if (!map.isEmpty()) { final ChatMessage lastMsg = map.get(map.lastKey()); while (aMsg.getDate().compareTo(lastMsg.getDate()) <= 0) { aMsg.setDate(new Date(1 + aMsg.getDate().getTime())); } aMsg.setPreviousMessageDate(lastMsg.getDate()); } map.put(aMsg.getDate(), aMsg); while (map.size() > MAX_ROOM_HIST) { map.remove(map.firstKey()); } } SynchroCache.put("ChatCache", m_chatCache); }