ContentValues

/**

 *This class is used to store a set of values that the {@link ContentResolver}

 *can process.

 */

 

/* ContentValues

 * 这个类被用于存储一个集合,这些值能被ContentResolver处理

 * 该类只是简单的封装了HashMap

 * */

 

public final classContentValues implementsParcelable {

    public static final String TAG = "ContentValues";

 

    /** Holds the actual values */

    /* 拥有实际的值*/

    private HashMap<String, Object> mValues;

 

    /**

    * Creates an empty set of values using the default initial size

    */

   

    /* 创建一个空集合用于初始化默认大小,默认8个键值对

    */

    public ContentValues() {

       // Choosing a default size of 8 based onanalysis of typical

       // consumption by applications.

       //选择一个默认大小8,这个值是基于应用的典型消耗分析得出的。初始化8个键值对比较有效率

       mValues= newHashMap<String, Object>(8);

    }

 

    /**

    * Creates an empty set of values using the given initial size

    *

    * @param size the initial size of the set of values

    */

   

    /* 使用size参数创建一个size大小的空集合,

    * @param size

    */

    public ContentValues(int size) {

       mValues= newHashMap<String, Object>(size, 1.0f);

    }

 

    /**

    * Creates a set of values copied from the given set

    *

    * @param from the values to copy

    */

   

    /* 从一个给定的集合创建一个新集合

    * 从一个ContentValues创建另一个ContentValues

     * @param from

    */

    public ContentValues(ContentValues from) {

       mValues= newHashMap<String, Object>(from.mValues);

    }

 

    /**

    * Creates a set of values copied from the given HashMap. This is used

    * by the Parcel unmarshalling code.

    *

    * @param values the values to start with

    * {@hide}

    */

   

    /* 从一个HashMap创建ContentValues,起始就是索引的赋值

    * @param values

    */

    private ContentValues(HashMap<String, Object>values) {

       mValues= values;

    }

    /*判断是否是同一个HashMap,HashMap的对象的索引值是同一个即相同*/

    @Override

    public booleanequals(Object object) {

       if(!(object instanceofContentValues)) {

           return false;

       }

       return mValues.equals(((ContentValues) object).mValues);

    }

 

    @Override

    public inthashCode() {

       return mValues.hashCode();

    }

 

    /**

    * Adds a value to the set.

    *

    * @param key the name of the value to put

    * @param value the data for the value to put

    */

    //添加一个值到集合

    public voidput(String key, String value) {

       mValues.put(key,value);

    }

 

    /**

    * Adds all values from the passed in ContentValues.

    *

    * @param other the ContentValues from which to copy

    */

    //将ContentValues里的HashMap的值全都赋值给当前ContentValues里的HashMap

    public voidputAll(ContentValues other) {

       mValues.putAll(other.mValues);

    }

 

    /**

    * Adds a value to the set.

    *

    * @param key the name of the value to put

    * @param value the data for the value to put

    */

    //添加一个Byte类型的值到集合

    public voidput(String key, Byte value) {

       mValues.put(key,value);

    }

 

    /**

    * Adds a value to the set.

    *

    * @param key the name of the value to put

    * @param value the data for the value to put

    */

    public voidput(String key, Short value) {

       mValues.put(key,value);

    }

 

    /**

    * Adds a value to the set.

    *

    * @param key the name of the value to put

    * @param value the data for the value to put

    */

    public voidput(String key, Integer value) {

       mValues.put(key,value);

    }

 

    /**

    * Adds a value to the set.

    *

    * @param key the name of the value to put

    * @param value the data for the value to put

    */

    public voidput(String key, Long value) {

       mValues.put(key,value);

    }

 

    /**

    * Adds a value to the set.

    *

    * @param key the name of the value to put

    * @param value the data for the value to put

    */

    public voidput(String key, Float value) {

       mValues.put(key,value);

    }

 

    /**

    * Adds a value to the set.

    *

    * @param key the name of the value to put

    * @param value the data for the value to put

    */

    public voidput(String key, Double value) {

       mValues.put(key,value);

    }

 

    /**

    * Adds a value to the set.

    *

    * @param key the name of the value to put

    * @param value the data for the value to put

    */

    public voidput(String key, Boolean value) {

       mValues.put(key,value);

    }

 

    /**

    * Adds a value to the set.

    *

    * @param key the name of the value to put

    * @param value the data for the value to put

    */

    public voidput(String key, byte[]value) {

       mValues.put(key,value);

    }

 

    /**

    * Adds a null value to the set.

    *

    * @param key the name of the value to make null

    */

    public voidputNull(String key) {

       mValues.put(key,null);

    }

 

    /**

    * Returns the number of values.

    *

    * @return the number of values

    */

    //集合的大小,值的数量

    public intsize() {

       return mValues.size();

    }

 

    /**

    * Remove a single value.

    *

    * @param key the name of the value to remove

    */

    //移除一个键值对

    public voidremove(String key) {

       mValues.remove(key);

    }

 

    /**

    * Removes all values.

    */

    //清除所有键值对

    public voidclear() {

       mValues.clear();

    }

 

    /**

    * Returns true if this object has the named value.

    *

    * @param key the value to check for

    * @return {@code true} if the value is present, {@code false} otherwise

    */

    //判断是否包含这个key,是则返回true否则false

    public booleancontainsKey(String key) {

       return mValues.containsKey(key);

    }

 

    /**

    * Gets a value. Valid value types are {@link String}, {@linkBoolean}, and

    * {@link Number} implementations.

    *

    * @param key the value to get

    * @return the data for the value

    */

    //获取一个值,值的类型是 字符串,布尔值和数字,作为对象返回

    public Object get(String key) {

       return mValues.get(key);

    }

 

    /**

    * Gets a value and converts it to a String.

    *

    * @param key the value to get

    * @return the String for the value

    */

    //将获取的值转化为字符串返回

    public String getAsString(String key) {

       Object value = mValues.get(key);

       returnvalue != null? value.toString() : null;

    }

 

    /**

    * Gets a value and converts it to a Long.

    *

    * @param key the value to get

    * @return the Long value, or null if the value is missing or cannot be converted

    */

    public Long getAsLong(String key) {

       Object value = mValues.get(key);

       try{

           returnvalue != null? ((Number) value).longValue() : null;

       } catch(ClassCastException e) {

           if(value instanceofCharSequence) {

                try {

                    return Long.valueOf(value.toString());

                } catch (NumberFormatException e2) {

                    Log.e(TAG, "Cannotparse Long value for " + value + " at key " + key);

                    return null;

                }

           } else{

                Log.e(TAG, "Cannotcast value for " + key + " to a Long: " + value, e);

                return null;

           }

       }

    }

 

    /**

    * Gets a value and converts it to an Integer.

    *

    * @param key the value to get

    * @return the Integer value, or null if the value is missing or cannot be converted

    */

    public Integer getAsInteger(String key) {

       Object value = mValues.get(key);

       try{

           returnvalue != null? ((Number) value).intValue() : null;

        } catch (ClassCastException e) {

           if(value instanceofCharSequence) {

                try {

                    return Integer.valueOf(value.toString());

                } catch (NumberFormatException e2) {

                    Log.e(TAG, "Cannotparse Integer value for " + value + " at key " + key);

                    return null;

                }

           } else{

                Log.e(TAG, "Cannotcast value for " + key + " to a Integer: " + value, e);

                return null;

           }

       }

    }

 

    /**

    * Gets a value and converts it to a Short.

    *

    * @param key the value to get

    * @return the Short value, or null if the value is missing or cannot be converted

    */

    public Short getAsShort(String key) {

       Object value = mValues.get(key);

       try{

           returnvalue != null? ((Number) value).shortValue() : null;

       } catch(ClassCastException e) {

           if(value instanceofCharSequence) {

                try {

                    return Short.valueOf(value.toString());

                } catch (NumberFormatException e2) {

                    Log.e(TAG, "Cannotparse Short value for " + value + " at key " + key);

                    return null;

                }

           } else{

                Log.e(TAG, "Cannotcast value for " + key + " to a Short: " + value, e);

                return null;

           }

       }

    }

 

    /**

    * Gets a value and converts it to a Byte.

    *

    * @param key the value to get

    * @return the Byte value, or null if the value is missing or cannot be converted

    */

    public Byte getAsByte(String key) {

       Object value = mValues.get(key);

       try{

           returnvalue != null? ((Number) value).byteValue() : null;

       } catch(ClassCastException e) {

           if(value instanceofCharSequence) {

                try {

                    return Byte.valueOf(value.toString());

                } catch (NumberFormatException e2) {

                    Log.e(TAG, "Cannotparse Byte value for " + value + " at key " + key);

                    return null;

                }

           } else{

                Log.e(TAG, "Cannotcast value for " + key + " to a Byte: " + value, e);

                return null;

           }

       }

    }

 

    /**

    * Gets a value and converts it to a Double.

    *

    * @param key the value to get

    * @return the Double value, or null if the value is missing or cannot be converted

    */

    public Double getAsDouble(String key) {

       Object value = mValues.get(key);

       try{

           returnvalue != null? ((Number) value).doubleValue() : null;

       } catch(ClassCastException e) {

           if(value instanceofCharSequence) {

                try {

                    return Double.valueOf(value.toString());

                } catch (NumberFormatException e2) {

                    Log.e(TAG, "Cannotparse Double value for " + value + " at key " + key);

                    return null;

                }

           } else{

                Log.e(TAG, "Cannotcast value for " + key + " to a Double: " + value, e);

                return null;

           }

       }

    }

 

    /**

    * Gets a value and converts it to a Float.

    *

    * @param key the value to get

    * @return the Float value, or null if the value is missing or cannot be converted

    */

    public Float getAsFloat(String key) {

       Object value = mValues.get(key);

       try{

           returnvalue != null? ((Number) value).floatValue() : null;

       } catch(ClassCastException e) {

           if(value instanceofCharSequence) {

                try {

                    return Float.valueOf(value.toString());

                } catch (NumberFormatException e2) {

                    Log.e(TAG, "Cannotparse Float value for " + value + " at key " + key);

                    return null;

                }

           } else{

                Log.e(TAG, "Cannotcast value for " + key + " to a Float: " + value, e);

                return null;

           }

       }

    }

 

    /**

    * Gets a value and converts it to a Boolean.

    *

    * @param key the value to get

    * @return the Boolean value, or null if the value is missing or cannot be converted

    */

    public Boolean getAsBoolean(String key) {

       Object value = mValues.get(key);

       try{

           return(Boolean) value;

       } catch(ClassCastException e) {

           if(value instanceofCharSequence) {

                return Boolean.valueOf(value.toString());

           } else if (value instanceof Number) {

                return ((Number) value).intValue() != 0;

           } else{

                Log.e(TAG, "Cannotcast value for " + key + " to a Boolean: " + value, e);

                return null;

           }

       }

    }

 

    /**

    * Gets a value that is a byte array. Note that this method will notconvert

    * any other types to byte arrays.

    *

    * @param key the value to get

    * @return the byte[] value, or null is the value is missing or not a byte[]

    */

    public byte[]getAsByteArray(String key) {

       Object value = mValues.get(key);

       if(value instanceof byte[]) {

           return(byte[]) value;

       } else{

           return null;

       }

    }

 

    /**

    * Returns a set of all of the keys and values

    *

    * @return a set of all of the keys and values

    */

    public Set<Map.Entry<String, Object>>valueSet() {

       return mValues.entrySet();

    }

 

    /**

    * Returns a set of all of the keys

    *

    * @return a set of all of the keys

    */

    public Set<String> keySet() {

       return mValues.keySet();

    }

 

    public static final Parcelable.Creator<ContentValues> CREATOR =

           newParcelable.Creator<ContentValues>() {

       @SuppressWarnings({"deprecation", "unchecked"})

       publicContentValues createFromParcel(Parcel in) {

           // TODO - what ClassLoader should be passed toreadHashMap?

           HashMap<String, Object> values = in.readHashMap(null);

           return new ContentValues(values);

       }

 

       publicContentValues[] newArray(intsize) {

           return new ContentValues[size];

       }

    };

 

    public intdescribeContents() {

       return0;

    }

 

    @SuppressWarnings("deprecation")

    public voidwriteToParcel(Parcel parcel, intflags) {

       parcel.writeMap(mValues);

    }

 

    /**

    * Unsupported, here until we get proper bulk insert APIs.

    * {@hide}

    */

    @Deprecated

    public void putStringArrayList(Stringkey, ArrayList<String> value) {

       mValues.put(key,value);

    }

 

    /**

    * Unsupported, here until we get proper bulk insert APIs.

    * {@hide}

    */

    @SuppressWarnings("unchecked")

    @Deprecated

    public ArrayList<String> getStringArrayList(Stringkey) {

       return(ArrayList<String>) mValues.get(key);

    }

 

    /**

    * Returns a string containing a concise, human-readable description of this object.

    * @return a printable representation of this object.

    */

    @Override

    public String toString() {

       StringBuilder sb = newStringBuilder();

       for(String name : mValues.keySet()){

           String value = getAsString(name);

           if(sb.length() > 0) sb.append(" ");

           sb.append(name + "="+ value);

       }

       returnsb.toString();

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值