From 2a845da9b6a2568005afeceb69223c8be89504bb Mon Sep 17 00:00:00 2001 From: Eric House Date: Sun, 6 Jan 2019 16:29:52 -0800 Subject: [PATCH] use List instead of array --- .../eehouse/android/xw4/BTInviteDelegate.java | 14 ++--- .../eehouse/android/xw4/InviteDelegate.java | 54 ++++++------------- .../android/xw4/RelayInviteDelegate.java | 2 +- .../android/xw4/SMSInviteDelegate.java | 2 +- .../android/xw4/WiDirInviteDelegate.java | 10 ++-- 5 files changed, 32 insertions(+), 50 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTInviteDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTInviteDelegate.java index f735cfe0d..80f9f13cf 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTInviteDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTInviteDelegate.java @@ -34,10 +34,10 @@ import org.eehouse.android.xw4.DBUtils.SentInvitesInfo; import org.eehouse.android.xw4.DlgDelegate.Action; import java.io.Serializable; -import java.util.Arrays; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; -import java.util.Iterator; +import java.util.List; import java.util.Set; public class BTInviteDelegate extends InviteDelegate { @@ -51,7 +51,7 @@ public class BTInviteDelegate extends InviteDelegate { private ProgressDialog m_progress; private static class Persisted implements Serializable { - TwoStringPair[] pairs; + List pairs; // HashMap: m_stamps is serialized, so can't be abstract type HashMap stamps = new HashMap<>(); @@ -75,20 +75,20 @@ public class BTInviteDelegate extends InviteDelegate { sort(); } - void remove(final Set checked) + void remove(final Set checked) { for ( InviterItem item : checked ) { TwoStringPair pair = (TwoStringPair)item; stamps.remove( pair.str2 ); - pairs = TwoStringPair.remove( pairs, pair ); + TwoStringPair.remove( pairs, pair ); } } - boolean empty() { return pairs == null || pairs.length == 0; } + boolean empty() { return pairs == null || pairs.size() == 0; } private void sort() { - Arrays.sort( pairs, new Comparator() { + Collections.sort( pairs, new Comparator() { @Override public int compare( TwoStringPair rec1, TwoStringPair rec2 ) { long val1 = stamps.get( rec1.str2 ); diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/InviteDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/InviteDelegate.java index 9e8de5b80..36a322027 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/InviteDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/InviteDelegate.java @@ -38,10 +38,12 @@ import android.widget.Spinner; import android.widget.TextView; import java.io.Serializable; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Set; @@ -63,46 +65,21 @@ abstract class InviteDelegate extends ListDelegateBase this.str1 = str1; this.str2 = str2; } - public static TwoStringPair[] make( String[] names, String[] addrs ) - { - TwoStringPair[] pairs = new TwoStringPair[names.length]; - for ( int ii = 0; ii < pairs.length; ++ii ) { - pairs[ii] = new TwoStringPair( names[ii], addrs[ii] ); - } - return pairs; - } - - public static TwoStringPair[] add( TwoStringPair[] pairs, String name, - String addr ) + public static List add( List pairs, String name, + String addr ) { if ( null == pairs ) { - pairs = new TwoStringPair[0]; + pairs = new ArrayList<>(); } - pairs = Arrays.copyOfRange( pairs, 0, pairs.length + 1 ); - pairs[pairs.length-1] = new TwoStringPair( name, addr ); + pairs.add( new TwoStringPair( name, addr ) ); return pairs; } - public static TwoStringPair[] remove( TwoStringPair[] pairs, InviterItem dead ) + public static void remove( List pairs, InviterItem dead ) { - int pos = -1; - for ( int ii = 0; ii < pairs.length; ++ii ) { - TwoStringPair pair = pairs[ii]; - if ( pair.equals(dead) ) { - pos = ii; - break; - } - } - - TwoStringPair[] newPairs = new TwoStringPair[pairs.length - 1]; - // lower range - System.arraycopy(pairs, 0, newPairs, 0, pos ); - // upper range - System.arraycopy(pairs, pos + 1, newPairs, pos, newPairs.length - pos ); - - return newPairs; + pairs.remove(dead); } public String getDev() { return str1; } @@ -191,12 +168,13 @@ abstract class InviteDelegate extends ListDelegateBase tryEnable(); } - protected void updateListAdapter( InviterItem[] items ) + protected void updateListAdapter( List items ) { updateListAdapter( R.layout.two_strs_item, items ); } - protected void updateListAdapter( int itemId, InviterItem[] items ) + protected void updateListAdapter( int itemId, + List items ) { updateChecked( items ); m_adapter = new InviteItemsAdapter( itemId, items ); @@ -282,7 +260,7 @@ abstract class InviteDelegate extends ListDelegateBase protected void clearChecked() { m_checked.clear(); } // Figure which previously-checked items belong in the new set. - private void updateChecked( InviterItem[] newItems ) + private void updateChecked( List newItems ) { Set old = new HashSet(); old.addAll( m_checked ); @@ -318,11 +296,13 @@ abstract class InviteDelegate extends ListDelegateBase private InviterItem[] m_items; private int m_itemId; - public InviteItemsAdapter( int itemID, InviterItem[] items ) + public InviteItemsAdapter( int itemID, List items ) { - super( null == items? 0 : items.length ); + super( null == items? 0 : items.size() ); m_itemId = itemID; - m_items = items; + if ( null != items ) { + m_items = items.toArray( new InviterItem[items.size()] ); + } // m_items = new LinearLayout[getCount()]; } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayInviteDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayInviteDelegate.java index 8d738b3c2..966cd3ad2 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayInviteDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayInviteDelegate.java @@ -463,7 +463,7 @@ public class RelayInviteDelegate extends InviteDelegate { }); addSelf(); - updateListAdapter( m_devIDRecs.toArray( new DevIDRec[m_devIDRecs.size()] ) ); + updateListAdapter( m_devIDRecs ); tryEnable(); } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSInviteDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSInviteDelegate.java index 23942077a..d3b11ad22 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSInviteDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSInviteDelegate.java @@ -298,7 +298,7 @@ public class SMSInviteDelegate extends InviteDelegate { } }); - updateListAdapter( m_phoneRecs.toArray( new PhoneRec[m_phoneRecs.size()] ) ); + updateListAdapter( m_phoneRecs ); tryEnable(); } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/WiDirInviteDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/WiDirInviteDelegate.java index 6d39ffe53..7628604f8 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/WiDirInviteDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/WiDirInviteDelegate.java @@ -27,9 +27,11 @@ import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.TextView; -import java.util.Iterator; -import java.util.Map; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; public class WiDirInviteDelegate extends InviteDelegate implements WiDirService.DevSetListener { @@ -106,13 +108,13 @@ public class WiDirInviteDelegate extends InviteDelegate private void rebuildList() { int count = m_macsToName.size(); - TwoStringPair[] pairs = new TwoStringPair[count]; + List pairs = new ArrayList<>(); // String[] names = new String[count]; // String[] addrs = new String[count]; Iterator iter = m_macsToName.keySet().iterator(); for ( int ii = 0; ii < count; ++ii ) { String mac = iter.next(); - pairs[ii] = new TwoStringPair(mac, m_macsToName.get(mac) ); + pairs.add( new TwoStringPair(mac, m_macsToName.get(mac) ) ); // addrs[ii] = mac; // names[ii] = m_macsToName.get(mac); }