`
mmdev
  • 浏览: 12894393 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Contact数据模型之ValuesDelta

 
阅读更多
public static class ValuesDelta implements Parcelable
ValuesDelta位于com.android.contacts.model包的EntityDelta.java中,它是EntityDelta类的内部类,它实现了Parcelable接口。
ValuesDeltat的数据模型描述
ValuesDelta主要用于维护一个ContentValues的原始值和修改后的值。成员变量ContentValues mBefore对应原始值,ContentValues mAfter对应修改后的值。
ValuesDelta根据ContentValues mBeforeContentValues mAfter的状态代表3个意义
insert:mBefore不存在,而只有mAfter
update:mBefore和mAfter都存在
delete:只有mBefore存在
注1:ValuesDelta是EntityDelta的一个内部类。
注2:ContentValues mAfter对ContentValues mBefore相当于override的意思。
主要成员变量
protected ContentValues mBefore;
ContentValues的原始值
protected ContentValues mAfter;
ContentValues的新值
protected String mIdColumn = BaseColumns._ID;
用于表明ContentValues的哪一项是关键项(即ID项)
mIdColumn对于insert是暂时的。当是insert是时,在buildDiff(Uri targetUri)中,会把该mIdColumn对应的项目删除并没有把它加入ContentProviderOperation.Builder中。
private boolean mFromTemplate;
:具体意义不明
protected static int sNextInsertId = -1;
Next value to assign to {@link #mIdColumn} when building an insert
operation through {@link #fromAfter(ContentValues)}. This is used so
we can concretely reference this {@link ValuesDelta} before it has
been persisted.
插入的时候用,表示下一个插入项的id。
:对于insert来说mIdColumn是暂时的。当是insert是时,在buildDiff(Uri targetUri)中,会把该mIdColumn对应的项目删除并没有把它加入ContentProviderOperation.Builder中。
构造函数
protected ValuesDelta() {
}
空的构造函数。它主要是为Parcelable的实现读而提供的。
外部应该调用以下的静态方法来实例化
public static ValuesDelta fromBefore(ContentValues before)
:这样实例化的ValuesDelta是delete形态,但是可以调用put系列函数或mergeAfter(ValuesDelta local, ValuesDelta remote)后变成update形态。
public static ValuesDeltafromAfter(ContentValues after)
:这样实例化的ValuesDelta是insert形态,且永远只能是insert形态
public static ValuesDeltamergeAfter(ValuesDelta local, ValuesDelta remote)
:如果localnull,该函数会实例化一个ValuesDelta,它只有mAfter有值(mBefore为null).
这样实例化的ValuesDelta是insert形态,且永远只能是insert形态
主要静态函数
public static ValuesDeltafromBefore(ContentValues before)
用ContentValues before作为原始值ContentValues mBefore,来创建一个ValuesDelta。
并初始话ContentValues mAfter为一个空的ContentValues。
public static ValuesDeltafromAfter(ContentValues after)
用null作为原始值,ContentValues after作为新值,来创建一个ValuesDelta。
设置新值的mIdColumn为sNextInsertId,并把sNextInsertId下移。
public static ValuesDeltamergeAfter(ValuesDelta local, ValuesDelta remote)
把ValuesDelta remote的值合并到ValuesDelta local中。
如果ValuesDelta local的mBefore不存在,就用ValuesDelta remote的getCompleteValues方法返回值赋予mAfter。
否则,直接把ValuesDelta remote的mAfter赋予ValuesDelta local的mAfter.
最后返回新的ValuesDelta local。
:如果local是null,会创建新的ValuesDelta,并赋予它。
主要成员函数
public ContentValuesgetAfter()
得到新值ContentValues mAfter
public StringgetAsString(String key)
得到ContentValues中的key的值,且该值是String的形式。如果在新值mAfter中有key的值,就调用mAfter的getAsString来得,
否则如果在原始值mBefore中有key的值,就调用mBefore的getAsString来得,如果都没有则返回null.
public byte[]getAsByteArray(String key)
得到ContentValues中的key的值,且该值是byte[]的形式。原理同上。
public LonggetAsLong(String key)
得到ContentValues中的key的值,且该值是Long的形式。原理同上。
public IntegergetAsInteger(String key)
得到ContentValues中的key的值,且该值是Integer的形式。原理同上。
public IntegergetAsInteger(String key, Integer defaultValue)
得到ContentValues中的key的值,且该值是Integer的形式。原理同上。只是如果没找key对应的值,则返回Integer defaultValue。
public StringgetMimetype()
得到Data.MIMETYPE这个key的值。他是直接调用getAsString(Data.MIMETYPE);
public LonggetId()
得到mIdColumn这个key所对应的值。他是直接调用getAsLong(mIdColumn);
public void setIdColumn(String idColumn)
设置mIdColumn的值为idColumn
public booleanisPrimary()
返回Data.IS_PRIMARY这个key所对应的值
public voidsetFromTemplate(boolean isFromTemplate)
设置mFromTemplate的值为isFromTemplate
public booleanisFromTemplate()
返回mFromTemplate的值
public booleanisSuperPrimary()
返回Data.IS_SUPER_PRIMARY这个key所对应的值。
public booleanbeforeExists()
返回mBefore是否包含有mIdColumn这个key
public booleanisVisible()
返回true,如果mAfter不为空。否则返回false.因为如果mAfter为空,就表示删除,当然就不可见。
public booleanisDelete()
返回true,如果beforeExists()为true且mAfter为null,否则返回false
public booleanisTransient()
返回true,如果mBefore和mAfter都为null,否则返回false
public booleanisUpdate()
返回true,如果beforeExists()为true且 mAfter.size()大于0,否则返回false
public booleanisNoop()
返回true,如果beforeExists()为true且mAfter.size()等于0(mAfter必须不为null),否则返回false.
:NOOP是No operation的意思
public booleanisInsert()
返回true,如果beforeExists()为false且mAfter不为null
public voidmarkDeleted()
把mAfter设置为null
private voidensureUpdate()
Ensure that our internal structure is ready for storing updates.
其实就是检查mAfter是否为null,如果mAfter为null,就创建一个ContentValues并把它赋予mAfter.
public voidput(String key, String value)
首先调用ensureUpdate(),然后调用mAfter.put(String key, String value)来存放key所对应的值value.
public voidput(String key, byte[] value)
和上同理
public voidput(String key, int value)
和上同理
public Set<String>keySet()
得到mBefore和mAfter中所有的key
public ContentValuesgetCompleteValues()
得到完整的Values,mAfter重写mBefore后的Values,如果他包含GroupMembership.GROUP_SOURCE_ID,把其所对应的项从Values移除。
public booleanequals(Object object)
调用subsetEquals来进行比较的,且是双向比较。
public String toString()
调用toString(StringBuilder builder)来实现的。
public void toString(StringBuilder builder)
调用KeySet,将其返回的结果加入到StringBuilder builder中。
public booleansubsetEquals(ValuesDelta other)
比较他们的values是否相当。
:mAfter和mBefore是override的关系。
public ContentProviderOperation.BuilderbuildDiff(Uri targetUri)
把ValuesDelta自己转化为ContentProviderOperation.Builder。如果isInsert()为ture,就转化为insert.delete,update同理。
Uri targetUri是ContentProviderOperation.Builder操作(insert,update,delete)所针对的Url。
public int describeContents()
返回0.
:它来自Parcelable接口。
public voidwriteToParcel(Parcel dest, int flags)
把mBefore,mAfter,mIdColumn写入Parcel dest。
注1:mBefore,mAfter是ContentValues,ContentValues是实现了Parcelable接口的。所以直接用 dest.writeParcelable(mBefore, flags)这种形式就可以了。
注2:它来自Parcelable接口。
public void readFromParcel(Parcel source)
从Parcel source中读取mBefore,mAfter,mIdColumn。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics