use List instead of array

This commit is contained in:
Eric House 2019-01-06 16:29:52 -08:00
parent 83340567b2
commit 2a845da9b6
5 changed files with 32 additions and 50 deletions

View file

@ -34,10 +34,10 @@ import org.eehouse.android.xw4.DBUtils.SentInvitesInfo;
import org.eehouse.android.xw4.DlgDelegate.Action; import org.eehouse.android.xw4.DlgDelegate.Action;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.List;
import java.util.Set; import java.util.Set;
public class BTInviteDelegate extends InviteDelegate { public class BTInviteDelegate extends InviteDelegate {
@ -51,7 +51,7 @@ public class BTInviteDelegate extends InviteDelegate {
private ProgressDialog m_progress; private ProgressDialog m_progress;
private static class Persisted implements Serializable { private static class Persisted implements Serializable {
TwoStringPair[] pairs; List<TwoStringPair> pairs;
// HashMap: m_stamps is serialized, so can't be abstract type // HashMap: m_stamps is serialized, so can't be abstract type
HashMap<String, Long> stamps = new HashMap<>(); HashMap<String, Long> stamps = new HashMap<>();
@ -75,20 +75,20 @@ public class BTInviteDelegate extends InviteDelegate {
sort(); sort();
} }
void remove(final Set<InviterItem> checked) void remove(final Set<? extends InviterItem> checked)
{ {
for ( InviterItem item : checked ) { for ( InviterItem item : checked ) {
TwoStringPair pair = (TwoStringPair)item; TwoStringPair pair = (TwoStringPair)item;
stamps.remove( pair.str2 ); 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() private void sort()
{ {
Arrays.sort( pairs, new Comparator<TwoStringPair>() { Collections.sort( pairs, new Comparator<TwoStringPair>() {
@Override @Override
public int compare( TwoStringPair rec1, TwoStringPair rec2 ) { public int compare( TwoStringPair rec1, TwoStringPair rec2 ) {
long val1 = stamps.get( rec1.str2 ); long val1 = stamps.get( rec1.str2 );

View file

@ -38,10 +38,12 @@ import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -63,46 +65,21 @@ abstract class InviteDelegate extends ListDelegateBase
this.str1 = str1; this.str2 = str2; this.str1 = str1; this.str2 = str2;
} }
public static TwoStringPair[] make( String[] names, String[] addrs ) public static List<TwoStringPair> add( List<TwoStringPair> pairs, String name,
{
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 ) String addr )
{ {
if ( null == pairs ) { if ( null == pairs ) {
pairs = new TwoStringPair[0]; pairs = new ArrayList<>();
} }
pairs = Arrays.copyOfRange( pairs, 0, pairs.length + 1 ); pairs.add( new TwoStringPair( name, addr ) );
pairs[pairs.length-1] = new TwoStringPair( name, addr );
return pairs; return pairs;
} }
public static TwoStringPair[] remove( TwoStringPair[] pairs, InviterItem dead ) public static void remove( List<TwoStringPair> pairs, InviterItem dead )
{ {
int pos = -1; pairs.remove(dead);
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;
} }
public String getDev() { return str1; } public String getDev() { return str1; }
@ -191,12 +168,13 @@ abstract class InviteDelegate extends ListDelegateBase
tryEnable(); tryEnable();
} }
protected void updateListAdapter( InviterItem[] items ) protected void updateListAdapter( List<? extends InviterItem> items )
{ {
updateListAdapter( R.layout.two_strs_item, items ); updateListAdapter( R.layout.two_strs_item, items );
} }
protected void updateListAdapter( int itemId, InviterItem[] items ) protected void updateListAdapter( int itemId,
List<? extends InviterItem> items )
{ {
updateChecked( items ); updateChecked( items );
m_adapter = new InviteItemsAdapter( itemId, items ); m_adapter = new InviteItemsAdapter( itemId, items );
@ -282,7 +260,7 @@ abstract class InviteDelegate extends ListDelegateBase
protected void clearChecked() { m_checked.clear(); } protected void clearChecked() { m_checked.clear(); }
// Figure which previously-checked items belong in the new set. // Figure which previously-checked items belong in the new set.
private void updateChecked( InviterItem[] newItems ) private void updateChecked( List<? extends InviterItem> newItems )
{ {
Set<InviterItem> old = new HashSet<InviterItem>(); Set<InviterItem> old = new HashSet<InviterItem>();
old.addAll( m_checked ); old.addAll( m_checked );
@ -318,11 +296,13 @@ abstract class InviteDelegate extends ListDelegateBase
private InviterItem[] m_items; private InviterItem[] m_items;
private int m_itemId; private int m_itemId;
public InviteItemsAdapter( int itemID, InviterItem[] items ) public InviteItemsAdapter( int itemID, List<? extends InviterItem> items )
{ {
super( null == items? 0 : items.length ); super( null == items? 0 : items.size() );
m_itemId = itemID; m_itemId = itemID;
m_items = items; if ( null != items ) {
m_items = items.toArray( new InviterItem[items.size()] );
}
// m_items = new LinearLayout[getCount()]; // m_items = new LinearLayout[getCount()];
} }

View file

@ -463,7 +463,7 @@ public class RelayInviteDelegate extends InviteDelegate {
}); });
addSelf(); addSelf();
updateListAdapter( m_devIDRecs.toArray( new DevIDRec[m_devIDRecs.size()] ) ); updateListAdapter( m_devIDRecs );
tryEnable(); tryEnable();
} }

View file

@ -298,7 +298,7 @@ public class SMSInviteDelegate extends InviteDelegate {
} }
}); });
updateListAdapter( m_phoneRecs.toArray( new PhoneRec[m_phoneRecs.size()] ) ); updateListAdapter( m_phoneRecs );
tryEnable(); tryEnable();
} }

View file

@ -27,9 +27,11 @@ import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.TextView; 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 public class WiDirInviteDelegate extends InviteDelegate
implements WiDirService.DevSetListener { implements WiDirService.DevSetListener {
@ -106,13 +108,13 @@ public class WiDirInviteDelegate extends InviteDelegate
private void rebuildList() private void rebuildList()
{ {
int count = m_macsToName.size(); int count = m_macsToName.size();
TwoStringPair[] pairs = new TwoStringPair[count]; List<TwoStringPair> pairs = new ArrayList<>();
// String[] names = new String[count]; // String[] names = new String[count];
// String[] addrs = new String[count]; // String[] addrs = new String[count];
Iterator<String> iter = m_macsToName.keySet().iterator(); Iterator<String> iter = m_macsToName.keySet().iterator();
for ( int ii = 0; ii < count; ++ii ) { for ( int ii = 0; ii < count; ++ii ) {
String mac = iter.next(); 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; // addrs[ii] = mac;
// names[ii] = m_macsToName.get(mac); // names[ii] = m_macsToName.get(mac);
} }