The Android development series was seen by Contentvalues

Source: Internet
Author: User
Tags class definition

This blog inside I want to focus on the analysis of Contentvalues source and it involved in the inheritance interface Parcelabel, there are hashmap source.

Believe that the use of Android inside the database operation of friends for Contentvalues must not feel strange, it is actually very much like a Dictionary object, can be used to store key value pairs. For example the code is as follows:

Contentvalues contentvalues=new contentvalues () contentvalues.put ("name", "Xiao"); Contentvalues.put ("Age", 20); Contentvalues.put ("Isstudent", true);

You will find that contentvalues can be used to put all kinds of data, how does it have this magical function? Let's take a look at its source code below. First, it is the definition of the Contentvalues class:

Public final class Contentvalues implements Parcelable {}

We can see that it implements the Parcelabel interface, which is mainly used for data installation and transport related operations. Speaking of which, let us also look at the Parcelabel interface inside exactly what the definition of the method, the source code is as follows:

Public interface Parcelable {public       static final int parcelable_write_return_value = 0x0001;        public static final int contents_file_descriptor = 0x0001;        public int describecontents ();        public void Writetoparcel (Parcel dest, int flags);    Public interface Creator<t> {public               T createfromparcel (Parcel source);                Public t[] NewArray (int size);    }    Public interface Classloadercreator<t> extends creator<t> {public               T createfromparcel (Parcel source, ClassLoader loader);}    }

We can see that there is a Writetoparcel method is used to transfer data, as to how it is used to package data, it is necessary to see the implementation of the implementation of the Parcelabel interface class.

All right, let's go back to the point we're going to discuss. Contentvalues, first look at the contentvalues included in the constructor, the source code is as follows:

    Private hashmap<string, object> mvalues; Public contentvalues () {//Choosing a default size of 8 based on analysis of typical//consumption by APPL        Ications.    Mvalues = new hashmap<string, object> (8);  }/** * Creates an empty set of values using the given initial size * * @param size The initial size of the    Set of values */public contentvalues (int size) {mvalues = new hashmap<string, object> (size, 1.0f); }/** * Creates a set of values copied from the given set * * @param from the values to copy */P    Ublic contentvalues (contentvalues from) {mvalues = new hashmap<string, object> (from.mvalues); }/** * Creates a set of values copied from the given HashMap.     This was used * by the Parcel unmarshalling code.  * * @param values the values to start with * {@hide} */Private Contentvalues (hashmap<string, object> Values) {mvalues = VALues; }

  I believe you can see from the comments, the structure of contentvalues is mainly based on the specific parameters passed in the code to construct the corresponding HashMap object, and then the various put operations, get operations, The remove operation is for HashMap, where the method of the put type is as follows:

 public void put (string key, String value) {Mvalues.put (key, value);    } public void Putall (Contentvalues other) {Mvalues.putall (other.mvalues);    } public void put (String key, Byte value) {mvalues.put (key, value);    } public void put (String key, short value) {Mvalues.put (key, value);    } public void put (String key, Integer value) {mvalues.put (key, value);    } public void put (String key, Long value) {mvalues.put (key, value);    } public void put (String key, Float value) {mvalues.put (key, value);    } public void put (String key, Double value) {mvalues.put (key, value);    } public void put (String key, Boolean value) {mvalues.put (key, value);    } public void put (String key, byte[] value) {Mvalues.put (key, value);    } public void Putnull (String key) {mvalues.put (key, NULL); }

  With the above method, we can understand why Contentvalues can put various types of values, then let's look at the Get method, the source code is as follows:

 Public Object get (String key) {return mvalues.get (key);        } public string getasstring (string key) {Object value = Mvalues.get (key); return value! = null?    Value.tostring (): null;        } public Long Getaslong (String key) {Object value = Mvalues.get (key); try {return value! = null?        (number) value). Longvalue (): null; } catch (ClassCastException e) {if (value instanceof charsequence) {try {R                Eturn long.valueof (value.tostring ()); } catch (NumberFormatException E2) {LOG.E (TAG, "Cannot parse Long value for" + Value + "at key" + K                    EY);                return null;                }} else {log.e (TAG, "cannot cast value for" + key + "to a Long:" + value, e);            return null;        }}} public Integer Getasinteger (String key) {Object value = Mvalues.get (key);       try {     return value! = null?        (number) value). Intvalue (): null; } catch (ClassCastException e) {if (value instanceof charsequence) {try {R                Eturn integer.valueof (value.tostring ()); } catch (NumberFormatException E2) {LOG.E (TAG, "Cannot parse Integer value for" + Value + "at key"                    + key);                return null;                }} else {log.e (TAG, "cannot cast value for" + key + "to a Integer:" + value, e);            return null;        }}} public short Getasshort (String key) {Object value = Mvalues.get (key); try {return value! = null?        (number) value). Shortvalue (): null; } catch (ClassCastException e) {if (value instanceof charsequence) {try {R                Eturn short.valueof (value.tostring ()); } catch (NumberFormatException E2) {LoG.E (TAG, "Cannot parse short value for" + Value + "at key" + key);                return null;                }} else {log.e (TAG, "cannot cast value for" + key + "to a short:" + value, e);            return null;        }}} public Byte Getasbyte (String key) {Object value = Mvalues.get (key); try {return value! = null?        (number) value). Bytevalue (): null; } catch (ClassCastException e) {if (value instanceof charsequence) {try {R                Eturn byte.valueof (value.tostring ()); } catch (NumberFormatException E2) {LOG.E (TAG, "Cannot parse Byte value for" + Value + "at key" + K                    EY);                return null;                }} else {log.e (TAG, "cannot cast value for" + key + "to a Byte:" + value, e);            return null; }}} public Double getasdouble (String Key) {Object value = Mvalues.get (key); try {return value! = null?        (number) value). Doublevalue (): null; } catch (ClassCastException e) {if (value instanceof charsequence) {try {R                Eturn double.valueof (value.tostring ());  } catch (NumberFormatException E2) {LOG.E (TAG, "Cannot parse Double value for" + Value + "at key" +                    Key);                return null;                }} else {log.e (TAG, "cannot cast value for" + key + "to a Double:" + value, e);            return null;        }}} public Float getasfloat (String key) {Object value = Mvalues.get (key); try {return value! = null?        (number) value). Floatvalue (): null; } catch (ClassCastException e) {if (value instanceof charsequence) {try {R       Eturn float.valueof (value.tostring ());         } catch (NumberFormatException E2) {LOG.E (TAG, "Cannot parse Float value for" + Value + "at                    Key "+ key);                return null;                }} else {log.e (TAG, "cannot cast value for" + key + "to a Float:" + value, e);            return null;        }}} public Boolean Getasboolean (String key) {Object value = Mvalues.get (key);        try {return (Boolean) value; } catch (ClassCastException e) {if (value instanceof charsequence) {return boolean.valueof (val            Ue.tostring ());            } else if (value instanceof number) {return (number) value). Intvalue ()! = 0;                } else {log.e (TAG, "cannot cast value for" + key + "to a Boolean:" + value, e);            return null;        }}} public byte[] Getasbytearray (String key) {Object value = Mvalues.get (key); if (valueinstanceof byte[]) {return (byte[]) value;        } else {return null; }    }

