Whoops, we add new history messages to the end, so start pruning from the

beginning. To do this more efficiently, we end up having to do some
jockeying.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2679 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2003-06-24 05:19:57 +00:00
parent 71109615bc
commit faee2e4088
@@ -1,5 +1,5 @@
// //
// $Id: SpeakProvider.java,v 1.12 2003/06/14 06:23:41 mdb Exp $ // $Id: SpeakProvider.java,v 1.13 2003/06/24 05:19:57 mdb Exp $
package com.threerings.crowd.chat.server; package com.threerings.crowd.chat.server;
@@ -229,7 +229,7 @@ public class SpeakProvider
*/ */
public static ArrayList getChatHistory (String username) public static ArrayList getChatHistory (String username)
{ {
ArrayList history = getHistoryList(username); HistoryList history = getHistoryList(username);
pruneHistory(System.currentTimeMillis(), history); pruneHistory(System.currentTimeMillis(), history);
return history; return history;
} }
@@ -256,7 +256,7 @@ public class SpeakProvider
} }
// add the message to this user's chat history // add the message to this user's chat history
ArrayList history = getHistoryList(username); HistoryList history = getHistoryList(username);
history.add(msg); history.add(msg);
// if the history is big enough, potentially prune it (we always // if the history is big enough, potentially prune it (we always
@@ -271,11 +271,11 @@ public class SpeakProvider
/** /**
* Returns this user's chat history, creating one if necessary. * Returns this user's chat history, creating one if necessary.
*/ */
protected static ArrayList getHistoryList (String username) protected static HistoryList getHistoryList (String username)
{ {
ArrayList history = (ArrayList)_histories.get(username); HistoryList history = (HistoryList)_histories.get(username);
if (history == null) { if (history == null) {
_histories.put(username, history = new ArrayList()); _histories.put(username, history = new HistoryList());
} }
return history; return history;
} }
@@ -283,17 +283,21 @@ public class SpeakProvider
/** /**
* Prunes all messages from this history which are expired. * Prunes all messages from this history which are expired.
*/ */
protected static void pruneHistory (long now, ArrayList history) protected static void pruneHistory (long now, HistoryList history)
{ {
for (int ii = history.size()-1; ii >= 0; ii--) { int prunepos = -1;
for (int ii = 0, ll = history.size(); ii < ll; ii++) {
ChatMessage msg = (ChatMessage)history.get(ii); ChatMessage msg = (ChatMessage)history.get(ii);
if (now - msg.timestamp > HISTORY_EXPIRATION) { if (now - msg.timestamp > HISTORY_EXPIRATION) {
Log.info("Expiring from history " + msg + "."); // Log.info("Expiring from history " + msg + ".");
history.remove(ii); prunepos = ii;
} else { } else {
break; // stop when we get to the first valid message break; // stop when we get to the first valid message
} }
} }
if (prunepos >= 0) {
history.remove(0, prunepos+1);
}
} }
/** Used to note the recipients of a chat message. */ /** Used to note the recipients of a chat message. */
@@ -313,6 +317,15 @@ public class SpeakProvider
} }
} }
/** Extends {@link ArrayList} for the sole purpose of exposing {@link
* ArrayList#removeRange}. Yay! */
protected static class HistoryList extends ArrayList
{
public void remove (int fromIndex, int toIndex) {
removeRange(fromIndex, toIndex);
}
}
/** Our speech object. */ /** Our speech object. */
protected DObject _speakObj; protected DObject _speakObj;