move (effective) [de]serialization logic into class

This commit is contained in:
Eric House 2015-02-26 06:18:19 -08:00
parent ed212801c8
commit 3cccb0cb11
4 changed files with 41 additions and 44 deletions

View file

@ -216,7 +216,7 @@ public class DBUtils {
int col = cursor.getColumnIndex( DBHelper.CONTYPE );
if ( 0 <= col ) {
tmp = cursor.getInt( col );
summary.conTypes = intToConnTypeSet( tmp );
summary.conTypes = new CommsConnTypeSet( tmp );
col = cursor.getColumnIndex( DBHelper.SEED );
if ( 0 < col ) {
summary.seed = cursor.getInt( col );
@ -319,7 +319,7 @@ public class DBUtils {
}
if ( null != summary.conTypes ) {
values.put( DBHelper.CONTYPE, connTypeSetToInt(summary.conTypes) );
values.put( DBHelper.CONTYPE, summary.conTypes.toInt() );
values.put( DBHelper.SEED, summary.seed );
values.put( DBHelper.NPACKETSPENDING, summary.nPacketsPending );
for ( Iterator<CommsConnType> iter = summary.conTypes.iterator();
@ -560,7 +560,7 @@ public class DBUtils {
int indx2 = cursor.getColumnIndex( DBHelper.CONTYPE );
for ( int ii = 0; cursor.moveToNext(); ++ii ) {
long rowid = cursor.getLong( indx1 );
CommsConnTypeSet typs = intToConnTypeSet( cursor.getInt(indx2) );
CommsConnTypeSet typs = new CommsConnTypeSet( cursor.getInt(indx2) );
// Better have an address if has pending sends
Assert.assertTrue( 0 < typs.size() );
result.put( rowid, typs );
@ -2136,37 +2136,4 @@ public class DBUtils {
s_cachedRowID = rowid;
s_cachedBytes = bytes;
}
private static final int BIT_VECTOR_MASK = 0x8000;
public static CommsConnTypeSet intToConnTypeSet( int asInt )
{
CommsConnTypeSet result = new CommsConnTypeSet();
boolean isVector = 0 != (BIT_VECTOR_MASK & asInt);
asInt &= ~BIT_VECTOR_MASK;
CommsConnType[] values = CommsConnType.values();
if ( isVector ) {
for ( CommsConnType value : values ) {
int ord = value.ordinal();
if ( 0 != (asInt & (1 << (ord - 1)))) {
result.add( value );
}
}
} else {
result.add( values[asInt] );
}
return result;
}
public static int connTypeSetToInt( CommsConnTypeSet set )
{
int result = BIT_VECTOR_MASK;
if ( null != set ) {
for ( Iterator<CommsConnType> iter = set.iterator(); iter.hasNext(); ) {
CommsConnType typ = iter.next();
result |= 1 << (typ.ordinal() - 1);
}
}
return result;
}
}

View file

@ -106,7 +106,7 @@ public class NetLaunchInfo {
btName = bundle.getString( MultiService.BT_NAME );
btAddress = bundle.getString( MultiService.BT_ADDRESS );
m_addrs = DBUtils.intToConnTypeSet( bundle.getInt( ADDRS_KEY ) );
m_addrs = new CommsConnTypeSet( bundle.getInt( ADDRS_KEY ) );
}
public NetLaunchInfo( Context context, Uri data )
@ -129,7 +129,7 @@ public class NetLaunchInfo {
m_inviteID = json.getString( MultiService.INVITEID );
} else {
int addrs = Integer.decode( data.getQueryParameter( ADDRS_KEY ) );
m_addrs = DBUtils.intToConnTypeSet( addrs );
m_addrs = new CommsConnTypeSet( addrs );
if ( m_addrs.contains( CommsConnType.COMMS_CONN_RELAY ) ) {
room = data.getQueryParameter( ROOM_KEY );
@ -240,7 +240,7 @@ public class NetLaunchInfo {
bundle.putString( MultiService.BT_ADDRESS, btAddress );
bundle.putInt( MultiService.FORCECHANNEL, forceChannel );
int flags = DBUtils.connTypeSetToInt( m_addrs );
int flags = m_addrs.toInt();
bundle.putInt( ADDRS_KEY, flags );
}
@ -249,7 +249,7 @@ public class NetLaunchInfo {
String result = null;
try {
JSONObject obj = new JSONObject()
.put( ADDRS_KEY, DBUtils.connTypeSetToInt( m_addrs ) )
.put( ADDRS_KEY, m_addrs.toInt() )
.put( MultiService.LANG, lang )
.put( MultiService.DICT, dict )
.put( MultiService.GAMENAME, gameName )
@ -313,7 +313,7 @@ public class NetLaunchInfo {
JSONObject json = new JSONObject( data );
int flags = json.getInt(ADDRS_KEY);
m_addrs = DBUtils.intToConnTypeSet( flags );
m_addrs = new CommsConnTypeSet( flags );
lang = json.optInt( MultiService.LANG, -1 );
forceChannel = json.optInt( MultiService.FORCECHANNEL, 0 );
@ -359,7 +359,7 @@ public class NetLaunchInfo {
public Uri makeLaunchUri( Context context )
{
int addrs = DBUtils.connTypeSetToInt( m_addrs );
int addrs = m_addrs.toInt();
Uri.Builder ub = new Uri.Builder()
.scheme( "http" )
.path( String.format( "//%s%s",

View file

@ -484,7 +484,7 @@ public class XWPrefs {
result.add( CommsConnType.COMMS_CONN_BT );
}
} else {
result = DBUtils.intToConnTypeSet( flags );
result = new CommsConnTypeSet( flags );
}
// Save it if changed
@ -499,7 +499,7 @@ public class XWPrefs {
public static void setAddrTypes( Context context, CommsConnTypeSet set )
{
int flags = DBUtils.connTypeSetToInt( set );
int flags = set.toInt();
setPrefsInt( context, R.string.key_addrs_pref, flags );
}

View file

@ -64,8 +64,38 @@ public class CommsAddrRec {
};
public static class CommsConnTypeSet extends HashSet<CommsConnType> {
private static final int BIT_VECTOR_MASK = 0x8000;
private static CommsConnTypeSet s_supported;
public CommsConnTypeSet() { this(BIT_VECTOR_MASK); }
public CommsConnTypeSet( int bits )
{
boolean isVector = 0 != (BIT_VECTOR_MASK & bits);
bits &= ~BIT_VECTOR_MASK;
CommsConnType[] values = CommsConnType.values();
if ( isVector ) {
for ( CommsConnType value : values ) {
int ord = value.ordinal();
if ( 0 != (bits & (1 << (ord - 1)))) {
add( value );
}
}
} else {
add( values[bits] );
}
}
public int toInt()
{
int result = BIT_VECTOR_MASK;
for ( Iterator<CommsConnType> iter = iterator(); iter.hasNext(); ) {
CommsConnType typ = iter.next();
result |= 1 << (typ.ordinal() - 1);
}
return result;
}
public static CommsConnTypeSet getSupported( Context context )
{
if ( null == s_supported ) {