From: Nathaniel Wesley Filardo Date: Sun, 22 Sep 2019 13:56:41 +0000 (+0100) Subject: treewide: make messages a SortedSet, not a List X-Git-Tag: release-1.4~10 X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=dbb0797423eb07ac3f9835b7d9444bbf6b801b1b;p=acmetensortoys-ctfws-android treewide: make messages a SortedSet, not a List --- diff --git a/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameStateManager.java b/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameStateManager.java index 0049391..c3889f3 100644 --- a/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameStateManager.java +++ b/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameStateManager.java @@ -1,6 +1,5 @@ package com.acmetensortoys.ctfwstimer.lib; -import java.util.ArrayList; import java.util.HashSet; import java.util.InputMismatchException; import java.util.List; @@ -8,6 +7,8 @@ import java.util.Locale; import java.util.NoSuchElementException; import java.util.Scanner; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; public class CtFwSGameStateManager { @@ -262,7 +263,7 @@ public class CtFwSGameStateManager { return 0 == this.compareTo((Msg)o); } } - private final List msgs = new ArrayList<>(); + private SortedSet msgs = new TreeSet<>(); private long lastMsgTimestamp; public void onNewMessage(String str) { Msg m = null; @@ -288,8 +289,7 @@ public class CtFwSGameStateManager { // XXX this is bogus if (isMessageTimeWithin(t) && (lastMsgTimestamp <= t)) { lastMsgTimestamp = t; - if (!msgs.contains(m)) { - msgs.add(m); + if (msgs.add(m)) { notifyMessages(); } } @@ -297,13 +297,8 @@ public class CtFwSGameStateManager { } public void onMessageReset(long before) { synchronized(this) { - while(!msgs.isEmpty()) { - Msg m = msgs.get(0); - if (m.when <= before) { - msgs.remove(0); - } else { - break; - } + if (!msgs.isEmpty() && msgs.first().when <= before) { + msgs = msgs.tailSet(new Msg(before, "")); } notifyMessages(); } @@ -325,7 +320,7 @@ public class CtFwSGameStateManager { // (or since the last), even though usually one only cares about the most recent // entry on the list. We reserve the right to trim this list in the future, but // at the moment we do not. Callees should not alter the list in any way. - void onCtFwSMessage(CtFwSGameStateManager game, List msgs); + void onCtFwSMessage(CtFwSGameStateManager game, SortedSet msgs); } final private Set mObsvs = new HashSet<>(); private synchronized void notifyFlags() { diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSDisplayLocal.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSDisplayLocal.java index f73edd4..261153a 100644 --- a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSDisplayLocal.java +++ b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSDisplayLocal.java @@ -18,8 +18,7 @@ import android.widget.TextView; import com.acmetensortoys.ctfwstimer.lib.CtFwSGameStateManager; import java.text.NumberFormat; -import java.util.List; -import java.util.ListIterator; +import java.util.SortedSet; import static android.view.View.INVISIBLE; @@ -374,7 +373,7 @@ class CtFwSDisplayLocal implements CtFwSGameStateManager.Observer { } @Override - public void onCtFwSMessage(CtFwSGameStateManager gs, List msgs) { + public void onCtFwSMessage(CtFwSGameStateManager gs, SortedSet msgs) { final TextView msgstv = mAct.findViewById(R.id.msgs); int s = msgs.size(); @@ -389,10 +388,7 @@ class CtFwSDisplayLocal implements CtFwSGameStateManager.Observer { } final StringBuffer sb = new StringBuffer(); - for (ListIterator news = msgs.listIterator(0); - news.hasNext(); ) { - - CtFwSGameStateManager.Msg m = news.next(); + for (CtFwSGameStateManager.Msg m : msgs) { long td = (m.when == 0) ? 0 : (gs.isConfigured()) ? m.when - gs.getStartT() : 0; diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java index 482e3b0..e94894b 100644 --- a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java +++ b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java @@ -21,6 +21,7 @@ import com.acmetensortoys.ctfwstimer.lib.CtFwSGameStateManager; import java.text.NumberFormat; import java.util.List; +import java.util.SortedSet; class MainServiceNotification { final public String CTFWS_GAME_CHANNEL_ID = "GAME"; @@ -151,14 +152,14 @@ class MainServiceNotification { private int lastMsgIx = 0; @Override - public void onCtFwSMessage(CtFwSGameStateManager game, List msgs) { + public void onCtFwSMessage(CtFwSGameStateManager game, SortedSet msgs) { // Only do anything if we have added something to the list since last we looked // and if it's in (or after) the current game. // Always update the length in case this is a reset to zero. int s = msgs.size(); Log.d("CtFwSNotify", "on msg s=" + s + " lastix=" + lastMsgIx); if (s > lastMsgIx) { - CtFwSGameStateManager.Msg m = msgs.get(s-1); + CtFwSGameStateManager.Msg m = msgs.last(); Log.d("CtFwsNotify", "msg gst=" + game.getStartT() + " when=" + m.when); if (game.isConfigured() && m.when >= game.getStartT()) { notifyUserSomehow(NotificationSource.MESG);