With the code above, we can see very intuitively that different get methods call different types of ((number) value). The Intvalue method strongly turns the fetch, and returns NULL if it is not available.

Since Contentvalues is based on HashMap to achieve the operation, then we need to look at HashMap what is going on? The first is the HashMap class definition, the source code is as follows:

public class hashmap<k, v> extends Abstractmap<k, v> implements Cloneable, serializable{}

From the code above, we can see that HashMap is built on generics and implements both cloning and serialization interfaces. This means that, to a certain extent, we can instantiate any type of hashmap, and make it a clone, serialization function, see the following code:

Hashmap<integer,object> hashone=new hashmap<> (); Hashmap<string,object> hashtwo=new hashmap<> (); Hashmap<boolean,object> hashthree=new hashmap<> (); Hashmap<float,object> hashfour=new hashmap<> ();

It's just that we usually get used to the string type key in the project. OK, let us continue to look down, the first thing to say is hashmapentry internal static class, the source code is as follows:

Static Class Hashmapentry<k, V> implements Entry<k, v> {final K key;        V value;        final int hash;        Hashmapentry<k, v> next;            Hashmapentry (K key, V value, int hash, hashmapentry<k, v> next) {This.key = key;            This.value = value;            This.hash = hash;        This.next = Next;        } Public final K GetKey () {return key;        Public final V GetValue () {return value;            Public final V SetValue (v value) {v oldValue = this.value;            This.value = value;        return oldValue; } @Override Public Final Boolean equals (Object o) {if (!            o instanceof Entry)) {return false;            } entry<?,? > E = (entry<?,? >) o;        Return Objects.equal (E.getkey (), key) && objects.equal (E.getvalue (), value); } @Override Public Final INT Hashcode () {return (key = = null? 0:key.hashcode ()) ^ (value = = null? 0:VALUE.HASHC        Ode ());        } @Override Public final String toString () {return key + "=" + value; }    }

The

 hashmapentry class implements the entry interface, and the entry interface is an internal interface inside the map interface. By implementing the entry interface, the HashMap has getkey/getvalue/setvalue and other related functions. At the same time we can see hashmap inside the good multi-functional implementation are aimed at hashmapentry expansion. Another important concept of HASHMAP is the set interface, let us take a look at the final type of private internal class EntrySet, the source code is as follows:

Private Final class EntrySet extends Abstractset<entry<k, v>> {public        iterator<entry<k, v> > iterator () {            return newentryiterator ();        }        Public Boolean contains (Object o) {            if (!) ( o instanceof Entry))                return false;            entry<?,? > E = (entry<?,? >) o;            Return containsmapping (E.getkey (), E.getvalue ());        }        public boolean remove (Object o) {            if (!) ( o instanceof Entry))                return false;            entry<?,? > E = (entry<?,? >) o;            Return removemapping (E.getkey (), E.getvalue ());        }        public int size () {            return size;        }        public Boolean isEmpty () {            return size = = 0;        }        public void Clear () {            HashMap.this.clear ();        }    }

As its name, the set interface is primarily provided with HASHMAP-related operations. Let's look at the source code inside the set interface, as follows:

    Public boolean Add (E object);    public boolean addall (collection<? extends e> Collection);    public void Clear ();    Public Boolean contains (Object object);    public boolean Containsall (collection<?> Collection);    public Boolean equals (Object object);    public int hashcode ();    public boolean isEmpty ();    Public iterator<e> Iterator ();    Public boolean remove (Object object);    public boolean RemoveAll (collection<?> Collection);    public boolean Retainall (collection<?> Collection);    public int size ();    Public object[] ToArray ();    Public <T> t[] ToArray (t[] array);

Well, today's blog is here. Technology is limited, if there is no welcome to shoot Bricks!

The Android development series was seen by Contentvalues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.