Android Open Source - android-tools Model Helper






From Project

Back to project page android-tools.

License

The source code is released under:

MIT License

If you think the Android project android-tools listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.comfreeze.lib.api.helper;
//from  w  w  w  .  ja  v  a  2 s . co m
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;

import net.comfreeze.lib.api.helper.ModelHelper.ModelHelperMap.ElementType;

import org.json.custom.JSONException;
import org.json.custom.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;

abstract public class ModelHelper<T> {
    public LinkedHashMap<String, ModelHelperMap> map;
    public LinkedHashMap<String, Object> dataMap;

    public T fromJSON(JSONObject source) {
        if (null != source) {
            HashMap<String, ElementType> indexes = getTypeIndex(MAP_INDEX.JSON);
            if (null != indexes) {
                Iterator<String> keys = indexes.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    dataMap.put(key, JSONHelper.get(source, key));
                }
            }
        }
        return (T) this;
    }

    public T fromCursor(Cursor source) {
        if (null != source) {
            HashMap<String, ElementType> indexes = getTypeIndex(MAP_INDEX.SQL);
            if (null != indexes) {
                Iterator<String> keys = indexes.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    dataMap.put(key, CursorHelper.get(source, key, indexes.get(key)));
                }
            }
        }
        return (T) this;
    }

    public T fromBundle(Bundle source) {
        if (null != source) {
            HashMap<String, ElementType> indexes = getTypeIndex(MAP_INDEX.JAVA);
            if (null != indexes) {
                Iterator<String> keys = indexes.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    ElementType type = indexes.get(key);
                    switch (type) {
                        case SQL_INTEGER:
                        case INTEGER:
                            dataMap.put(key, source.getInt(key));
                            break;
                        case LONG:
                            dataMap.put(key, source.getLong(key));
                            break;
                        case DOUBLE:
                            dataMap.put(key, source.getDouble(key));
                            break;
                        case SQL_REAL:
                        case FLOAT:
                            dataMap.put(key, source.getFloat(key));
                            break;
                        case SQL_TEXT:
                        case STRING:
                            dataMap.put(key, source.getString(key));
                            break;
                        case BYTE:
                            dataMap.put(key, source.getByte(key));
                            break;
                        case BYTE_ARRAY:
                        case SQL_BLOB:
                            dataMap.put(key, source.getByteArray(key));
                            break;
                    }
                }
            }
        }
        return (T) this;
    }

    public JSONObject toJSONObject() {
        JSONObject output = new JSONObject();
        if (null != dataMap) {
            HashMap<String, ElementType> indexes = getTypeIndex(MAP_INDEX.JSON);
            if (null != indexes) {
                Iterator<String> keys = indexes.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    if (dataMap.containsKey(key)) {
                        try {
                            output.put(key, dataMap.get(key));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return output;
    }

    public ContentValues toContentValues() {
        ContentValues output = new ContentValues();
        if (null != dataMap) {
            HashMap<String, ElementType> indexes = getTypeIndex(MAP_INDEX.SQL);
            if (null != indexes) {
                Iterator<String> keys = indexes.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    ElementType type = indexes.get(key);
                    switch (type) {
                        case SQL_INTEGER:
                        case INTEGER:
                            output.put(key, (Integer) dataMap.get(key));
                            break;
                        case SQL_REAL:
                        case FLOAT:
                            output.put(key, (Float) dataMap.get(key));
                            break;
                        case DOUBLE:
                            output.put(key, (Double) dataMap.get(key));
                            break;
                        case LONG:
                            output.put(key, (Long) dataMap.get(key));
                            break;
                        case SQL_TEXT:
                        case STRING:
                            output.put(key, (String) dataMap.get(key));
                            break;
                        case BYTE:
                            output.put(key, (Byte) dataMap.get(key));
                            break;
                        case BYTE_ARRAY:
                        case SQL_BLOB:
                            output.put(key, (byte[]) dataMap.get(key));
                            break;
                    }
                }
            }
        }
        return output;
    }

    public Bundle toBundle() {
        Bundle output = new Bundle();
        if (null != dataMap) {
            HashMap<String, ElementType> indexes = getTypeIndex(MAP_INDEX.JAVA);
            if (null != indexes) {
                Iterator<String> keys = indexes.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    ElementType type = indexes.get(key);
                    switch (type) {
                        case SQL_INTEGER:
                        case INTEGER:
                            output.putInt(key, (Integer) dataMap.get(key));
                            break;
                        case SQL_REAL:
                        case FLOAT:
                            output.putFloat(key, (Float) dataMap.get(key));
                            break;
                        case DOUBLE:
                            output.putDouble(key, (Double) dataMap.get(key));
                            break;
                        case LONG:
                            output.putLong(key, (Long) dataMap.get(key));
                            break;
                        case SQL_TEXT:
                        case STRING:
                            output.putString(key, (String) dataMap.get(key));
                            break;
                        case BYTE:
                            output.putByte(key, (Byte) dataMap.get(key));
                            break;
                        case BYTE_ARRAY:
                        case SQL_BLOB:
                            output.putByteArray(key, (byte[]) dataMap.get(key));
                            break;
                    }
                }
            }
        }
        return output;
    }

    public HashMap<String, ElementType> getTypeIndex(String index) {
        HashMap<String, ElementType> indexes = new HashMap<String, ElementType>();
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            ModelHelperMap itemMap = map.get(key);
            if (itemMap.hasMap(index))
                indexes.put(key, itemMap.getMap(index));
        }
        return indexes;
    }

    // private Class<?> clazz;
    //
    // public ModelHelper(Class<?> clazz) {
    // this.clazz = clazz;
    // }
    public static final class MAP_INDEX {
        public static final String UNKNOWN = "UNKNOWN";
        public static final String JSON = "JSON";
        public static final String SQL = "SQL";
        public static final String JAVA = "JAVA";
    }

    public static class ModelHelperMap {
        public HashMap<String, ElementType> types = new HashMap<String, ElementType>();
        public Object defaultValue = null;

        public static ModelHelperMap newInstance(HashMap<String, ElementType> types, Object defaultValue) {
            ModelHelperMap map = new ModelHelperMap();
            map.types = types;
            map.defaultValue = defaultValue;
            return map;
        }

        public static ModelHelperMap newInstance(Object defaultValue) {
            ModelHelperMap map = new ModelHelperMap();
            map.defaultValue = defaultValue;
            return map;
        }

        public ModelHelperMap addMap(String index, ElementType type) {
            types.put(index, type);
            return this;
        }

        public ElementType getMap(String index) {
            return types.get(index);
        }

        public boolean hasMap(String index) {
            return types.containsKey(index);
        }

        public static enum ElementType {
            UNKNOWN, //
            // SQLITE3
            SQL_INTEGER, SQL_REAL, //
            SQL_TEXT, SQL_BLOB, //
            // JSON
            JSON_OBJECT, JSON_ARRAY, //
            // Others
            FLOAT, LONG, DOUBLE, STRING, //
            INTEGER, BYTE, BYTE_ARRAY, //
            BOOLEAN, SHORT //
        }
    }
}




