Code example for ContentValues

Methods: containsKeygetAsLongremovesize

0
                return null; 
            } 
        } 
 
        public Long getAsLong(String key) {
            if (mAfter != null && mAfter.containsKey(key)) {
                return mAfter.getAsLong(key);
            } else if (mBefore != null && mBefore.containsKey(key)) {
                return mBefore.getAsLong(key);
            } else { 
                return null; 
            } 
        } 
 
        public Integer getAsInteger(String key) {
            return getAsInteger(key, null);
        } 
 
        public Integer getAsInteger(String key, Integer defaultValue) {
            if (mAfter != null && mAfter.containsKey(key)) {
                return mAfter.getAsInteger(key);
            } else if (mBefore != null && mBefore.containsKey(key)) {
                return mBefore.getAsInteger(key);
            } else { 
                return defaultValue;
            } 
        } 
 
        public String getMimetype() {
            return getAsString(Data.MIMETYPE);
        } 
 
        public Long getId() {
            return getAsLong(mIdColumn);
        } 
 
        public void setIdColumn(String idColumn) {
            mIdColumn = idColumn;
        } 
 
        public boolean isPrimary() { 
            final Long isPrimary = getAsLong(Data.IS_PRIMARY);
            return isPrimary == null ? false : isPrimary != 0;
        } 
 
        public void setFromTemplate(boolean isFromTemplate) {
            mFromTemplate = isFromTemplate;
        } 
 
        public boolean isFromTemplate() { 
            return mFromTemplate;
        } 
 
        public boolean isSuperPrimary() { 
            final Long isSuperPrimary = getAsLong(Data.IS_SUPER_PRIMARY);
            return isSuperPrimary == null ? false : isSuperPrimary != 0;
        } 
 
        public boolean beforeExists() { 
            return (mBefore != null && mBefore.containsKey(mIdColumn));
        } 
 
        public boolean isVisible() { 
            // When "after" is present, then visible 
            return (mAfter != null);
        } 
 
        public boolean isDelete() { 
            // When "after" is wiped, action is "delete" 
            return beforeExists() && (mAfter == null);
        } 
 
        public boolean isTransient() { 
            // When no "before" or "after", is transient 
            return (mBefore == null) && (mAfter == null);
        } 
 
        public boolean isUpdate() { 
            // When "after" has some changes, action is "update" 
            return beforeExists() && (mAfter != null && mAfter.size() > 0);
        } 
 
        public boolean isNoop() { 
            // When "after" has no changes, action is no-op 
            return beforeExists() && (mAfter != null && mAfter.size() == 0);
        } 
 
        public boolean isInsert() { 
            // When no "before" id, and has "after", action is "insert" 
            return !beforeExists() && (mAfter != null);
        } 
 
        public void markDeleted() { 
            mAfter = null;
        } 
 
        /** 
         * Ensure that our internal structure is ready for storing updates. 
         */ 
        private void ensureUpdate() { 
            if (mAfter == null) {
                mAfter = new ContentValues();
            } 
        } 
 
        public void put(String key, String value) {
            ensureUpdate(); 
            mAfter.put(key, value);
        } 
 
        public void put(String key, byte[] value) {
            ensureUpdate(); 
            mAfter.put(key, value);
        } 
 
        public void put(String key, int value) {
            ensureUpdate(); 
            mAfter.put(key, value);
        } 
 
        /** 
         * Return set of all keys defined through this object. 
         */ 
        public Set<String> keySet() {
            final HashSet<String> keys = Sets.newHashSet();
 
            if (mBefore != null) {
                for (Map.Entry<String, Object> entry : mBefore.valueSet()) {
                    keys.add(entry.getKey());
                } 
            } 
 
            if (mAfter != null) {
                for (Map.Entry<String, Object> entry : mAfter.valueSet()) {
                    keys.add(entry.getKey());
                } 
            } 
 
            return keys;
        } 
 
        /** 
         * Return complete set of "before" and "after" values mixed together, 
         * giving full state regardless of edits. 
         */ 
        public ContentValues getCompleteValues() {
            final ContentValues values = new ContentValues();
            if (mBefore != null) {
                values.putAll(mBefore);
            } 
            if (mAfter != null) {
                values.putAll(mAfter);
            } 
            if (values.containsKey(GroupMembership.GROUP_ROW_ID)) {
                // Clear to avoid double-definitions, and prefer rows 
                values.remove(GroupMembership.GROUP_SOURCE_ID);
            } 
 
            return values;
        } 
 
        /** 
         * Merge the "after" values from the given {@link ValuesDelta}, 
         * discarding any existing "after" state. This is typically used when 
         * re-parenting changes onto an updated {@link Entity}. 
         */ 
        public static ValuesDelta mergeAfter(ValuesDelta local, ValuesDelta remote) {
            // Bail early if trying to merge delete with missing local 
            if (local == null && (remote.isDelete() || remote.isTransient())) return null;
 
            // Create local version if none exists yet 
            if (local == null) local = new ValuesDelta();
 
            if (!local.beforeExists()) {
                // Any "before" record is missing, so take all values as "insert" 
                local.mAfter = remote.getCompleteValues();
            } else { 
                // Existing "update" with only "after" values 
                local.mAfter = remote.mAfter;
            } 
 
            return local;
        } 
 
        @Override 
        public boolean equals(Object object) {
            if (object instanceof ValuesDelta) {
                // Only exactly equal with both are identical subsets 
                final ValuesDelta other = (ValuesDelta)object;
                return this.subsetEquals(other) && other.subsetEquals(this);
            } 
            return false; 
        } 
 
        @Override 
        public String toString() {
            final StringBuilder builder = new StringBuilder();
            toString(builder);
            return builder.toString();
        } 
 
        /** 
         * Helper for building string representation, leveraging the given 
         * {@link StringBuilder} to minimize allocations. 
         */ 
        public void toString(StringBuilder builder) {
            builder.append("{ ");
            for (String key : this.keySet()) {
                builder.append(key);
                builder.append("=");
                builder.append(this.getAsString(key));
                builder.append(", ");
            } 
            builder.append("}");
        } 
 
        /** 
         * Check if the given {@link ValuesDelta} is both a subset of this 
         * object, and any defined keys have equal values. 
         */ 
        public boolean subsetEquals(ValuesDelta other) {
            for (String key : this.keySet()) {
                final String ourValue = this.getAsString(key);
                final String theirValue = other.getAsString(key);
                if (ourValue == null) {
                    // If they have value when we're null, no match 
                    if (theirValue != null) return false;
                } else { 
                    // If both values defined and aren't equal, no match 
                    if (!ourValue.equals(theirValue)) return false;
                } 
            } 
            // All values compared and matched 
            return true; 
        } 
 
        /** 
         * Build a {@link ContentProviderOperation} that will transform our 
         * "before" state into our "after" state, using insert, update, or 
         * delete as needed. 
         */ 
        public ContentProviderOperation.Builder buildDiff(Uri targetUri) {
            Builder builder = null;
            if (isInsert()) { 
                // Changed values are "insert" back-referenced to Contact 
                mAfter.remove(mIdColumn);
                builder = ContentProviderOperation.newInsert(targetUri);
                builder.withValues(mAfter);
            } else if (isDelete()) { 
                // When marked for deletion and "before" exists, then "delete" 
                builder = ContentProviderOperation.newDelete(targetUri);