]> hydra-www.ietfng.org Git - acmetensortoys-ctfws-android/commitdiff
treewide: make messages a SortedSet, not a List
authorNathaniel Wesley Filardo <nwfilardo@gmail.com>
Sun, 22 Sep 2019 13:56:41 +0000 (14:56 +0100)
committerNathaniel Wesley Filardo <nwfilardo@gmail.com>
Sun, 22 Sep 2019 14:35:53 +0000 (15:35 +0100)
lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameStateManager.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSDisplayLocal.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java

index 0049391c652cd816e8a34bbc064dc575137683ae..c3889f33221323f0a46cd1164db65cf95dba9609 100644 (file)
@@ -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<Msg> msgs = new ArrayList<>();
+    private SortedSet<Msg> 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<Msg> msgs);
+        void onCtFwSMessage(CtFwSGameStateManager game, SortedSet<Msg> msgs);
     }
     final private Set<Observer> mObsvs = new HashSet<>();
     private synchronized void notifyFlags() {
index f73edd42c4a4ed0b08bb76fe827c0391cb45833f..261153a830ff42156caab5aa88078bb5680c8750 100644 (file)
@@ -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<CtFwSGameStateManager.Msg> msgs) {
+    public void onCtFwSMessage(CtFwSGameStateManager gs, SortedSet<CtFwSGameStateManager.Msg> 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<CtFwSGameStateManager.Msg> 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;
 
index 482e3b0bcd1282fb029aee85e13157879cf0c268..e94894bf1bdf7e212979f3a1cc1167a363cf9288 100644 (file)
@@ -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<CtFwSGameStateManager.Msg> msgs) {
+            public void onCtFwSMessage(CtFwSGameStateManager game, SortedSet<CtFwSGameStateManager.Msg> 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);