Java Source Code List

net.comfreeze.lib.BundleBuilder.java
net.comfreeze.lib.CFZApplication.java
net.comfreeze.lib.ContentValueBuilder.java
net.comfreeze.lib.FragmentMap.java
net.comfreeze.lib.adapter.HeaderListAdapter.java
net.comfreeze.lib.adapter.IHeaderListAdapter.java
net.comfreeze.lib.adapter.SeparatedListAdapter.java
net.comfreeze.lib.api.BaseAPI.java
net.comfreeze.lib.api.RestAPI.java
net.comfreeze.lib.api.XMLAPI.java
net.comfreeze.lib.api.helper.CursorHelper.java
net.comfreeze.lib.api.helper.JSONHelper.java
net.comfreeze.lib.api.helper.ModelHelper.java
net.comfreeze.lib.api.xml.WordpressAPI.java
net.comfreeze.lib.audio.SoundManager.java
net.comfreeze.lib.db.DatabaseHelper.java
net.comfreeze.lib.db.DatabaseTable.java
net.comfreeze.lib.db.helper.HelperCursor.java
net.comfreeze.lib.db.model.CFZModel.java
net.comfreeze.lib.db.model.FieldColumnMap.java
net.comfreeze.lib.fragments.CFZListFragment.java
net.comfreeze.lib.provider.CFZSimpleProvider.java
net.comfreeze.lib.service.CFZService.java
net.comfreeze.lib.ui.SupportFragmentActivity.java
net.comfreeze.lib.ui.dialog.CFZDialogProgress.java
net.comfreeze.lib.ui.fragment.CFZFragmentBase.java
net.comfreeze.lib.views.BiScrollView.java
net.comfreeze.lib.views.CFZViewHelper.java
net.comfreeze.lib.views.FlowLayout.java
net.comfreeze.lib.views.GestureHelper.java
net.comfreeze.lib.views.HeaderListView.java
net.comfreeze.lib.views.ResizingView.java
net.comfreeze.lib.views.ViewCollection.java
net.comfreeze.lib.views.ViewUtils.java
net.comfreeze.lib.xml.XMLParser.java
net.comfreeze.lib.xml.wordpress.FeedXmlParser.java