Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (c) 2012 Todoroo Inc
 *
 * See the file "LICENSE" for the full license governing this code.
 */

import android.content.ContentValues;

import java.util.Map.Entry;

public class Main {
    public static final String SEPARATOR_ESCAPE = "!PIPE!";
    public static final String SERIALIZATION_SEPARATOR = "|";

    /**
     * Serializes a content value into a string
     */
    public static String contentValuesToSerializedString(ContentValues source) {
        StringBuilder result = new StringBuilder();
        for (Entry<String, Object> entry : source.valueSet()) {
            addSerialized(result, entry.getKey(), entry.getValue());
        }
        return result.toString();
    }

    /** add serialized helper */
    private static void addSerialized(StringBuilder result, String key, Object value) {
        result.append(key.replace(SERIALIZATION_SEPARATOR, SEPARATOR_ESCAPE)).append(SERIALIZATION_SEPARATOR);
        if (value instanceof Integer) {
            result.append('i').append(value);
        } else if (value instanceof Double) {
            result.append('d').append(value);
        } else if (value instanceof Long) {
            result.append('l').append(value);
        } else if (value instanceof String) {
            result.append('s').append(value.toString().replace(SERIALIZATION_SEPARATOR, SEPARATOR_ESCAPE));
        } else if (value instanceof Boolean) {
            result.append('b').append(value);
        } else {
            throw new UnsupportedOperationException(value.getClass().toString());
        }
        result.append(SERIALIZATION_SEPARATOR);
    }
}